---
title: nth_value analytic window function
description: Learn the syntax of the nth\_value window function of the SQL language in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/functions/nth_value
---

# `nth_value` analytic window function

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime

Returns the value at a specific `offset` in the window.

## Syntax

```
nth_value(expr, offset) [ IGNORE NULLS | RESPECT NULLS ] OVER clause
```

## Arguments

* `expr`: An expression of any type.
* `offset`: An INTEGER literal greater than 0.
* `IGNORE NULLS` or `RESPECT NULLS`: When `IGNORE NULLS` is used, any `expr` value that is NULL is ignored in the count. The default is `RESPECT NULLS`.
* [OVER clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions.md): The clause describing the windowing. See: [Window functions](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions.md).

## Returns

The result type matches the `expr` type.

The window function returns the value of `expr` at the row that is the `offset`th row from the beginning of the window frame.

If there is no such `offset`th row, returns `NULL`.

You must use the [ORDER BY clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-orderby.md) clause with this function.
If the order is non-unique, the result is non-deterministic.

## Examples

```sql
> SELECT a, b, nth_value(b, 2) OVER (PARTITION BY a ORDER BY b)
    FROM VALUES ('A1', 2), ('A1', 1), ('A2', 3), ('A1', 1) tab(a, b);
 A1 1   1
 A1 1   1
 A1 2   1
 A2 3   NULL
```

## Related functions

* [`lag` analytic window function](https://docs.databricks.com/aws/en/sql/language-manual/functions/lag.md)
* [`lead` analytic window function](https://docs.databricks.com/aws/en/sql/language-manual/functions/lead.md)
* [`first` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/first.md)
* [`last` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/last.md)
* [Window functions](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions.md)
