today:
119
yesterday:
179
Total:
1,001,215

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
46 Oracle APEX 5.1.4 Installation admin 2018.04.17 132
45 HEAD TITLE (제목)을 조건에 따라 바꾸기 admin 2016.05.03 132
44 Column Data 일괄 추가 admin 2023.11.24 129
43 Global & Custom Serach file admin 2016.04.28 127
42 LIST OF VALUE (DYNAMIC) admin 2016.04.28 126
41 What Is ERP? admin 2018.02.14 121
40 test admin 2016.05.11 121
39 Oracle SQL Developer: Online Migration 3.1 admin 2018.04.25 120
38 How to Install Oracle 11g on Windows 10 - 64 bit | Download / Install Oracle 11g Database file admin 2022.03.17 120
37 Interactive Report admin 2016.05.07 116
36 Display Value, Return Value admin 2016.05.10 115
35 LAST DAY SQL admin 2024.01.19 114
34 Null Display Value admin 2024.01.10 111
33 QR-CODE admin 2017.11.30 111
32 APEX ADMIN PASSWORD 바꾸기 admin 2016.05.02 108
31 SQL DEVELOPER file admin 2022.03.17 106
30 TEST admin 2023.03.27 97
29 SQL Basics - Lesson 1 - Introduction admin 2017.06.17 97
28 image (products) admin 2017.11.24 92
27 Material requirements planning (MRP) admin 2018.02.14 88