SQL Aggregation
In this SQL lesson, we will study Overview through small executable cases. First we will make the normal result clear; then inspect data flow, ownership, failure behavior, and the caller contract before adding framework or infrastructure complexity.
Professional code is readable under change. A future developer should be able to identify which input is trusted, who may update state, where I/O happens, and how an expected error reaches a caller without reconstructing the whole application.
What You Will Build
10 focused SQL examples for Overview. Each has a visible result, an explicit contract, and a next change to test so the lesson becomes reusable knowledge rather than passive reading.
Example 1: Create a bounded table
Let’s isolate one SQL Aggregation rule together. Run it, trace the inputs and output, then change one boundary value before reusing the pattern. Small observable programs turn syntax into a reliable engineering habit.
Keep contracts explicit: validate data before use, make ownership and mutation visible, and return a named result or failure a caller can test. Static types and a successful build help, but runtime boundaries still need intentional checks.
SQL
CREATE TABLE topic (
id bigint PRIMARY KEY,
title varchar(120) NOT NULL,
created_at timestamp NOT NULL
);Expected Result
topic stores an id, required title, and creation time
Try next: make the nearest empty, invalid, missing, or boundary case observable instead of leaving behavior to an unchecked assumption.
Example 2: Insert one intentional row
Let’s isolate one SQL Aggregation rule together. Run it, trace the inputs and output, then change one boundary value before reusing the pattern. Small observable programs turn syntax into a reliable engineering habit.
Keep contracts explicit: validate data before use, make ownership and mutation visible, and return a named result or failure a caller can test. Static types and a successful build help, but runtime boundaries still need intentional checks.
SQL
INSERT INTO topic (id, title, created_at)
VALUES (1, 'SQL', CURRENT_TIMESTAMP);Expected Result
One topic row is added
Try next: make the nearest empty, invalid, missing, or boundary case observable instead of leaving behavior to an unchecked assumption.
Example 3: Select only needed columns
Let’s isolate one SQL Aggregation rule together. Run it, trace the inputs and output, then change one boundary value before reusing the pattern. Small observable programs turn syntax into a reliable engineering habit.
Keep contracts explicit: validate data before use, make ownership and mutation visible, and return a named result or failure a caller can test. Static types and a successful build help, but runtime boundaries still need intentional checks.
SQL
SELECT id, title
FROM topic
ORDER BY created_at DESC;Expected Result
Topics are returned newest first
Try next: make the nearest empty, invalid, missing, or boundary case observable instead of leaving behavior to an unchecked assumption.
Example 4: Filter before sorting
Let’s isolate one SQL Aggregation rule together. Run it, trace the inputs and output, then change one boundary value before reusing the pattern. Small observable programs turn syntax into a reliable engineering habit.
Keep contracts explicit: validate data before use, make ownership and mutation visible, and return a named result or failure a caller can test. Static types and a successful build help, but runtime boundaries still need intentional checks.
SQL
SELECT id, title
FROM topic
WHERE title LIKE '%SQL%'
ORDER BY title ASC
LIMIT 20;Expected Result
At most twenty matching rows are returned alphabetically
Try next: make the nearest empty, invalid, missing, or boundary case observable instead of leaving behavior to an unchecked assumption.
Example 5: Join related records
Let’s isolate one SQL Aggregation rule together. Run it, trace the inputs and output, then change one boundary value before reusing the pattern. Small observable programs turn syntax into a reliable engineering habit.
Keep contracts explicit: validate data before use, make ownership and mutation visible, and return a named result or failure a caller can test. Static types and a successful build help, but runtime boundaries still need intentional checks.
SQL
SELECT t.title, c.name
FROM topic t
JOIN category c ON c.id = t.category_id
WHERE c.active = TRUE;Expected Result
Only topics in active categories are returned
Try next: make the nearest empty, invalid, missing, or boundary case observable instead of leaving behavior to an unchecked assumption.
Example 6: Aggregate after grouping
Let’s isolate one SQL Aggregation rule together. Run it, trace the inputs and output, then change one boundary value before reusing the pattern. Small observable programs turn syntax into a reliable engineering habit.
Keep contracts explicit: validate data before use, make ownership and mutation visible, and return a named result or failure a caller can test. Static types and a successful build help, but runtime boundaries still need intentional checks.
SQL
SELECT category_id, count(*) AS topics
FROM topic
GROUP BY category_id
HAVING count(*) >= 5;Expected Result
Only categories with five or more topics are returned
Try next: make the nearest empty, invalid, missing, or boundary case observable instead of leaving behavior to an unchecked assumption.
Example 7: Use EXISTS for a related condition
Let’s isolate one SQL Aggregation rule together. Run it, trace the inputs and output, then change one boundary value before reusing the pattern. Small observable programs turn syntax into a reliable engineering habit.
Keep contracts explicit: validate data before use, make ownership and mutation visible, and return a named result or failure a caller can test. Static types and a successful build help, but runtime boundaries still need intentional checks.
SQL
SELECT u.id, u.name
FROM app_user u
WHERE EXISTS (
SELECT 1
FROM purchase p
WHERE p.user_id = u.id
);Expected Result
Only users with at least one purchase are returned
Try next: make the nearest empty, invalid, missing, or boundary case observable instead of leaving behavior to an unchecked assumption.
Example 8: Name a readable CTE
Let’s isolate one SQL Aggregation rule together. Run it, trace the inputs and output, then change one boundary value before reusing the pattern. Small observable programs turn syntax into a reliable engineering habit.
Keep contracts explicit: validate data before use, make ownership and mutation visible, and return a named result or failure a caller can test. Static types and a successful build help, but runtime boundaries still need intentional checks.
SQL
WITH recent_topics AS (
SELECT id, title
FROM topic
WHERE created_at >= CURRENT_DATE - INTERVAL '7 days'
)
SELECT * FROM recent_topics;Expected Result
The final query reads from the named recent_topics result
Try next: make the nearest empty, invalid, missing, or boundary case observable instead of leaving behavior to an unchecked assumption.
Example 9: Rank rows in each group
Let’s isolate one SQL Aggregation rule together. Run it, trace the inputs and output, then change one boundary value before reusing the pattern. Small observable programs turn syntax into a reliable engineering habit.
Keep contracts explicit: validate data before use, make ownership and mutation visible, and return a named result or failure a caller can test. Static types and a successful build help, but runtime boundaries still need intentional checks.
SQL
SELECT
category_id,
title,
row_number() OVER (PARTITION BY category_id ORDER BY created_at DESC) AS position
FROM topic;Expected Result
Each topic has a newest-first position within its category
Try next: make the nearest empty, invalid, missing, or boundary case observable instead of leaving behavior to an unchecked assumption.
Example 10: Protect a multi-step write
Let’s isolate one SQL Aggregation rule together. Run it, trace the inputs and output, then change one boundary value before reusing the pattern. Small observable programs turn syntax into a reliable engineering habit.
Keep contracts explicit: validate data before use, make ownership and mutation visible, and return a named result or failure a caller can test. Static types and a successful build help, but runtime boundaries still need intentional checks.
SQL
BEGIN;
UPDATE account SET balance = balance - 100 WHERE id = 1;
UPDATE account SET balance = balance + 100 WHERE id = 2;
COMMIT;Expected Result
Both balance changes commit together or neither does
Try next: make the nearest empty, invalid, missing, or boundary case observable instead of leaving behavior to an unchecked assumption.
Example 11: Question: Which paid orders should fulfilment ship next?
Question: The fulfilment team needs the three newest paid orders, with the customer name and total, so it can create a short shipping queue.
Read the sample rows first. Then predict the result table before running the SQL query. We are using this query lab to reinforce SQL Aggregation with a decision a real team could make.
Sample Data
| order_id | customer | status | total |
|---|---|---|---|
| 501 | Asha | PAID | 1200.00 |
| 502 | Ravi | PAID | 850.00 |
| 503 | Mira | PENDING | 640.00 |
| 504 | Asha | PAID | 310.00 |
SQL
SELECT
o.id AS order_id,
c.name AS customer,
o.total
FROM orders AS o
JOIN customers AS c ON c.id = o.customer_id
WHERE o.status = 'PAID'
ORDER BY o.created_at DESC
FETCH FIRST 3 ROWS ONLY;Expected Result
| order_id | customer | total |
|---|---|---|
| 504 | Asha | 310.00 |
| 502 | Ravi | 850.00 |
| 501 | Asha | 1200.00 |
Try next: Change PAID to PENDING and explain why the queue is different before you execute the query.
Example 12: Question: How much has each customer spent?
Question: Finance wants one row per customer, counting only paid orders, so it can compare confirmed revenue without mixing in pending work.
Read the sample rows first. Then predict the result table before running the SQL query. We are using this query lab to reinforce SQL Aggregation with a decision a real team could make.
Sample Data
| customer | paid_order | total |
|---|---|---|
| Asha | 501 | 1200.00 |
| Asha | 504 | 310.00 |
| Ravi | 502 | 850.00 |
| Mira | 503 | 640.00 pending |
SQL
SELECT
c.name AS customer,
COUNT(o.id) AS paid_orders,
SUM(o.total) AS total_spent
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.id
WHERE o.status = 'PAID'
GROUP BY c.id, c.name
ORDER BY total_spent DESC;Expected Result
| customer | paid_orders | total_spent |
|---|---|---|
| Asha | 2 | 1510.00 |
| Ravi | 1 | 850.00 |
Try next: Add a HAVING clause that keeps only customers whose total_spent is at least 1000.00.
Example 13: Question: Which customers have not placed an order?
Question: A support campaign needs customers with no order rows at all. The left join keeps every customer before the null check identifies the missing relationship.
Read the sample rows first. Then predict the result table before running the SQL query. We are using this query lab to reinforce SQL Aggregation with a decision a real team could make.
Sample Data
| customer_id | customer | order_id |
|---|---|---|
| 10 | Asha | 501 |
| 11 | Ravi | 502 |
| 12 | Mira | 503 |
| 13 | Dev | — |
SQL
SELECT
c.id AS customer_id,
c.name AS customer
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.id
WHERE o.id IS NULL
ORDER BY c.name;Expected Result
| customer_id | customer |
|---|---|
| 13 | Dev |
Try next: Move a paid-order filter into the join condition and predict which customers remain visible.
Example 14: Question: Who is first in each region by paid spend?
Question: Sales wants a leaderboard that restarts for every region. A window function ranks rows without collapsing the individual customer rows.
Read the sample rows first. Then predict the result table before running the SQL query. We are using this query lab to reinforce SQL Aggregation with a decision a real team could make.
Sample Data
| region | customer | paid_spend |
|---|---|---|
| North | Asha | 1510.00 |
| North | Ravi | 850.00 |
| South | Mira | 920.00 |
| South | Dev | 400.00 |
SQL
SELECT
c.region,
c.name AS customer,
SUM(o.total) AS paid_spend,
RANK() OVER (
PARTITION BY c.region
ORDER BY SUM(o.total) DESC
) AS regional_rank
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.id
WHERE o.status = 'PAID'
GROUP BY c.region, c.name
ORDER BY c.region, regional_rank;Expected Result
| region | customer | paid_spend | regional_rank |
|---|---|---|---|
| North | Asha | 1510.00 | 1 |
| North | Ravi | 850.00 | 2 |
| South | Mira | 920.00 | 1 |
| South | Dev | 400.00 | 2 |
Try next: Replace RANK with ROW_NUMBER and discuss what changes if two customers tie.
Example 15: Question: Which recent paid customers are in the North region?
Question: The operations dashboard needs a named intermediate result for recently paid customers, then filters that readable set by region.
Read the sample rows first. Then predict the result table before running the SQL query. We are using this query lab to reinforce SQL Aggregation with a decision a real team could make.
Sample Data
| customer | region | paid_at |
|---|---|---|
| Asha | North | 2026-09-12 |
| Ravi | North | 2026-09-10 |
| Mira | South | 2026-09-13 |
SQL
WITH recent_paid_customers AS (
SELECT DISTINCT
c.id, c.name, c.region
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.id
WHERE o.status = 'PAID'
AND o.created_at >= CURRENT_DATE - INTERVAL '7 days'
)
SELECT name AS customer, region
FROM recent_paid_customers AS c
WHERE c.region LIKE 'North%'
ORDER BY customer;Expected Result
| customer | region |
|---|---|
| Asha | North |
| Ravi | North |
Try next: Change the seven-day boundary to thirty days and say which additional rows you expect before running it.
Design Review and Worked Trace
Review SQL Aggregation as a full trace: input, validation, calculation or side effect, output, and cleanup. State the normal result, then state the boundary behavior. If either is hard to explain, split the work into a smaller function with a narrower contract.
Keep secret values and privileged decisions outside display code. Authentication establishes an identity; authorization decides what that identity may do. Check both close to protected operations, and use intentional failure responses rather than leaking internal details or crashing generically.
- Run the normal input and record the exact result.
- Test an empty, malformed, or boundary input deliberately.
- Keep state, ownership, and side effects visible to the caller.
- Return a result or named failure that can be tested.
- Never log credentials, tokens, or sensitive input.
This discipline makes SQL code easier to review, safer to change, and more useful in production than a collection of untested syntax fragments.
Common Mistakes
- Skipping input validation because a local example happened to work.
- Hiding mutation, ownership, or failure behavior inside a convenient shortcut.
- Using a client-side display rule as the only protection for sensitive work.
- Copying code without changing an input to prove its contract.
