CHAPTER 13
Aggregating by category — the analytics workhorse.
🌟 Think of it this way: Two spreadsheets: one with customer names+IDs, another with order amounts+customer IDs. A JOIN merges them so you can see 'Ananya spent ₹1,45,148'. Without JOINs, you would have to manually cross-reference the sheets.
Returns rows only where there is a match in BOTH tables. Lisa (no orders) is excluded.
SQL
SELECT c.name, o.order_id, o.total, o.status FROM customers c INNER JOIN orders o ON c.customer_id = o.customer_id ORDER BY c.name, o.order_id;
Find customers and their order details only for orders greater than 3000. This shows how INNER JOIN works with filtering conditions.
SQL
SELECT c.name, o.order_id, o.total FROM customers c INNER JOIN orders o ON c.customer_id = o.customer_id WHERE o.total > 3000;
✅ Pro Tip: This is commonly used in business to identify high-value customers.
| customer_id | name | city | country |
|---|---|---|---|
| 1 | Ananya | Hyderabad | India |
| 2 | Rohan | Bangalore | India |
| 3 | Sam | Mumbai | India |
| 4 | Lisa | London | UK |
| 5 | Ravi | Delhi | India |
| order_id | customer_id | order_date | total | status |
|---|---|---|---|---|
| 1 | 1 | 2024-01-15 | 79999 | completed |
| 2 | 2 | 2024-01-20 | 3498 | completed |
| 3 | 1 | 2024-02-10 | 65000 | completed |
| 4 | 3 | 2024-02-14 | 499 | pending |
| 5 | 2 | 2024-03-01 | 248 | completed |
| 6 | 5 | 2024-03-15 | 2999 | shipped |
| 7 | 1 | 2024-04-05 | 149 | completed |