Learn SQL
-- windows.gaps_islands_sessionization

Gaps & Islands + Sessionization in SQL

Constant-difference blocks; flag-plus-cumulative sessions

--concept

Islands: subtract a row-number sequence from the ordered key — constant differences mark contiguous blocks, GROUP BY the difference. Sessions: LAG timestamps, flag gaps over threshold, cumulative SUM of flags as session ids. Difference-of-sequences is the whiteboard insight; flag-plus-sum is production sessionization for event logs.

--how it works
1

Island key

id − ROW_NUMBER() OVER (ORDER BY id) constant per contiguous block.

2

Group the key

GROUP BY grp with MIN/MAX bounds — gaps vanish into key changes.

3

Flag time gaps

LAG(ts) per user; flag where gap > threshold (30 min).

4

Cumulative sessions

SUM(flag) OVER (PARTITION BY user ORDER BY ts) assigns session ids.

--visualization
id − ROW_NUMBER() constant per block → GROUP BY grp
start_idend_id
13
79
Stage 1 / 1
Sessions: LAG gap flag + cumulative SUM per user — same toolkit, time domain.
--example
query.sql
1-- island bounds:
2SELECT MIN(id) AS start_id, MAX(id) AS end_id FROM (SELECT id, id - ROW_NUMBER() OVER (ORDER BY id) AS grp FROM tickets) t GROUP BY grp;
Result
start_idend_id
13
79

IDs 1,2,3 share one difference (block one); 7,8,9 share another. Gaps 4-6 vanish into the grouping key. Same LAG+difference toolkit sessionizes clickstreams by time gaps.

--variants

Ticket blocks 1-3, 7-9

Two islands from one difference.

query.sql
1SELECT MIN(id) AS s, MAX(id) AS e FROM (SELECT id, id - ROW_NUMBER() OVER (ORDER BY id) AS grp FROM tickets) t GROUP BY grp;
Result
start_idend_id
13
79
1Rows 1,2,3 share grp K; 7,8,9 share K′.
2Gap 4-6 changes the difference — new group.
3Bounds emerge from GROUP BY the derived key.

30-min sessions

Flag + running sum sessionizes.

query.sql
1-- flag gaps, then:
2-- SUM(flag) OVER (PARTITION BY user_id ORDER BY ts) AS session_id
Result
user_idsessions
user_72
1LAG fetches prior event per user.
2Large gaps raise flag 1, else 0.
3Cumulative SUM numbers sessions in order.
--key facts
Islandsid − row_number constant per block
Sessionsflag gaps + cumulative SUM
Boundaryshallow in SQL; deep graphs elsewhere
--use cases
Ranges: tickets, sequences, date coverage blocks
Product sessions: clickstreams, prior-status chains
--under the hood
Difference trick is one sorted pass; LAG/session needs one window sort per partition key — index (user, ts).
Deep graph paths need recursive path arrays + cycle guards, not islands.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.