SQL traps and time periods in three sessions
Some queries run without an error and still return the wrong number: a join that repeats rows, a whole-number division, a comparison with NULL, a tie that LIMIT cuts off. Interviewers use these on purpose, because they show who checks a result and who trusts it.
Ten exercises in three sessions. The first two cover the traps, the third covers dates and periods: year-over-year growth, overlapping bookings, merging periods and the peak number at one time.
Session 1: Wrong numbers without an error
- Revenue inflated by a join (fan-out)For each customer with at least one paid order (status paid), return the name, the total amount of their paid orders and the total quantity of items on those orders. An order without item lines counts with quantity 0. Customer names are unique.
- Average lines per order (whole-number division)Return the average number of item lines per order, over the orders that have item lines, rounded to 2 decimals.
- Not equal, and the rows with NULLReturn the names of all employees who do not work in the Engineering department. Employees with no department count too.
Session 2: Pick the right row
- Most orders, ties includedWhich customers placed the most orders, counting orders of every status? Return the name and the number of orders of every customer who shares first place. Customer names are unique.
- Last event per user, with a tie-breakReturn each user's last event: user_id, event and event_time. When two events of a user have the same time, the one with the higher id is the later one.
- Price at the time of the orderprice_history holds the price of a product from each valid_from date onward. For every order line of a product that has a price on the order date, return the order id, the product id and the price that was valid on the order date. A price counts from its valid_from date itself.
Session 3: Dates and periods
- Year-over-year growthFor every month of 2025 that is in the table, return the month, its revenue, the revenue of the same month one year earlier, and the growth as a percentage rounded to 1 decimal. When there is no figure for the same month a year earlier, the last two columns are NULL. When that figure is 0, the growth is NULL.
- Overlapping bookingsA booking occupies its room from check_in up to, but not including, check_out. Return every pair of bookings for the same room that overlap, as two ids with the lower id first. A booking that starts on the day another one ends does not overlap it.
- 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.
- Peak number of bookings at onceA booking occupies its room from check_in up to, but not including, check_out. Return one number: the highest number of bookings that are in the hotel at the same time, over all rooms.
Other plans and cases
- 14-day SQL interview sprint (41)
- SQL for data analyst interviews (24)
- SQL for BI analyst interviews (21)
- SQL for data engineer interviews (24)
- Product analytics SQL in four sessions (12)
- Window functions in six sessions (18)
- SQL on messy data: one work week (16)
- PostgreSQL features in four sessions (11)
- SQL business case: the refund review (5)
- SQL business case: the payroll review (5)
- SQL business case: the monthly revenue report (4)
- SQL business case: customer value and segments (4)
- SQL business case: product performance (4)
- All exercises by level
Your first 8 exercises are free, with no account, from any plan or level. Progress is kept in this browser; a free account keeps it on every device.