Islands are runs of consecutive values (consecutive login days, contiguous ids); gaps are the missing values between them. Classic solution: subtract ROW_NUMBER() OVER (PARTITION BY UserId ORDER BY LoginDate) days from each date; rows in the same island share the same result, so GROUP BY UserId, grp gives start and end with MIN/MAX. For gaps, use LEAD(value) OVER (ORDER BY value) and keep rows where the next value is more than one step away. Both are single-pass and need distinct values first.
Checks advanced set-based thinking on sequences such as consecutive days or ranges.
Last updated 23 Nov 2026.