Skip to content
We areTOP 1%

Number of Unique Subjects Taught by Each Teacher

EasyLeetCodeSQL: Aggregation & GROUP BY
Solve on LeetCode(opens in a new tab)
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews
Hint and exampleShow

When the same value can appear in several rows of a group, count it only once with COUNT(DISTINCT column).

Example: visits(patient, doctor, clinic)

patient | doctor | clinic
P1      | D1     | North
P1      | D1     | South
P1      | D2     | North
P2      | D3     | North
SELECT patient, COUNT(DISTINCT doctor) AS doctors
FROM visits
GROUP BY patient;
-- P1 | 2, P2 | 1   (COUNT(*) would wrongly give 3 for P1)

More in SQL: Aggregation & GROUP BY

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account