Bird
0
0

Which of the following SQL queries correctly groups sales by product and orders the result by total sales descending?

easy📝 Syntax Q12 of 15
SQL - GROUP BY and HAVING
Which of the following SQL queries correctly groups sales by product and orders the result by total sales descending?
ASELECT product, SUM(amount) FROM sales GROUP BY product ORDER BY SUM(amount) DESC;
BSELECT product, SUM(amount) FROM sales ORDER BY product GROUP BY SUM(amount) DESC;
CSELECT product, SUM(amount) FROM sales GROUP BY product ORDER BY product DESC;
DSELECT product, SUM(amount) FROM sales ORDER BY SUM(amount) GROUP BY product DESC;
Step-by-Step Solution
Solution:
  1. Step 1: Check GROUP BY and ORDER BY order

    The correct syntax is GROUP BY first, then ORDER BY to sort grouped results.
  2. Step 2: Verify ordering by aggregated column

    Ordering by SUM(amount) DESC sorts by total sales descending, matching the requirement.
  3. Final Answer:

    SELECT product, SUM(amount) FROM sales GROUP BY product ORDER BY SUM(amount) DESC; -> Option A
  4. Quick Check:

    GROUP BY then ORDER BY with aggregate [OK]
Quick Trick: GROUP BY before ORDER BY; order by aggregate for totals [OK]
Common Mistakes:
MISTAKES
  • Swapping GROUP BY and ORDER BY order
  • Ordering by non-aggregated columns incorrectly
  • Using ORDER BY before GROUP BY

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes