Spend per customer
MediumJoinsE-commerce
The problem
Show every customer's name and the total amount of their delivered orders, as total_spent.
Customers with no delivered orders must still appear, with a total_spent of 0.
Learn this in the course: Joins and NULLs (video and notes)
Tables · Online shop
Click a table or column name to insert it into your query.
- INTEGER · primary key
- TEXT
- TEXT · can be NULL
- DATE
Sample rows (10 total)
| customer_id | name | city | signup_date |
|---|---|---|---|
| 1 | Aarav Mehta | Mumbai | 2025-01-10 |
| 2 | Diya Nair | Bengaluru | 2025-01-18 |
| 3 | Kabir Singh | Delhi | 2025-02-02 |
| 4 | Meera Iyer | Chennai | 2025-02-14 |
| 5 | Rohan Das | Kolkata | 2025-03-01 |
- INTEGER · primary key
- INTEGER · → customers
- DATE
- TEXT · delivered | cancelled | returned | pending
- INTEGER · rupees
Sample rows (20 total)
| order_id | customer_id | order_date | status | amount |
|---|---|---|---|---|
| 1 | 1 | 2025-05-01 | delivered | 1200 |
| 2 | 1 | 2025-05-15 | delivered | 800 |
| 3 | 1 | 2025-06-02 | cancelled | 500 |
| 4 | 2 | 2025-05-03 | delivered | 2500 |
| 5 | 2 | 2025-05-20 | delivered | 450 |
Ctrl/⌘ + Enter runs the query. Table and column names are suggested as you type. Press Esc then Tab to leave the editor with the keyboard.
Expected output (10 rows)
| name | total_spent |
|---|---|
| Meera Iyer | 3600 |
| Tara Joshi | 0 |
| Isha Kapoor | 4700 |
| Diya Nair | 4750 |
| Neel Patel | 0 |
| Kabir Singh | 300 |
| Rohan Das | 700 |
| Vikram Rao | 950 |
| Aarav Mehta | 2000 |
| Sana Khan | 0 |
Row order doesn’t matter for this question. Numbers are compared with a tiny tolerance; text must match exactly.
Stuck?
Stored on this device only. Your latest query and “solved” status are remembered in this browser only. They don’t sync between devices, and clearing your browser data removes them.