AlgoMaster Logo

Exercise: Gaps and Islands

Last Updated: May 12, 2026

1 min read

For this exercise, we will work with below data:

SQL

Exercise 1

Easy

Write a query that returns the distinct streaming dates for each user, ordered by user_id then stream_date. Show user_id and stream_date. Cast started_at to a date.

SQL

Expected Query

Expected Output

Exercise 2

Medium

Using LEAD, write a query that returns each user's stream dates alongside their next stream date. Show user_idstream_date, and next_stream_date. Order by user_id then stream_date.

SQL

Expected Query

Expected Output

Exercise 3

Hard

Write a query that lists all dates between 2024-01-10 and 2024-01-22 (inclusive) on which no user streamed anything. Show missing_date ordered ascending.

SQL

Expected Query

Expected Output