Bird
0
0

Which of the following is the correct syntax to get the last value of a column score ordered by date using LAST_VALUE in PostgreSQL?

easy📝 Syntax Q12 of 15
PostgreSQL - Window Functions in PostgreSQL
Which of the following is the correct syntax to get the last value of a column score ordered by date using LAST_VALUE in PostgreSQL?
ASELECT LAST_VALUE(score) FROM results ORDER BY date;
BSELECT LAST_VALUE(score) OVER (ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM results;
CSELECT LAST_VALUE(score) OVER (PARTITION BY date) FROM results;
DSELECT LAST_VALUE(score) OVER (ORDER BY date) FROM results;
Step-by-Step Solution
Solution:
  1. Step 1: Understand LAST_VALUE default frame

    LAST_VALUE by default looks from current row to end, so it may not return the last value overall without framing.
  2. Step 2: Use explicit window frame to get true last value

    Adding ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ensures the entire partition is considered.
  3. Final Answer:

    SELECT LAST_VALUE(score) OVER (ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM results; -> Option B
  4. Quick Check:

    LAST_VALUE needs full frame to get last value [OK]
Quick Trick: Add full window frame to LAST_VALUE for correct last row [OK]
Common Mistakes:
  • Omitting window frame causing wrong last value
  • Using PARTITION BY incorrectly
  • Using LAST_VALUE without OVER() clause

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes