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| Column | Type |
|---|---|
| num | INT |
Examples
Input:
CREATE TABLE numbers (num INT); INSERT INTO numbers VALUES (1), (2), (3), (5), (6);Output:
4Explanation:
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:
1Explanation:
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:
2Explanation:
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