DBMS & SQL · Beginner · Topic 5 of 5
SQL: Aggregation & GROUP BY
5 questions.
Your progress0%
- 1st Visit
- 0/5
- 2nd Visit
- 0/5
- Revise
- 0/5
Overview
Aggregate functions such as COUNT, SUM, AVG, MIN and MAX turn many rows into one value. GROUP BY splits the rows into groups that share the same values, and each aggregate is computed once per group. WHERE filters rows before grouping, while HAVING filters groups after the aggregates are computed, so a condition such as COUNT(*) > 5 belongs in HAVING. Every non-aggregated column in SELECT should also appear in GROUP BY. Aggregates ignore NULL values, so COUNT(col) can be smaller than COUNT(*). Look for phrases such as "per customer", "for each department", "at least n" or "more than once".
SELECT dept, COUNT(*) AS staff, AVG(salary) AS avg_salary
FROM employees
WHERE active = 1 -- row filter
GROUP BY dept
HAVING COUNT(*) >= 5; -- group filter
Questions
| # | 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) |