SQL
All Top 50 SQL Interview Questions#22
Filter Aggregated Groups with HAVING Clause in SQL
MediumAmazonInterview Question #22
Asked at AmazonFrom `orders`, find all `customer_id`s who have placed at least 5 orders AND whose total spend exceeds $10,000.
Input Table: orders
6 rows preview| order_id | customer_id | amount |
|---|---|---|
| 1 | 101 | 3000 |
| 2 | 101 | 3000 |
| 3 | 101 | 2500 |
| 4 | 101 | 1500 |
| 5 | 101 | 500 |
| 6 | 102 | 12000 |
Expected Output Structure1 rows
| customer_id | order_count | total_spend |
|---|---|---|
| 101 | 5 | 10500 |
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.