Bird
0
0

Why does this query fail to find gaps correctly?

medium📝 Debug Q7 of 15
SQL - Advanced Query Patterns
Why does this query fail to find gaps correctly?
SELECT num + 1 AS missing FROM seq WHERE num + 1 <> LEAD(num);
ALEAD() cannot be used in SELECT clause
Bnum + 1 cannot be used in WHERE clause
CMissing GROUP BY causes error
DLEAD() requires an OVER() clause with ORDER BY
Step-by-Step Solution
Solution:
  1. Step 1: Check LEAD() usage

    LEAD() is a window function and must have an OVER() clause specifying order.
  2. Step 2: Identify missing OVER() clause

    Without OVER(ORDER BY ...), LEAD() causes syntax error or wrong results.
  3. Final Answer:

    LEAD() requires an OVER() clause with ORDER BY -> Option D
  4. Quick Check:

    Window functions need OVER() [OK]
Quick Trick: Always use OVER() with LEAD() [OK]
Common Mistakes:
  • Omitting OVER() clause
  • Misplacing LEAD() in WHERE
  • Confusing aggregate and window functions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes