Bird
0
0

Given the table numbers with column num containing values (1, 2, 4, 5, 7), what will the following query return?

medium📝 query result Q13 of 15
SQL - Advanced Query Patterns
Given the table numbers with column num containing values (1, 2, 4, 5, 7), what will the following query return?
SELECT num, LEAD(num) OVER (ORDER BY num) AS next_num FROM numbers;
A[{num:1, next_num:2}, {num:2, next_num:4}, {num:4, next_num:5}, {num:5, next_num:7}, {num:7, next_num:null}]
B[{num:1, next_num:1}, {num:2, next_num:2}, {num:4, next_num:4}, {num:5, next_num:5}, {num:7, next_num:7}]
C[{num:1, next_num:3}, {num:2, next_num:4}, {num:4, next_num:6}, {num:5, next_num:7}, {num:7, next_num:null}]
D[{num:1, next_num:null}, {num:2, next_num:null}, {num:4, next_num:null}, {num:5, next_num:null}, {num:7, next_num:null}]
Step-by-Step Solution
Solution:
  1. Step 1: Understand LEAD() output

    LEAD(num) returns the next row's num value ordered by num. So each row shows its own num and the next number.
  2. Step 2: Apply to given data

    For values 1,2,4,5,7:
    1's next is 2
    2's next is 4
    4's next is 5
    5's next is 7
    7 has no next, so null
  3. Final Answer:

    [{num:1, next_num:2}, {num:2, next_num:4}, {num:4, next_num:5}, {num:5, next_num:7}, {num:7, next_num:null}] -> Option A
  4. Quick Check:

    LEAD() shifts values down by one [OK]
Quick Trick: LEAD() shows next row's value or null if none [OK]
Common Mistakes:
  • Assuming LEAD() repeats current value
  • Expecting gaps filled automatically
  • Confusing LEAD() with LAG()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes