Illustrative tables: customers (id, name, country) holds 1 Asha UK, 2 Ben US and 3 Chen UK. orders (id, customer_id, amount) holds (1, 1, 50), (2, 1, 30), (3, 2, 20) and (4, 3, 40). Find total order value by country, keeping only countries above 100.
| Clause | SQL | Effect |
|---|
| Join | `FROM customers c JOIN orders o ON o.customer_id = c.id` | Four joined rows, one per order |
| Group | `GROUP BY c.country` | UK rows: 50, 30, 40; US rows: 20 |
| Aggregate | `SELECT c.country, SUM(o.amount) AS total, COUNT(*) AS orders` | UK 120 from 3 orders; US 20 from 1 order |
| Filter groups | `HAVING SUM(o.amount) > 100` | Only UK, 120, remains |
WHERE filters rows before grouping; HAVING filters groups after aggregation. Using WHERE SUM(...) is a common error.