SQLite NTH_VALUE() Function
Example
Show the second most expensive product on every row:
SELECT ProductName, Price,
NTH_VALUE(ProductName, 2) OVER (
ORDER BY Price DESC
ROWS BETWEEN UNBOUNDED PRECEDING
AND UNBOUNDED FOLLOWING) AS Second
FROM Products;
Try it Yourself »
Definition and Usage
The NTH_VALUE() function returns the value from the nth row in the window frame. It returns NULL if the frame has fewer rows.
Note: Window functions need an OVER clause. Read more in the SQLite Window Functions chapter.
Tip: Also look at FIRST_VALUE(), LAST_VALUE().
Syntax
NTH_VALUE(expression, n) OVER (ORDER BY column frame)
Parameter Values
| Parameter | Description |
|---|---|
| expression | Required. The column or calculation to return |
| n | Required. The row number in the frame, starting with 1 |
Technical Details
| Return type: | Same as the input |
|---|---|
| Works in: | SQLite 3.25.0 and later |