AnalystPath

Roll Up Sunny and Rainy Streaks

SQLHardSenior level~15 min

Problem

Table: `RainyDays`

```text
+-----------+------+
| Column | Type |
+-----------+------+
| rain_day | date |
+-----------+------+
rain_day is the primary key. Each row is a calendar day on which it rained.
```

Table: `SunnyDays`

```text
+-----------+------+
| Column | Type |
+-----------+------+
| sun_day | date |
+-----------+------+
sun_day is the primary key. Each row is a calendar day that was sunny.
```

Across the year **2023**, every day is either rainy or sunny (never both), and the two tables together cover a set of dates. Collapse consecutive same-weather days into streaks. For each maximal run of consecutive days with the same weather, output one row with the weather and the run's first and last day.

Return `weather` (`'rainy'` or `'sunny'`), `streak_start`, `streak_end`, ordered by `streak_start`. Only consider days in 2023.

Tables

Example rows — the live problem includes the full dataset.

RainyDays
rain_day
2023-01-04
2023-01-05
SunnyDays
sun_day
2023-01-01
2023-01-02
2023-01-03

Expected output

Your answer should return 3 rows with the columns weather, streak_start, streak_end.

Starter code (SQL)

SELECT *
FROM RainyDays;

Solve this SQL question free

Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related SQL questions