Bird
0
0

Identify the error in this query:

medium📝 Debug Q14 of 15
SQL - Advanced Window Functions
Identify the error in this query:
SELECT employee_id, LAST_VALUE(salary) OVER (ORDER BY employee_id) AS last_sal FROM employees;
ALAST_VALUE needs a frame clause to get correct last value
BMissing PARTITION BY clause causes error
CORDER BY cannot be used inside OVER()
Dsalary column must be aggregated with GROUP BY
Step-by-Step Solution
Solution:
  1. Step 1: Check LAST_VALUE default behavior

    By default, LAST_VALUE looks only up to current row, so it may not return the true last value.
  2. Step 2: Add frame clause to fix

    Adding ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING makes LAST_VALUE consider all rows.
  3. Final Answer:

    LAST_VALUE needs a frame clause to get correct last value -> Option A
  4. Quick Check:

    LAST_VALUE without frame returns wrong last row [OK]
Quick Trick: Add full frame to LAST_VALUE for correct last row [OK]
Common Mistakes:
  • Thinking PARTITION BY is mandatory
  • Believing ORDER BY is invalid inside OVER()
  • Confusing LAST_VALUE with aggregate functions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes