last aggregate function

Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime

Returns the last value of expr for the group of rows.


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

This function can also be invoked as a window function using the OVER clause.


  • expr: An expression of any type.

  • ignoreNull: An optional BOOLEAN literal defaulting 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.


The result type matches expr.

The function is a synonym for last_value aggregate function.

This function is non-deterministic.


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

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

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