🟢 Easy
Aggregate Functions
COUNT() SUM() AVG() MIN() MAX()
SQL
SELECT COUNT(*) AS employee_count FROM employees;
SELECT SUM(salary) AS total_salary FROM employees;
SELECT AVG(salary) AS average_salary FROM employees;
SELECT MIN(salary), MAX(salary) FROM employees;
🟡 Intermediate
GROUP BY
GROUP BY groups rows that share the same values and lets you calculate aggregates for each group.
SQL
SELECT department_id, AVG(salary) AS average_salary
FROM employees
GROUP BY department_id;
Mental Model
Employees → Group by department → Calculate average → One result per department
🟡 Intermediate
HAVING
HAVING filters groups after aggregation, unlike WHERE which filters individual rows before grouping.
SQL
SELECT department_id, AVG(salary) AS average_salary
FROM employees
GROUP BY department_id
HAVING AVG(salary) > 75000;
Filters groups, not rows.
🟡 Intermediate
WHERE vs HAVING
Understanding WHERE vs HAVING is crucial: WHERE filters rows before grouping, HAVING filters groups after aggregation.
| WHERE | HAVING |
|---|---|
| Filters rows | Filters groups |
| Before GROUP BY | After GROUP BY |
| No aggregate | With aggregate |
Rule: WHERE → individual rows, HAVING → grouped results
Interview Tip
WHERE filters before grouping, HAVING after. HAVING without GROUP BY is allowed but unusual.