Skip to content

Instantly share code, notes, and snippets.

@piti118
Created October 31, 2025 02:58
Show Gist options
  • Select an option

  • Save piti118/710416c5308d1e65fd68c16947fccb1f to your computer and use it in GitHub Desktop.

Select an option

Save piti118/710416c5308d1e65fd68c16947fccb1f to your computer and use it in GitHub Desktop.
### SQL Question
You are given the following three tables:
**`customers`**
| customer_id | name |
|-------------:|-------------|
| 1 | Alice |
| 2 | Bob |
| 3 | Charlie |
**`orders`**
| order_id | customer_id | order_date |
|---------:|-------------:|-------------|
| 101 | 1 | 2025-01-10 |
| 102 | 2 | 2025-01-12 |
| 103 | 1 | 2025-01-14 |
**`order_items`**
| item_id | order_id | product_name | quantity | price |
|--------:|---------:|--------------|---------:|------:|
| 1001 | 101 | Keyboard | 1 | 50 |
| 1002 | 101 | Mouse | 2 | 25 |
| 1003 | 102 | Monitor | 1 | 200 |
| 1004 | 103 | Mouse | 1 | 25 |
---
### Question
Write an SQL query to show **each customer’s total spending**, including customers who have not made any orders, sorted by the **highest spender first**.
### Example output
| customer_name | total_spent |
|---------------|-------------|
| Alice | 125 |
| Bob | 200 |
| Charlie | 0 |
@piti118
Copy link
Author

piti118 commented Oct 31, 2025

SQL Question

You are given the following three tables:

customers

customer_id name
1 Alice
2 Bob
3 Charlie

orders

order_id customer_id order_date
101 1 2025-01-10
102 2 2025-01-12
103 1 2025-01-14

order_items

item_id order_id product_name quantity price
1001 101 Keyboard 1 50
1002 101 Mouse 2 25
1003 102 Monitor 1 200
1004 103 Mouse 1 25

Question

Write an SQL query to show each customer’s total spending, including customers who have not made any orders, sorted by the highest spender first.

Example output

customer_name total_spent
Alice 125
Bob 200
Charlie 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment