Find gaps in order ids
SQL
Medium
2 views
Problem Description
Assume order_id should be continuous. Using a helper Numbers(n) table, list the missing order_id between MIN(order_id) and MAX(order_id).
Sample Test Case
Input:
Orders(order_id), Numbers(n) 1..N
Constraints
Think: generate range then remove existing ids
Official Solution
SELECT n.n AS missing_order_id FROM Numbers n WHERE n.n BETWEEN (SELECT MIN(order_id) FROM Orders) AND (SELECT MAX(order_id) FROM Orders) AND NOT EXISTS (SELECT 1 FROM Orders o WHERE o.order_id = n.n);
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!