Number of Unique Subjects Taught by Each Teacher
EasyLeetCodeSQL: Aggregation & GROUP BY
Hint and exampleShowHide
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
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | Classes With at Least 5 Students | LeetCode | Easy | Solve Classes With at Least 5 Students on LeetCode (opens in a new tab) | |||
| 2 | Duplicate Emails | LeetCode | Easy | Solve Duplicate Emails on LeetCode (opens in a new tab) | |||
| 3 | Number of Unique Subjects Taught by Each Teacher | LeetCode | Easy | Solve Number of Unique Subjects Taught by Each Teacher on LeetCode (opens in a new tab) | |||
| 4 | Customer Placing the Largest Number of Orders | LeetCode | Easy | Solve Customer Placing the Largest Number of Orders on LeetCode (opens in a new tab) | |||
| 5 | Biggest Single Number | LeetCode | Easy | Solve Biggest Single Number on LeetCode (opens in a new tab) |