358. Consecutive Order-Day Streaks (per Employee)

Hard Deloitte Real-World Scenarios
Query Rules
  • The query columns and order must match the expected result schema.
  • NULL values should be handled appropriately where applicable.
  • The grader compares output values and sorting order to evaluate.

Schema Browser

Click a table's header to toggle expand/collapse.
locations
3 cols
departments
3 cols
employees
6 cols
orders
6 cols

Reference Solution

Expected SQL
WITH grp AS (SELECT emp_id, order_date, DATE(order_date, '-' || ROW_NUMBER() OVER (PARTITION BY emp_id ORDER BY order_date) || ' days') AS grp_key FROM orders), streaks AS (SELECT emp_id, COUNT(*) AS streak_length FROM grp GROUP BY emp_id, grp_key) SELECT emp_id, MAX(streak_length) AS longest_streak FROM streaks GROUP BY emp_id HAVING MAX(streak_length) >= 2 ORDER BY longest_streak DESC, emp_id;
SQL Editor
Ctrl+Enter to run
Console Output

Console Terminal

Write your query and click "Run Query" (Ctrl + Enter) to see results and testcase validation.