I'm working on the following schema (bold text stands for pk, and ":" stand for referenced tables) :
USERS(username, name, surname);
PRODUCTS(id, name, quantity);
PURCHASES(user:USERS, product:PRODUCTS, dateAndTime, quantityPurchased);
I want to find name and surname of the user who has made the max number of purchases.
Firstly I use a nested query to find out the number of purchases for each user and then I select the user which purchased >= ALL those values:
SELECT name, surname, username
FROM users JOIN purchases ON username = user
GROUP BY name, surname, username
HAVING count(*) >= ALL(
SELECT count(*)
FROM utenti JOIN acquisti ON username = user
GROUP BY username)
Is there another way to achieve the same without using nested queries?
Thank you in advance for your time.