SQL
All Top 50 SQL Interview Questions#11
Calculate Customer Lifetime Value with INNER JOIN in SQL
EasyAmazonInterview Question #11
Asked at AmazonGiven `customers` (customer_id, name) and `orders` (order_id, customer_id, total_amount), write a SQL query to return each customer's `name`, total number of orders `order_count`, and total spend `total_spent`. Exclude customers who haven't placed orders.
Input Table: customers & orders
4 rows preview| customer_id | name | order_id | total_amount |
|---|---|---|---|
| 1 | Aarav | 101 | 1500 |
| 1 | Aarav | 102 | 2500 |
| 2 | Bhavna | 103 | 3000 |
| 3 | Chirag | NULL | NULL |
Expected Output Structure2 rows
| name | order_count | total_spent |
|---|---|---|
| Aarav | 2 | 4000 |
| Bhavna | 1 | 3000 |
Interview Context
Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.
PostgreSQL 15
Ready to execute
Click "Run Query" or press Ctrl + Enter to test your query.
Output will be verified against the test dataset.