SQL for analysis · Build it
Added to cart but never ordered
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe interviewer asks
Using events(user_id, event_name, event_time), find the users who added something to cart in the last 7 days but didn't place an order within 24 hours of that add-to-cart. Marketing wants them for a reminder notification.
Then
Follow-ups the interviewer asks next
- Why
NOT EXISTSand notNOT IN? - A user added to cart three times this week and ordered after one of them. Are they on the list?
- The cart was on the app and the order on the website. Does your query catch that?
- The list goes out tomorrow at 10 am. Which users should you remove so the reminder doesn't annoy people?
- How would you measure whether the reminder works?
The answer
What a strong answer covers
- A correlated anti-join: cart events in the last 7 days,
WHERE NOT EXISTS (SELECT 1 FROM events o WHERE o.user_id = c.user_id AND o.event_name = 'order_placed' AND o.event_time >= c.event_time AND o.event_time < c.event_time + INTERVAL '24 hours'). - Or
LEFT JOIN … WHERE o.user_id IS NULL, with the time conditions in theON, not theWHERE. NOT INreturns no rows if the subquery has a NULL, and can't express the 24-hour window per cart.- Defines the unit: per cart event or per user. "Any cart without an order in 24 hours" and "no order at all this week" are different lists; asks marketing which they want.
- Carts from the last 24 hours haven't had their full window; leaves them out for now.
- Matches on a cross-platform user id, not a device id, so app-to-web journeys are caught.
- Removes users who have ordered since, opted out of notifications, got a reminder recently, or are test accounts.
- Measures with a holdout: keeps 10% of the list un-notified and compares order rates.
The red pen
Common traps
Avoid:time conditions on the right table in the WHERE of a LEFT JOIN (it becomes an inner join); DISTINCT everywhere to hide duplicates.
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voicePractise questions like this out loud
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.