1. Home
  2. Data Science
  3. SQL for Data Analysis
  4. Why does LAST_VALUE(x) OVER (ORDER BY ts) usually return…

Added 30 Nov -0001

Why does LAST_VALUE(x) OVER (ORDER BY ts) usually return the current row's value rather than the final value in the partition?

  1. The window is implicitly partitioned by ts, so each row is its own group
  2. The function is nondeterministic and may return any row of the whole partition
  3. The default frame ends at the current row, so the last visible value is here Correct answer
  4. LAST_VALUE ignores the frame and evaluates over the whole peer group

Correct answer

C. The default frame ends at the current row, so the last visible value is here

21 views

MCQSEXAM

Explanation

With an ORDER BY in the window, the default frame is RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, so the last row visible to LAST_VALUE is the current one. Adding ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING makes it return the partition's final value. FIRST_VALUE escapes the surprise because its default frame already starts at the beginning of the partition.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More SQL for Data Analysis MCQs

Practise all SQL for Data Analysis MCQs

Similar MCQs