---
title: last aggregate function
description: Learn the syntax of the last function of the SQL language in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/functions/last
---

# `last` aggregate 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 last value of `expr` for the group of rows. The function is a synonym for [`last_value` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/last_value.md).

For the `last` navigation function used in MATCH\_RECOGNIZE, see [`last` function for MATCH\_RECOGNIZE](https://docs.databricks.com/aws/en/sql/language-manual/functions/match_recognize_last.md).

## Syntax

```
last(expr [, ignoreNull] ) [FILTER ( WHERE cond ) ] [ IGNORE NULLS | RESPECT NULLS ]
```

This function can also be invoked as a [window function](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions.md) using the `OVER` clause.

## Arguments

* `expr`: An expression of any type.
* `ignoreNull`: An optional `BOOLEAN` literal that defaults to false.
* `cond`: An optional boolean expression filtering the rows used for aggregation.
* `IGNORE NULLS` or `RESPECT NULLS`: When `IGNORE NULLS` is used or `ignoreNull` is `true` any `expr` value that is NULL is ignored. The default is `RESPECT NULLS`.

## Returns

The result type matches `expr`.

This function is non-deterministic.

## Examples

```sql
> SELECT last(col) FROM VALUES (10), (5), (20) AS tab(col);
 20

> SELECT last(col) FROM VALUES (10), (5), (NULL) AS tab(col);
 NULL

> SELECT last(col, true) FROM VALUES (10), (5), (NULL) AS tab(col);
 5

> SELECT last(col) IGNORE NULLS FROM VALUES (10), (5), (NULL) AS tab(col);
 5

> SELECT last(col) FILTER (WHERE col > 5) FROM VALUES (5), (20) AS tab(col);
 20
```

## Related

* [`last_value` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/last_value.md)
* [`first` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/first.md)
* [`first_value` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/first_value.md)
* [`last` function for MATCH\_RECOGNIZE](https://docs.databricks.com/aws/en/sql/language-manual/functions/match_recognize_last.md)
* [Window functions](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions.md)
