Bird
0
0

Which of the following SQL snippets correctly uses LEAD() to find gaps in a sequence column named num in table numbers?

easy📝 Syntax Q12 of 15
SQL - Advanced Query Patterns
Which of the following SQL snippets correctly uses LEAD() to find gaps in a sequence column named num in table numbers?
ASELECT num, LEAD(num) AS next_num FROM numbers;
BSELECT num, LEAD(num) FROM numbers ORDER BY num;
CSELECT num, LEAD(num) OVER (ORDER BY num) AS next_num FROM numbers;
DSELECT num, LEAD(num) OVER () AS next_num FROM numbers;
Step-by-Step Solution
Solution:
  1. Step 1: Check LEAD() syntax

    The correct syntax requires LEAD(column) OVER (ORDER BY column) to define the order for the window function.
  2. Step 2: Identify the correct option

    SELECT num, LEAD(num) OVER (ORDER BY num) AS next_num FROM numbers; uses LEAD(num) OVER (ORDER BY num), which is the correct form to get the next number in order.
  3. Final Answer:

    SELECT num, LEAD(num) OVER (ORDER BY num) AS next_num FROM numbers; -> Option C
  4. Quick Check:

    LEAD() needs OVER with ORDER BY [OK]
Quick Trick: Always include OVER(ORDER BY column) with LEAD() [OK]
Common Mistakes:
  • Omitting OVER clause
  • Not specifying ORDER BY inside OVER
  • Using LEAD() without alias

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes