§ Oracle Compatibility - Syntax - ROWNUM


§ 1. Syntax

ROWNUM is a pseudocolumn, mainly used to implement pagination, similar to the LIMIT usage. It is used as follows:

greatsql> SELECT * FROM employees WHERE ROWNUM < 11;

greatsql> SELECT * FROM (SELECT ROWNUM rn, x FROM t1) t1 WHERE t1.rn BETWEEN 3 AND 5;
1
2
3

§ 2. Semantic Description

greatsql> SELECT * FROM employees WHERE ROWNUM > 1;
1

Explanation: for the first row retrieved, its rownum number is 1, and where rownum > 1 does not satisfy the condition (1>1 is false), so it is skipped; for the second row, its number is still 1, and it still does not satisfy the condition (rownum >1 is false), so the query result is always empty.

greatsql> SELECT * FROM t1;
+------+------+
| ida  | age  |
+------+------+
|    1 |    2 |
|    2 |    3 |
|    3 |    0 |
+------+------+

greatsql> SELECT rownum, ida FROM t1 ORDER BY age;
+--------+------+
| rownum | ida  |
+--------+------+
|      3 |    3 |
|      1 |    1 |
|      2 |    2 |
+--------+------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Explanation: because the ORDER BY operation is completed in the last step, it may affect the final display order of rownum.

§ 2.1 Restrictions on rownum Values

Assuming that in the WHERE condition, the value set for rownum is N, the requirements for N are:

  1. The condition value N must be a positive number greater than or equal to 1, otherwise the query result is an empty set.
  2. The condition rownum > N returns an empty set. Because for the first row retrieved, rownum is numbered 1, which does not meet the condition (rownum > N), so this row is discarded. The second row is still numbered 1 and still does not meet the condition, and so on, the result set is empty. The example above has demonstrated this.
  3. When the condition is rownum <= 1, only the first row can be returned.
  4. When the condition is rownum >= 1, all records are returned.

§ 2.2 Execution Order Description

greatsql> SELECT * FROM t1 WHERE c1=? AND c2=? AND rownum <= N;
1

The above SQL statement contains multiple filter conditions, and the rownum filter condition is evaluated last.

§ 3. Other Usage Restrictions

§ 3.1 Different Support for JOIN

In outer join syntax, rownum is not supported as a join key, while inner join supports it. This is because using rownum in an outer join leads to ambiguous semantics.

greatsql> SELECT * FROM t1 LEFT JOIN t2 ON t1.ida = rownum;
ERROR 1235 (42000): This version of MySQL doesn't yet support 'ROWNUM occur in outer JOIN ON conditions.'

greatsql> SELECT * FROM t1 JOIN t2 ON t1.ida = rownum;
+------+------+----+------------+-------+---------------------+
| ida  | age  | id | name       | other | created             |
+------+------+----+------------+-------+---------------------+
|    1 |    2 |  5 | Fifth Name | 55555 | 2004-04-04 04:04:04 |
|    2 |    3 |  5 | Fifth Name | 55555 | 2004-04-04 04:04:04 |
|    3 |    0 |  5 | Fifth Name | 55555 | 2004-04-04 04:04:04 |
+------+------+----+------------+-------+---------------------+
1
2
3
4
5
6
7
8
9
10
11

§ 3.2 Restrictions on Subqueries

  1. Using rownum in a subquery after FROM is supported.
  2. Using rownum in a non-correlated scalar subquery is supported. For in/any/all/some subqueries, whether correlated or not, using rownum is not supported. The main reason is that during nested loop JOIN, there are execution order issues that may lead to different results. In this case, an error is reported as follows:
ERROR 1235 (42000): This version of MySQL doesn't yet support 'ROWNUM & IN/ALL/ANY/SOME subquery';
1

§ 3.3 Query Results May Differ from Oracle

  1. Because the data output order is inconsistent, performing rownum filtering on top of it may cause the final displayed results to differ.

  2. Inconsistent optimizer behavior may lead to different output results. For example, the following cases:

-- The results of this SQL are the same in GreatSQL and Oracle
greatsql> SELECT * FROM t1 LEFT JOIN t2 ON t1.r1=t2.r1; 

-- GreatSQL performs rownum<t1.r1 filtering on the JOIN result set according to the original statement
greatsql> SELECT * FROM t1 LEFT JOIN t2 ON t1.r1=t2.r1 WHERE rownum < t1.r1;

-- Oracle derives a rownum < t2.r1 filter based on the JOIN key, which filters out the null rows generated by the t2 table in the final result set
-- This does not conform to the original semantics
greatsql> SELECT * FROM t1 LEFT JOIN t2 ON t1.r1=t2.r1 WHERE rownum < t1.r1;
1
2
3
4
5
6
7
8
9

Scan the QR code to follow our WeChat official account

greatsql-wx