0
0
SQLquery~5 mins

FIRST_VALUE and LAST_VALUE in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the SQL function FIRST_VALUE() do?
It returns the first value in an ordered set of rows within a window or partition.
Click to reveal answer
beginner
What is the purpose of LAST_VALUE() in SQL?
It returns the last value in an ordered set of rows within a window or partition.
Click to reveal answer
intermediate
How do FIRST_VALUE() and LAST_VALUE() differ from aggregate functions like MIN() and MAX()?
FIRST_VALUE() and LAST_VALUE() return values based on row order within a window, while MIN() and MAX() return the smallest or largest value regardless of order.
Click to reveal answer
beginner
In SQL, what clause is required to use FIRST_VALUE() and LAST_VALUE() properly?
The OVER() clause with an ORDER BY to define the window or partition and the order of rows.
Click to reveal answer
advanced
Why might LAST_VALUE() return unexpected results without proper framing?
Because by default the window frame ends at the current row, LAST_VALUE() may return the current row's value instead of the last in the partition unless frame boundaries are set.
Click to reveal answer
What does FIRST_VALUE(column) OVER (ORDER BY date) return?
AThe earliest value of column based on date order
BThe smallest value of column overall
CThe last value of column in the table
DThe average value of column
Which clause is mandatory to use with LAST_VALUE()?
AHAVING
BGROUP BY
CORDER BY inside OVER()
DWHERE
What happens if you use LAST_VALUE() without specifying ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING?
AIt returns the first value
BIt returns NULL
CIt returns the last value in the entire partition
DIt returns the value of the current row
Which function would you use to get the first value in each group of rows?
AFIRST_VALUE()
BMAX()
CMIN()
DCOUNT()
Can FIRST_VALUE() and LAST_VALUE() be used without window functions?
AYes, they work like aggregate functions
BNo, they require OVER() clause
CYes, but only in WHERE clause
DNo, they are not SQL functions
Explain how FIRST_VALUE() and LAST_VALUE() work in SQL and when you would use them.
Think about how you find the first or last item in a sorted list.
You got /4 concepts.
    Describe the importance of window framing when using LAST_VALUE() and how to fix common issues.
    Consider how the window frame limits which rows LAST_VALUE() sees.
    You got /4 concepts.