Bird
0
0

You want to list customers who have placed more than 3 orders, each with amount over 50. Which query correctly applies WHERE and HAVING?

hard📝 Application Q8 of 15
SQL - GROUP BY and HAVING
You want to list customers who have placed more than 3 orders, each with amount over 50. Which query correctly applies WHERE and HAVING?
ASELECT customer_id FROM orders GROUP BY customer_id HAVING amount > 50 AND COUNT(*) > 3;
BSELECT customer_id FROM orders WHERE amount > 50 GROUP BY customer_id HAVING COUNT(*) > 3;
CSELECT customer_id FROM orders HAVING amount > 50 AND COUNT(*) > 3 GROUP BY customer_id;
DSELECT customer_id FROM orders WHERE COUNT(*) > 3 AND amount > 50 GROUP BY customer_id;
Step-by-Step Solution
Solution:
  1. Step 1: Filter individual orders

    Use WHERE to filter orders with amount > 50 before grouping.
  2. Step 2: Filter groups by order count

    Use HAVING to filter customers with more than 3 such orders.
  3. Final Answer:

    SELECT customer_id FROM orders WHERE amount > 50 GROUP BY customer_id HAVING COUNT(*) > 3; -> Option B
  4. Quick Check:

    WHERE filters rows; HAVING filters groups [OK]
Quick Trick: WHERE filters rows; HAVING filters groups after aggregation [OK]
Common Mistakes:
MISTAKES
  • Using HAVING to filter individual rows
  • Placing aggregate conditions in WHERE

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes