Bird
0
0

You have a sales table with columns region, salesperson, and amount. Write a query to find regions where the average sales amount per salesperson is greater than 500.Which query is correct?

hard📝 Application Q8 of 15
SQL - GROUP BY and HAVING

You have a sales table with columns region, salesperson, and amount. Write a query to find regions where the average sales amount per salesperson is greater than 500.

Which query is correct?

ASELECT region, salesperson, AVG(amount) FROM sales GROUP BY region HAVING AVG(amount) > 500;
BSELECT region, AVG(amount) FROM sales GROUP BY region, salesperson HAVING AVG(amount) > 500;
CSELECT region, AVG(amount) FROM sales GROUP BY region HAVING AVG(amount) > 500;
DSELECT region, AVG(amount) FROM sales HAVING AVG(amount) > 500 GROUP BY region;
Step-by-Step Solution
Solution:
  1. Step 1: Understand grouping needed

    To find average per salesperson in each region, group by both region and salesperson.
  2. Step 2: Apply HAVING to filter averages > 500

    HAVING AVG(amount) > 500 filters groups with average sales above 500.
  3. Final Answer:

    SELECT region, AVG(amount) FROM sales GROUP BY region, salesperson HAVING AVG(amount) > 500; -> Option B
  4. Quick Check:

    Group by region and salesperson, then HAVING filter = C [OK]
Quick Trick: Group by all non-aggregated columns before HAVING [OK]
Common Mistakes:
MISTAKES
  • Grouping only by region when salesperson is selected
  • Placing HAVING before GROUP BY

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes