Bird
0
0

You want to find the average sales amount per customer but only for customers with more than 5 sales. Which query correctly achieves this?

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

    Grouping by customer_id allows aggregation per customer.
  2. Step 2: Filter groups with HAVING clause

    HAVING filters groups after aggregation; COUNT(*) > 5 keeps customers with more than 5 sales.
  3. Final Answer:

    SELECT customer_id, AVG(amount) FROM sales GROUP BY customer_id HAVING COUNT(*) > 5; -> Option A
  4. Quick Check:

    Use HAVING to filter groups after aggregation [OK]
Quick Trick: Use HAVING to filter aggregated groups, not WHERE [OK]
Common Mistakes:
  • Using WHERE with COUNT()
  • Placing HAVING before GROUP BY
  • Omitting GROUP BY clause

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes