---
isPrivate: true
title: DDSQL Window Functions (Preview)
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: Docs > DDSQL Reference > DDSQL Window Functions (Preview)
---

> For the complete documentation index, see [llms.txt](https://docs.datadoghq.com/llms.txt).

# DDSQL Window Functions (Preview)

## Overview{% #overview %}

A window function applies an aggregation to some subset of the rows selected by a query. The selected rows are preserved in the query output, rather than being grouped into a single output row as they would be in a non-window aggregation.

For details on how window functions work, see the [Postgres documentation for Window Function](https://www.postgresql.org/docs/current/sql-expressions.html#SYNTAX-WINDOW-FUNCTIONS).

## Syntax{% #syntax %}

```sql
function_name ([expression [, expression ...]]) OVER (
  [ PARTITION BY expression [, ...] ]
  [ ORDER BY expression [ ASC | DESC ] [ NULLS { FIRST | LAST } ] [, ...] ]
  [ frame_clause ]
)
```

The optional `frame_clause` has the following syntax:

```sql
{ RANGE | ROWS } frame_start
| { RANGE | ROWS } BETWEEN frame_start AND frame_end
```

The `frame_start` and `frame_end` expressions can be one of the following:

- `UNBOUNDED PRECEDING`
- `offset PRECEDING`
- `CURRENT ROW`
- `offset FOLLOWING`
- `UNBOUNDED FOLLOWING`

## Functions{% #functions %}

The functions below can be used in windows, along with the [aggregation functions](https://docs.datadoghq.com/ddsql_reference/ddsql_preview/functions.md#aggregation-functions).

### row_number{% #row_number %}

| Name         | Return type | Description                                                                  |
| ------------ | ----------- | ---------------------------------------------------------------------------- |
| row_number() | integer     | Returns the number of the current row within its partition, counting from 1. |

### rank{% #rank %}

| Name   | Return type | Description                                                                                           |
| ------ | ----------- | ----------------------------------------------------------------------------------------------------- |
| rank() | integer     | Returns the rank of the current row, with gaps (the `row_number` of the first row in its peer group). |

### dense_rank{% #dense_rank %}

| Name         | Return type | Description                                                                                      |
| ------------ | ----------- | ------------------------------------------------------------------------------------------------ |
| dense_rank() | integer     | Returns the rank of the current row, without gaps. This function effectively counts peer groups. |

### first_value{% #first_value %}

| Name                   | Return type | Description                                                                       |
| ---------------------- | ----------- | --------------------------------------------------------------------------------- |
| first_value(value *T*) | *T*         | Returns the value evaluated at the row that is the first row of the window frame. |

### last_value{% #last_value %}

| Name                  | Return type | Description                                                                      |
| --------------------- | ----------- | -------------------------------------------------------------------------------- |
| last_value(value *T*) | *T*         | Returns the value evaluated at the row that is the last row of the window frame. |
