356. Find Islands (Consecutive Dates)

Hard Infosys Advanced & Tricky
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.
patients
10 cols
doctors
4 cols
admissions
7 cols
province_names
2 cols

Reference Solution

Expected SQL
WITH distinct_dates AS (SELECT DISTINCT admission_date FROM admissions), grp AS (SELECT admission_date, DATE(admission_date, '-' || ROW_NUMBER() OVER (ORDER BY admission_date) || ' days') AS grp_key FROM distinct_dates) SELECT MIN(admission_date) AS island_start, MAX(admission_date) AS island_end, COUNT(*) AS days_in_island FROM grp GROUP BY grp_key ORDER BY island_start;
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.