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?
CREATE TABLE Invoices (InvoiceNumber INT); INSERT INTO Invoices VALUES (1001), (1002), (1004), (1005), (1007);
Think about checking the next number after each existing invoice to see if it is missing.
Option D checks for each invoice number if the next number is missing by adding 1 and verifying it is not in the table. This correctly finds missing numbers like 1003 and 1006.
Which of the following SQL queries will cause a syntax error when trying to find missing IDs in a sequence?
CREATE TABLE Items (ID INT); INSERT INTO Items VALUES (1), (2), (4), (5);
Check the use of NOT EXISTS and NOT IN clauses.
Option B uses NOT EXISTS incorrectly with a value expression; it requires a subquery. This causes a syntax error.
You have a large table Orders with millions of sequential OrderID values. Which query is the most efficient to find missing OrderID values?
Consider join operations versus subqueries for performance on large data.
Option A uses a LEFT JOIN to find missing IDs efficiently by checking for absence in the join, which is generally faster than subqueries or generate_series on large tables.
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?
Think about what ID + 1 NOT IN (SELECT ID) means logically.
The query checks for each existing ID if the next consecutive number is missing by confirming it is not in the table, thus identifying gaps.
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
Consider how the query checks for missing numbers and what happens at the sequence boundaries.
The query only checks for missing numbers immediately after existing seq values. It cannot detect gaps before the minimum or after the maximum sequence number, so some gaps may be missed.