0
0
SQLquery~20 mins

Finding gaps in sequences in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sequence Gap Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find missing invoice numbers in a sequence

Given a table Invoices with a column InvoiceNumber containing sequential invoice numbers, which query correctly finds all missing invoice numbers between the minimum and maximum?

SQL
CREATE TABLE Invoices (InvoiceNumber INT);
INSERT INTO Invoices VALUES (1001), (1002), (1004), (1005), (1007);
ASELECT InvoiceNumber AS MissingNumber FROM Invoices WHERE InvoiceNumber NOT IN (SELECT InvoiceNumber + 1 FROM Invoices);
BSELECT InvoiceNumber - 1 AS MissingNumber FROM Invoices WHERE InvoiceNumber - 1 NOT IN (SELECT InvoiceNumber FROM Invoices);
CSELECT InvoiceNumber + 2 AS MissingNumber FROM Invoices WHERE InvoiceNumber + 2 NOT IN (SELECT InvoiceNumber FROM Invoices);
DSELECT InvoiceNumber + 1 AS MissingNumber FROM Invoices WHERE InvoiceNumber + 1 NOT IN (SELECT InvoiceNumber FROM Invoices);
Attempts:
2 left
💡 Hint

Think about checking the next number after each existing invoice to see if it is missing.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in gap detection query

Which of the following SQL queries will cause a syntax error when trying to find missing IDs in a sequence?

SQL
CREATE TABLE Items (ID INT);
INSERT INTO Items VALUES (1), (2), (4), (5);
ASELECT ID + 1 AS MissingID FROM Items WHERE ID + 1 NOT IN (SELECT ID FROM Items);
BSELECT ID + 1 AS MissingID FROM Items WHERE ID + 1 NOT EXISTS (SELECT ID FROM Items);
CSELECT ID + 1 AS MissingID FROM Items WHERE NOT EXISTS (SELECT 1 FROM Items WHERE ID = Items.ID + 1);
D;)smetI MORF DI TCELES( NI TON 1 + DI EREHW smetI MORF DIgnissiM SA 1 + DI TCELES
Attempts:
2 left
💡 Hint

Check the use of NOT EXISTS and NOT IN clauses.

optimization
advanced
2:00remaining
Optimize gap detection for large sequences

You have a large table Orders with millions of sequential OrderID values. Which query is the most efficient to find missing OrderID values?

ASELECT o1.OrderID + 1 AS MissingID FROM Orders o1 LEFT JOIN Orders o2 ON o1.OrderID + 1 = o2.OrderID WHERE o2.OrderID IS NULL;
B
WITH Numbers AS (SELECT generate_series(MIN(OrderID), MAX(OrderID)) AS num FROM Orders)
SELECT num AS MissingID FROM Numbers WHERE num NOT IN (SELECT OrderID FROM Orders);
CSELECT OrderID + 1 AS MissingID FROM Orders WHERE OrderID + 1 NOT IN (SELECT OrderID FROM Orders);
DSELECT DISTINCT OrderID FROM Orders WHERE OrderID NOT IN (SELECT OrderID + 1 FROM Orders);
Attempts:
2 left
💡 Hint

Consider join operations versus subqueries for performance on large data.

🧠 Conceptual
advanced
2:00remaining
Understanding gap detection logic

Why does the query SELECT ID + 1 FROM Table WHERE ID + 1 NOT IN (SELECT ID FROM Table) correctly find missing numbers in a sequence?

ABecause it checks for each ID if the next number is missing by verifying it is not present in the table.
BBecause it selects all IDs and adds 1 to them regardless of presence in the table.
CBecause it finds IDs that are duplicated in the table.
DBecause it returns all IDs that are present in the table plus one.
Attempts:
2 left
💡 Hint

Think about what ID + 1 NOT IN (SELECT ID) means logically.

🔧 Debug
expert
3:00remaining
Debugging incorrect gap detection query

The following query is intended to find missing sequence numbers in SeqTable but returns no results even though gaps exist. What is the reason?

SELECT seq + 1 AS MissingSeq FROM SeqTable WHERE seq + 1 NOT IN (SELECT seq FROM SeqTable);

Table data: seq = 1, 2, 4, 5

AThe subquery uses the wrong column name causing no matches.
BThe query syntax is invalid and causes an error.
CThe query misses the last gap because it only checks seq + 1 for existing seq values, so it cannot find gaps after the max value.
DThe query should use <code>NOT EXISTS</code> instead of <code>NOT IN</code> to work correctly.
Attempts:
2 left
💡 Hint

Consider how the query checks for missing numbers and what happens at the sequence boundaries.