Description

Write a SQL query to find the first missing number in a sequence starting from 1. **Output columns (in order):** missing_num

Table:numbers
ColumnType
numINT

Examples

Input:CREATE TABLE numbers (num INT); INSERT INTO numbers VALUES (1), (2), (3), (5), (6);
Output:4
Explanation:

The numbers present are 1, 2, 3, 5, 6. The sequence goes 1→2→3 then jumps to 5→6, skipping 4. Therefore, 4 is the first missing number in the sequence.

Input:CREATE TABLE numbers (num INT); INSERT INTO numbers VALUES (2), (3), (4), (5);
Output:1
Explanation:

The sequence is missing the very first number (1). Even though there are consecutive numbers 2,3,4,5, the sequence must start at 1, so 1 is the first missing number.

Input:CREATE TABLE numbers (num INT); INSERT INTO numbers VALUES (1), (3), (4), (6), (7), (8);
Output:2
Explanation:

The sequence has 1, then jumps to 3,4, then jumps to 6,7,8. The first gap occurs after 1 where 2 is missing, making 2 the first missing number.

Constraints

  • Sequence starts at 1

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!