Day 3 is where SQL becomes job-powerful. This is the day interviewers care about most. You move from “retrieving rows” → to analyzing data. 💯
Aggregation means:
Taking many rows and producing a single summarized value.
Counts rows.
SELECT COUNT(*) FROM users;Counts all rows including NULL values.
Count specific column:
SELECT COUNT(purchases) FROM users;Adds numeric values.
SELECT SUM(purchases) FROM users;Returns mean value.
SELECT AVG(age) FROM users;SELECT MIN(age), MAX(age) FROM users;Aggregate functions:
- Collapse multiple rows
- Return one result (unless grouped)
- Ignore NULL values (except COUNT(*))
Internally:
Database engine:
- Scans rows
- Applies function accumulator
- Returns final result
For large datasets: Indexes may help MIN/MAX But SUM/AVG usually require scanning relevant rows.
This is where SQL thinking changes.
It changes result structure.
Instead of: “One result for whole table”
You get: “One result per group”
Make sure your users table includes purchases and country.
SELECT country, COUNT(*)
FROM users
GROUP BY country;Output:
| country | count |
|---|---|
| USA | 3 |
| UK | 2 |
Every selected column must either:
- Be aggregated
- Or be inside GROUP BY
This fails:
SELECT country, age
FROM users
GROUP BY country;Why?
Because SQL doesn’t know which age to pick.
HAVING filters AFTER grouping.
Example:
SELECT country, COUNT(*)
FROM users
GROUP BY country
HAVING COUNT(*) > 2;WHERE filters rows. HAVING filters groups.
Execution order:
- FROM
- WHERE
- GROUP BY
- HAVING
- SELECT
- ORDER BY
Now we think like a company.
SELECT name, purchases
FROM users
ORDER BY purchases DESC
LIMIT 3;SELECT country, SUM(purchases)
FROM users
GROUP BY country;SELECT country, AVG(age)
FROM users
GROUP BY country;SELECT country, SUM(purchases)
FROM users
GROUP BY country
HAVING SUM(purchases) > 10;SELECT MAX(signup_year) FROM users;SELECT *
FROM users
WHERE signup_year = (SELECT MAX(signup_year) FROM users);Without GROUP BY:
Rows → Single summary
With GROUP BY:
Rows → Buckets → Summary per bucket
This is similar to:
MapReduce pattern in distributed systems.
When you run:
SELECT country, COUNT(*)
FROM users
GROUP BY country;Database:
- Reads rows
- Hashes or sorts by country
- Creates aggregation buckets
- Maintains counters
- Outputs grouped result
Two strategies:
- Hash aggregation
- Sort aggregation
We revisit this when discussing indexes.
Count total users.
Find average age of users.
Find total purchases made by users from USA.
Find number of users per signup_year.
Find countries where average age is greater than 28.
Find the signup_year with the highest number of users. (Hint: GROUP BY + ORDER BY + LIMIT)
Find the second highest age in the table. (You may use ORDER BY + LIMIT with offset in PostgreSQL/MySQL.)
- https://pgexercises.com/
- https://leetcode.com/problemset/database/
- https://www.hackerrank.com/domains/sql
- https://sqlzoo.net/
Focus specifically on:
- GROUP BY
- HAVING
- Aggregate interview questions
You should now:
✔ Understand aggregation deeply ✔ Write GROUP BY confidently ✔ Use HAVING properly ✔ Think in terms of business metrics ✔ Solve common SQL interview problems
Tomorrow (Day 4): We enter JOINS.
That is where SQL becomes real backend engineering. It’s also where most beginners struggle.