---
title: "ROWNUM, INST_NUM, ORDERBY_NUM and GROUPBY_NUM"
description: "Number result rows in CoraDB with ROWNUM, INST_NUM, ORDERBY_NUM and GROUPBY_NUM, when each is evaluated, and LIMIT."
url: "https://www.coradb.ai/manual/sql/function/rownum_fn.html"
markdown_url: "https://www.coradb.ai/manual/markdown/sql/function/rownum_fn.md"
product: "CoraDB"
version: "1.0"
keywords: "rownum, inst_num, orderby_num, groupby_num, row number, top n query, limit, pagination, coradb"
---

# ROWNUM, INST_NUM, ORDERBY_NUM and GROUPBY_NUM

These functions number the rows of a query result. They differ in **when** the numbering happens, which decides what a condition on them selects:

| Function | Numbers the rows | Use in |
|---|---|---|
| `ROWNUM`, `INST_NUM()` | As they are read, **before** `GROUP BY` and `ORDER BY` | `SELECT` list, `WHERE` |
| `ORDERBY_NUM()` | **after** `ORDER BY` | `SELECT` list, `FOR ORDERBY_NUM() ...` after `ORDER BY` |
| `GROUPBY_NUM()` | **after** `GROUP BY` | `SELECT` list, `HAVING` |

The numbers start at 1. **Return type:** BIGINT.

In most cases [LIMIT](https://www.coradb.ai/manual/sql/query/select.html#limit-clause) is simpler, and the window function [ROW_NUMBER()](https://www.coradb.ai/manual/sql/function/analysis_fn.html#row_number) gives full control over numbering. The functions on this page remain useful for compatibility and for conditions on the row number.

> **Try it yourself:**
> The examples run on the [e-commerce sample database](https://www.coradb.ai/manual/sample-database.html).



## ROWNUM, INST_NUM

```text
ROWNUM
INST_NUM()
```

`ROWNUM` (a pseudocolumn) and `INST_NUM()` number rows in the order they are produced, before any sorting. A condition on `ROWNUM` in the `WHERE` clause therefore stops reading after the given number of rows.

To take the first rows of a **sorted** result, sort in a subquery and number in the outer query. The four orders with the highest shipping cost:

```sql
SELECT * FROM (
    SELECT id, shipping_cost FROM orders ORDER BY shipping_cost DESC, id
) AS t
WHERE ROWNUM < 5;
```

| id | shipping_cost |
|---:|---:|
| 2136 | 59.99 |
| 2879 | 59.99 |
| 1080 | 58.00 |
| 1982 | 56.96 |

`LIMIT` returns the same rows more simply: `SELECT id, shipping_cost FROM orders ORDER BY shipping_cost DESC, id LIMIT 4;`.

A `ROWNUM` condition in the same query as the `ORDER BY` is applied **before** sorting: it picks the first four rows read, then sorts only those.

```sql
SELECT ROWNUM, id, shipping_cost FROM orders
WHERE ROWNUM < 5
ORDER BY shipping_cost DESC;
```

| rownum | id | shipping_cost |
|---:|---:|---:|
| 4 | 3 | 23.41 |
| 3 | 2 | 12.74 |
| 2 | 1 | 9.19 |
| 1 | 0 | 0.00 |

Notes:

- `ROWNUM` belongs to the `SELECT` in which it appears; in a subquery it numbers the subquery's rows.
- `ROWNUM` cannot be compared with a column or a correlated subquery.
- In `INSERT ... SELECT`, `UPDATE` and `DELETE`, `ROWNUM` in the `WHERE` clause limits the number of rows processed.

## ORDERBY_NUM

```text
ORDERBY_NUM()
SELECT ... ORDER BY ... FOR ORDERBY_NUM() <condition>
```

`ORDERBY_NUM()` numbers the rows after sorting. In the `FOR` clause after `ORDER BY`, it selects a range of the sorted result. The orders ranked 3rd to 5th by shipping cost:

```sql
SELECT ORDERBY_NUM(), id, shipping_cost FROM orders
ORDER BY shipping_cost DESC, id
FOR ORDERBY_NUM() BETWEEN 3 AND 5;
```

| orderby_num() | id | shipping_cost |
|---:|---:|---:|
| 3 | 1080 | 58.00 |
| 4 | 1982 | 56.96 |
| 5 | 2211 | 56.87 |

The same rows with `LIMIT offset, count`: `... ORDER BY shipping_cost DESC, id LIMIT 2, 3;`.

## GROUPBY_NUM

```text
GROUPBY_NUM()
SELECT ... GROUP BY ... HAVING GROUPBY_NUM() <condition>
```

`GROUPBY_NUM()` numbers the groups after grouping. In `HAVING`, it selects some of the groups:

```sql
SELECT GROUPBY_NUM(), customer_id, COUNT(*) FROM orders
GROUP BY customer_id
HAVING GROUPBY_NUM() BETWEEN 1 AND 3;
```

| groupby_num() | customer_id | count(*) |
|---:|---:|---:|
| 1 | 4 | 3 |
| 2 | 5 | 2 |
| 3 | 6 | 8 |

A `ROWNUM` condition, in contrast, limits the rows **before** they are grouped. Only the first five orders read are counted here:

```sql
SELECT customer_id, COUNT(*) FROM orders
WHERE ROWNUM BETWEEN 1 AND 5
GROUP BY customer_id;
```

| customer_id | count(*) |
|---:|---:|
| 4 | 3 |
| 5 | 2 |
