Meta Data#
Difficulty: easy First Attempt: 2021-10-10
- Submissions: Wrong Answer ×3 → Runtime Error → Wrong Answer → Accepted
Approach#
As in LC 182 this filters groups (SQL Template 4): GROUP BY class with HAVING ... >= 5. Count distinct students, COUNT(DISTINCT student), so repeats do not count twice. The current problem makes (student, class) the primary key, so COUNT(*) also works, but DISTINCT is safer.
My Attempts#
This took 6 submissions, all about duplicate rows:
- The first versions used
COUNT(class), and the 2021 test data listed the same student in the same class more than once, double counting them; attempt 1 also usedSELECT *. - Trying
FROM distinct(courses)to deduplicate was invalid syntax (RE). - Computing
COUNT(DISTINCT student)in a subquery and filtering>= 5outside passed; putting it straight intoHAVINGworks without the subquery.
Solution#
SELECT class
FROM Courses
GROUP BY class
HAVING COUNT(DISTINCT student) >= 5;
Complexity#
Grouping is O(n) or O(n log n).
Mistakes to Avoid#
SELECT *withGROUP BYfails in strict mode; select only grouped columns.- Count distinct students with
COUNT(DISTINCT student).
Interview Explanation#
I group by class and keep classes with at least five distinct students via HAVING, using COUNT DISTINCT so repeated rows do not count twice.
