Empowering CS learners, aspiring programmers, and startups with AI, Data Science & Programming insights — scaling skills from learning foundations to enterprise-grade solutions.
GROUP BY and ORDER BY clauses: Is its usage correct ?
›Forums›SQL›GROUP BY and ORDER BY clauses: Is its usage correct ?
The question in this quiz refer to the open source Chinook Database. Please familiarize yourself with the ER diagram to familiarize yourself with the table and column names to write accurate queries and get the appropriate answers.
Run Query: Show the number of orders placed by each customer (hint: this is found in the invoices table) and sort the result by the number of orders in descending order.
Here is my solution:
SELECT Count(InvoiceId) AS NoOfOrdersbyEachCustomer
FROM Invoices
GROUP BY CustomerId
ORDER BY NoOfOrdersbyEachCustomer DESC
Is it correct?
Reply
Although it is correct, not very useful as you cannot say which order by which customer. Add customerid in the select clause.
UPDATE
After adding CustomerId with SELECT, the output appears useful