Bird
0
0

You want to find the average order amount per customer but only for customers who placed more than 3 orders. Which query correctly achieves this?

hard📝 Application Q15 of 15
PostgreSQL - Aggregate Functions and GROUP BY
You want to find the average order amount per customer but only for customers who placed more than 3 orders. Which query correctly achieves this?
ASELECT customer_id, AVG(amount) FROM orders WHERE COUNT(*) > 3 GROUP BY customer_id;
BSELECT customer_id, AVG(amount) FROM orders HAVING COUNT(*) > 3 GROUP BY customer_id;
CSELECT customer_id, AVG(amount) FROM orders GROUP BY customer_id WHERE COUNT(*) > 3;
DSELECT customer_id, AVG(amount) FROM orders GROUP BY customer_id HAVING COUNT(*) > 3;
Step-by-Step Solution
Solution:
  1. Step 1: Understand filtering groups with HAVING

    HAVING filters groups after aggregation; WHERE filters rows before aggregation.
  2. Step 2: Check query order and clauses

    SELECT customer_id, AVG(amount) FROM orders GROUP BY customer_id HAVING COUNT(*) > 3; correctly uses GROUP BY then HAVING COUNT(*) > 3 to filter customers with more than 3 orders.
  3. Final Answer:

    SELECT customer_id, AVG(amount) FROM orders GROUP BY customer_id HAVING COUNT(*) > 3; -> Option D
  4. Quick Check:

    Use HAVING to filter groups after GROUP BY [OK]
Quick Trick: Use HAVING to filter groups, not WHERE [OK]
Common Mistakes:
  • Using WHERE to filter aggregated counts
  • Placing HAVING before GROUP BY
  • Syntax errors mixing WHERE and HAVING

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes