- This topic is empty.
Viewing 1 post (of 1 total)
Viewing 1 post (of 1 total)
- You must be logged in to reply to this topic.
Empowering Insights, Unleashing Possibilities
Data Science/Business Analytics for Small Business Applications › Forums › SQL › GROUP BY and ORDER BY clauses: Is its usage correct ?
Tagged: Group By in SQL, Order By in SQL
Here is the problem:
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