Bird
0
0

You have a students table with columns class and score. You want to find classes where the average score is at least 75 and the number of students is more than 10. Which query achieves this?

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

You have a students table with columns class and score. You want to find classes where the average score is at least 75 and the number of students is more than 10. Which query achieves this?

ASELECT class, AVG(score), COUNT(*) FROM students GROUP BY class HAVING AVG(score) >= 75 AND COUNT(*) > 10;
BSELECT class, AVG(score), COUNT(*) FROM students HAVING AVG(score) >= 75 AND COUNT(*) > 10 GROUP BY class;
CSELECT class, AVG(score), COUNT(*) FROM students WHERE AVG(score) >= 75 AND COUNT(*) > 10 GROUP BY class;
DSELECT class, AVG(score), COUNT(*) FROM students GROUP BY class WHERE AVG(score) >= 75 AND COUNT(*) > 10;
Step-by-Step Solution
Solution:
  1. Step 1: Group students by class

    Use GROUP BY class to group rows by class.
  2. Step 2: Filter groups with HAVING using aggregate conditions

    Use HAVING AVG(score) >= 75 AND COUNT(*) > 10 to keep classes meeting both conditions.
  3. Final Answer:

    SELECT class, AVG(score), COUNT(*) FROM students GROUP BY class HAVING AVG(score) >= 75 AND COUNT(*) > 10; -> Option A
  4. Quick Check:

    HAVING filters groups by multiple aggregates [OK]
Quick Trick: Use HAVING for multiple aggregate filters after GROUP BY [OK]
Common Mistakes:
MISTAKES
  • Placing HAVING before GROUP BY
  • Using WHERE with aggregate functions
  • Putting WHERE after GROUP BY

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes