# ROW\_NUMBER

Returns the sequential number of the current row within its partition, counting from 1.

`ROW_NUMBER()` is commonly used to rank rows after sorting them inside each partition. For deterministic results, specify an `ORDER BY` clause in the window definition.

## Signature

```sql
ROW_NUMBER()
```

## Usage Notes

* `ROW_NUMBER()` takes no arguments.
* Numbering restarts for each `PARTITION BY` group.
* The row order is determined by the window's `ORDER BY` clause.
* No explicit window frame clause can be specified for `ROW_NUMBER()`.

## Examples

Assign a row number to each salesperson by year-to-date sales.

```sql
SELECT
    ROW_NUMBER() OVER (ORDER BY SalesYTD DESC) AS Row,
    FirstName,
    LastName,
    SalesYTD AS "Total sales YTD"
FROM Sales.vSalesPerson;
```

Output:

| Row | FirstName | LastName | Total sales YTD |
| --- | --------- | -------- | --------------- |
| 1   | Joe       | Smith    | 2251368.34      |
| 2   | Alice     | Davis    | 2151341.64      |
| 3   | James     | Jones    | 1551363.54      |
| 4   | Dane      | Scott    | 1251358.72      |

Assign a row number within each customer partition, ordered by payment time.

```sql
SELECT
    customer_id,
    payment_date,
    amount,
    ROW_NUMBER() OVER (
        PARTITION BY customer_id
        ORDER BY payment_date
    ) AS payment_number
FROM payment;
```

Output:

| customer\_id | payment\_date              | amount | payment\_number |
| ------------ | -------------------------- | ------ | --------------- |
| 1            | 2023-02-14 23:22:38.996577 | 5.99   | 1               |
| 1            | 2023-02-15 16:31:19.996577 | 0.99   | 2               |
| 1            | 2023-02-15 19:37:12.996577 | 9.99   | 3               |
| 2            | 2023-02-17 19:23:24.996577 | 2.99   | 1               |
| 2            | 2023-02-17 19:24:10.996577 | 0.99   | 2               |

## Related Pages

* [Window Functions](/functions/window.md)
* [Function Index](/functions/function-index.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.pinot.apache.org/functions/window/row_number.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
