Search RU
Tasks / SQL / Gaps in invoice numbering
Statement medium

Gaps in invoice numbering

The `invoices` table contains invoice numbers. Find the missing numbers from 1
through the maximum existing number.

Result column: `missing_no`. Sort ascending.

SQL · PostgreSQL
sqlctegapsgenerate_series
Schema
CREATE TABLE invoices (
    id integer PRIMARY KEY,
    invoice_no integer NOT NULL UNIQUE
);
Data
INSERT INTO invoices (id, invoice_no) VALUES
    (1, 1),
    (2, 2),
    (3, 4),
    (4, 5),
    (5, 7);
query.sql PostgreSQL 17
AI mentor

The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.

Log in and ask
Hints 1 of 3 unlocked · attempts: 0
Hint 1

First get the maximum number. Then generate every number from 1 to that maximum
and compare with the existing rows.

Hint 2 — unlocks after 1 attempt
Hint 3 — unlocks after 3 attempts