Reviewed by Aditya Kumar · Last reviewed 2026-03-24
WITH ranked AS (SELECT CustomerName, sale_date, ROW_NUMBER() OVER (PARTITION BY CustomerName ORDER BY sale_date) AS rn FROM sales WHERE CustomerName = 'Alice Johnson') SELECT DATEDIFF((SELECT sale_date FROM ranked WHERE rn = 3), (SELECT sale_date FROM ranked WHERE rn = 1)) AS...
WITH ranked AS (SELECT CustomerName, sale_date, ROW_NUMBER() OVER (PARTITION BY CustomerName ORDER BY sale_date) AS rn FROM sales WHERE CustomerName = 'Alice Johnson') SELECT DATEDIFF((SELECT sale_date FROM ranked WHERE rn = 3), (SELECT sale_date FROM ranked WHERE rn = 1)) AS day_diff FROM ranked LIMIT 1. Or LAG/LEAD. Why: Extract 1st and 3rd sale dates; compute difference. Edge case: Fewer than 3 sales—returns NULL.
Red Flag: Not handling customers with <3 sales. Pro-Move: 'We use LAG(sale_date, 2) for 3rd; COALESCE for missing.'
According to DataEngPrep.tech, this is one of the most frequently asked SQL interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.