SQL interview question: longest consecutive order-day streak

HardConsecutive streaks

Problem

Shared data rules

For each buyer, find the longest streak of consecutive days with orders. Return the buyers ranked 1–5 by streak length; equal streaks share a rank, and the rank isn’t an output column.

Formula

Example

Input: orders
order_iduser_idstatuscreated_atRow note
11delivered2026-08-10 12:00
21delivered2026-08-11 09:30
31delivered2026-08-12 11:00
42delivered2026-08-14 10:00
52delivered2026-08-15 13:00
62delivered2026-08-15 18:00← second order that day
72delivered2026-08-16 13:00
Output
user_idstart_dayend_daydays
12026-08-102026-08-123
22026-08-142026-08-163

Both buyers have a 3-day streak — they share rank 1.

Notes

Solve it free

Your query runs on PostgreSQL in your browser. No sign-up needed.

Skills it tests

More interview questions

All questions