Bird
0
0

Given the table orders with columns customer_id and total, what will this query return?

medium📝 query result Q4 of 15
SQL - GROUP BY and HAVING

Given the table orders with columns customer_id and total, what will this query return?

SELECT customer_id, SUM(total) AS total_spent FROM orders GROUP BY customer_id HAVING SUM(total) > 1000;
AAll customers with total orders less than or equal to 1000
BAll orders with total greater than 1000
CAll customers with total orders greater than 1000
DSyntax error due to HAVING clause
Step-by-Step Solution
Solution:
  1. Step 1: Understand the GROUP BY and aggregation

    The query groups orders by customer_id and sums their total orders.
  2. Step 2: Apply HAVING filter

    HAVING SUM(total) > 1000 filters groups where total spent is more than 1000.
  3. Final Answer:

    All customers with total orders greater than 1000 -> Option C
  4. Quick Check:

    HAVING filters groups with sum > 1000 = B [OK]
Quick Trick: HAVING filters groups based on aggregate conditions [OK]
Common Mistakes:
MISTAKES
  • Thinking HAVING filters individual rows
  • Confusing total orders with individual order totals

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes