Bird
0
0

What will be the output of this query?

medium📝 query result Q5 of 15
PostgreSQL - Window Functions in PostgreSQL
What will be the output of this query?
SELECT employee_id, salary, LEAD(salary, 2) OVER (ORDER BY employee_id) AS salary_after_two FROM employees;
AShows salary and salary from previous two rows ordered by employee_id
BShows salary and salary from two rows ahead ordered by employee_id
CShows salary and salary from next row only
DCauses an error because offset 2 is invalid
Step-by-Step Solution
Solution:
  1. Step 1: Understand LEAD with offset

    LEAD(salary, 2) returns the salary value two rows ahead in the order defined by employee_id.
  2. Step 2: Analyze output

    Each row shows current salary and salary from two rows after it. If no such row exists, NULL is returned.
  3. Final Answer:

    Shows salary and salary from two rows ahead ordered by employee_id -> Option B
  4. Quick Check:

    LEAD with offset 2 returns value two rows forward [OK]
Quick Trick: LEAD(column, n) gets value n rows ahead [OK]
Common Mistakes:
  • Confusing offset direction
  • Assuming offset 2 invalid
  • Mixing LEAD with LAG
  • Ignoring NULL for missing rows

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes