today:
1,181
yesterday:
854
Total:
1,007,372

SQL ROWNUM

admin 2016.05.19 15:43 Views : 240

ROWNUM

For each row returned by a query, the ROWNUM pseudocolumn returns a number indicating the order in which Oracle selects the row from a table or set of joined rows. The first row selected has a ROWNUM of 1, the second has 2, and so on.

You can use ROWNUM to limit the number of rows returned by a query, as in this example:

SELECT * FROM employees WHERE ROWNUM < 10;

If an ORDER BY clause follows ROWNUM in the same query, then the rows will be reordered by the ORDER BY clause. The results can vary depending on the way the rows are accessed. For example, if the ORDER BY clause causes Oracle to use an index to access the data, then Oracle may retrieve the rows in a different order than without the index. Therefore, the following statement will not have the same effect as the preceding example:

SELECT * FROM employees WHERE ROWNUM < 11 ORDER BY last_name;

If you embed the ORDER BY clause in a subquery and place the ROWNUM condition in the top-level query, then you can force the ROWNUM condition to be applied after the ordering of the rows. For example, the following query returns the employees with the 10 smallest employee numbers. This is sometimes referred to as top-N reporting:

SELECT * FROM
   (SELECT * FROM employees ORDER BY employee_id)
   WHERE ROWNUM < 11;

In the preceding example, the ROWNUM values are those of the top-level SELECT statement, so they are generated after the rows have already been ordered by employee_id in the subquery.

Conditions testing for ROWNUM values greater than a positive integer are always false. For example, this query returns no rows:

SELECT * FROM employees
    WHERE ROWNUM > 1;

The first row fetched is assigned a ROWNUM of 1 and makes the condition false. The second row to be fetched is now the first row and is also assigned a ROWNUM of 1 and makes the condition false. All rows subsequently fail to satisfy the condition, so no rows are returned.

You can also use ROWNUM to assign unique values to each row of a table, as in this example:

UPDATE my_table
    SET column1 = ROWNUM;
No. Subject Author Date Views
126 ACTION admin 2019.01.11 6458
125 Fundamentals of PL/SQL admin 2016.05.10 2039
124 QR Code plugin admin 2017.11.30 1588
123 QR Code plugin Process admin 2017.11.30 964
122 How to Update Oracle XE Version admin 2018.02.23 931
121 GP PRINT admin 2018.05.31 920
120 INSTR admin 2016.05.19 789
119 SELECT last_name FROM admin 2018.12.12 744
118 IMP admin 2018.10.08 723
117 TREE admin 2016.05.18 722
116 JK admin 2019.02.05 702
115 CREATE TABLE admin 2018.09.25 471
114 APEX Password Change admin 2016.08.26 462
113 Membership Management admin 2016.06.09 435
112 JavaScript for Member List admin 2016.08.17 433
111 QRCODE admin 2020.05.25 405
110 control break admin 2016.06.25 398
109 SQL admin 2016.07.15 397
108 DATE Compare admin 2018.05.22 368
107 APEX Web Services Proxy Settings admin 2016.06.07 361