SQL interview question: find the bugs in a churn query

MediumCode review

Problem

Shared data rules

The colleague’s query returns an empty table. For each acquisition channel, find August 2026 buyers who bought nothing in September: their number, August orders and August revenue.

Query to fix

Fix it free
WITH august_orders AS (
  SELECT o.user_id,
         u.acq_channel,
         o.order_id,
         o.amount_usd
  FROM orders AS o
  JOIN users AS u
    ON u.user_id = o.user_id
  WHERE o.status = 'delivered'
    AND o.created_at >= '2026-08-01'
    AND o.created_at < '2026-08-31'
),
september_buyers AS (
  SELECT user_id
  FROM orders
  WHERE status = 'delivered'
    AND created_at >= '2026-09-01'
)
SELECT acq_channel,
       COUNT(DISTINCT user_id) AS churned_buyers,
       COUNT(*) AS august_orders,
       SUM(amount_usd) AS august_revenue_usd
FROM august_orders
WHERE user_id NOT IN (SELECT user_id FROM september_buyers)
GROUP BY acq_channel

Example

Input: users
user_idacq_channel
1ads
2organic
3organic
Input: orders
order_iduser_idcreated_atstatusamount_usdRow note
20112026-08-10 12:00delivered30
20222026-08-31 18:00delivered36
20332026-08-20 14:00delivered22
20432026-09-05 10:00delivered25← buyer 3 came back in September
205NULL2026-09-10 15:00delivered14← guest order
Expected output
acq_channelchurned_buyersaugust_ordersaugust_revenue_usd
ads1130
organic1136

Buyers 1 and 2 churned: they bought only in August. Buyer 3 bought in September too.

Fix it free

Fix the query — code checks it on PostgreSQL in your browser. No sign-up needed.

Skills it tests

More interview questions

All questions