Skip to content

ROWNUM, INST_NUM, ORDERBY_NUM and GROUPBY_NUM

View as Markdown

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 is simpler, and the window function ROW_NUMBER() gives full control over numbering. The functions on this page remain useful for compatibility and for conditions on the row number.

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:

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.

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()
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:

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()
SELECT ... GROUP BY ... HAVING GROUPBY_NUM() <condition>

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

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:

SELECT customer_id, COUNT(*) FROM orders
WHERE ROWNUM BETWEEN 1 AND 5
GROUP BY customer_id;
customer_id count(*)
4 3
5 2