Complete the code to select all IDs from the table.
SELECT [1] FROM sequence_table;The column id holds the sequence numbers we want to check.
Complete the code to find the next ID after the current one using LEAD function.
SELECT id, [1] OVER (ORDER BY id) AS next_id FROM sequence_table;LAG which looks backward.MAX which do not return next row.The LEAD function returns the next row's value in the ordered sequence.
Fix the error in the code to find gaps by comparing current and next IDs.
SELECT id FROM (SELECT id, LEAD(id) OVER (ORDER BY id) AS next_id FROM sequence_table) sub WHERE next_id - id [1] 1;
We want to find where the gap is bigger than 1, so next_id - id > 1.
Fill both blanks to generate missing IDs between gaps.
SELECT id + [1] AS missing_id FROM (SELECT id, LEAD(id) OVER (ORDER BY id) AS next_id FROM sequence_table) sub, generate_series(1, [2]) gs WHERE next_id - id > 1;
We add the series number starting at 1 up to the gap size minus 1 to find missing IDs.
Fill all three blanks to list all missing IDs in the sequence.
SELECT id + [1] AS missing_id FROM (SELECT id, LEAD(id) OVER (ORDER BY id) AS next_id FROM sequence_table) sub, generate_series([2], [3]) gs WHERE next_id - id > 1;
We generate the series from 1 to next_id - id - 1 and add gs to id to get the missing IDs: id + 1, id + 2, ..., next_id - 1.
