SQL Dojo Beta
Pattern

Gaps and islands (streaks)

Group consecutive days (or numbers) into runs: streaks, sessions, periods.
SELECT day - CAST(ROW_NUMBER() OVER (ORDER BY day) AS INTEGER) AS island, day
FROM days;

Consecutive days share the same island; GROUP BY island gives one row per run.

WITH days AS (SELECT DISTINCT CAST(ordered_at AS DATE) AS day FROM orders),
grouped AS (
  SELECT day, day - CAST(ROW_NUMBER() OVER (ORDER BY day) AS INTEGER) AS island
  FROM days
)
SELECT MIN(day) AS first_day, MAX(day) AS last_day, COUNT(*) AS days_in_a_row
FROM grouped
GROUP BY island
ORDER BY days_in_a_row DESC, first_day
LIMIT 3;
first_daylast_daydays_in_a_row
2025-05-182025-08-1489
2024-07-232024-10-0979
2025-09-112025-11-1162
Try it in Randori