SQL CASE WHEN practice problems
CASE is the IF of SQL. It labels rows (small, medium, large), protects a division, and inside SUM or COUNT it turns one pass over the table into several columns, which is how a pivot is written.
The problems cover labels, conditional counts and sums, and the order of the WHEN branches, where the first match wins.
Try one here: Label with CASE
Try it yourself
Return each order id with a label: large when the amount is 150 or more, otherwise small.
orders (first 12 of 14 rows)
| id | customer_id | order_date | amount | status |
|---|---|---|---|---|
| 101 | 1 | 2025-01-03 | 120 | paid |
| 102 | 1 | 2025-01-20 | 80.5 | paid |
| 103 | 2 | 2025-01-22 | 200 | paid |
| 104 | 3 | 2025-02-02 | 45 | refunded |
| 105 | 3 | 2025-02-15 | 310 | paid |
| 106 | 4 | 2025-02-18 | 150 | pending |
| 107 | 5 | 2025-03-01 | 60 | paid |
| 108 | 6 | 2025-03-09 | 95 | paid |
| 109 | 6 | 2025-03-30 | 130 | paid |
| 110 | 6 | 2025-04-11 | 75 | paid |
| 111 | 7 | 2025-04-12 | 210 | paid |
| 112 | 1 | 2025-04-25 | 40 | paid |
Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.
Basics
- Label with CASEReturn each order id with a label: large when the amount is 150 or more, otherwise small.
Aggregation
- Paid and refunded per customerFor each customer_id that has orders, return the customer_id, the number of paid orders and the number of refunded orders, in that order. A customer with no refunds shows 0.
- Share of paid ordersWhat percentage of all orders have the status paid? Return a single number, rounded to 1 decimal.
- Salary bandsPut every employee in a salary band: junior below 70,000, mid from 70,000 up to but not including 100,000, senior at 100,000 or more. Return each band name and how many employees are in it.
Interview classics
- Pivot the statusesReturn one row with three columns: the number of paid orders, refunded orders and pending orders, in that order.
- Repeat purchase rateOf all customers who placed at least one order, what percentage placed two or more orders, whatever the status? Return a single number rounded to 1 decimal.
- Cohort retentionGroup customers by the month of their first order, like 2025-01, counting every order whatever the status. For each such month return the month, the number of customers who first ordered in it, and how many of those customers ordered again in a later month.
- Merge bookings into occupied periodsFor each room, merge bookings that overlap or touch (one ends on the day the next starts) into one occupied period. Return the room, the first check_in and the last check_out of every period.
Case studies
- Customer value, step 3: segment the customersLabel each customer with at least one paid order. Return name, total (their paid spend) and segment: 'high' for 400 or more, 'mid' for 150 or more, otherwise 'low'.
- Customer value, step 4: summarize the segmentsRoll the segments up. For each segment from step 3 return segment, the number of customers in it as customers, and their combined paid spend as revenue.
Read first if you are stuck
Other practice sets
Your first 8 exercises are free, with no account. Every answer is checked on the rows you see and again on changed data.