Bird
0
0

Why does this query produce incorrect last values?

medium📝 Debug Q7 of 15
SQL - Advanced Window Functions
Why does this query produce incorrect last values?
SELECT department, salary, LAST_VALUE(salary) OVER (PARTITION BY department ORDER BY salary) FROM employees;
ABecause ORDER BY must be DESC for LAST_VALUE
BBecause PARTITION BY is not allowed with LAST_VALUE
CBecause the default frame ends at current row, not the last row
DBecause salary is not aggregated
Step-by-Step Solution
Solution:
  1. Step 1: Understand default frame behavior

    LAST_VALUE defaults to frame from unbounded preceding to current row, so it returns current row's salary.
  2. Step 2: Why this causes incorrect last values

    To get the actual last salary in partition, frame must be extended to unbounded following.
  3. Final Answer:

    Because the default frame ends at current row, not the last row -> Option C
  4. Quick Check:

    LAST_VALUE default frame excludes rows after current [OK]
Quick Trick: Use ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING for full frame [OK]
Common Mistakes:
  • Assuming default frame covers all rows
  • Believing PARTITION BY disallows LAST_VALUE
  • Thinking ORDER BY DESC is required
  • Confusing aggregation with window functions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes