📅  最后修改于: 2023-12-03 15:20:14.551000             🧑  作者: Mango
In SQL, the LIMIT
clause is used to restrict the number of results returned by a SQL query. In Oracle, the equivalent to LIMIT
is the ROWNUM
keyword, which is used to limit the number of rows returned by a query.
The syntax for using ROWNUM
in Oracle is as follows:
SELECT column1, column2, ...
FROM table_name
WHERE condition
AND ROWNUM <= number_of_rows;
The SELECT
statement is used to select the columns from the specified table_name
. The WHERE
clause is used to specify any conditions that the rows must meet in order to be included in the result set. The ROWNUM
keyword is used to limit the number of rows returned.
Suppose we have a table called employees
with the following data:
| id | name | age | |----|------|-----| | 1 | John | 25 | | 2 | Jane | 30 | | 3 | Bob | 27 | | 4 | Mary | 35 | | 5 | Tom | 22 |
To select the first two rows of this table using ROWNUM
, we would use the following query:
SELECT id, name
FROM employees
WHERE ROWNUM <= 2;
This would return the following result:
| id | name | |----|------| | 1 | John | | 2 | Jane |
Note that ROWNUM
is evaluated before the ORDER BY
clause, so if you want to retrieve the first n
rows in a specific order, you need to use a subquery.
In Oracle, the ROWNUM
keyword is used to limit the number of rows returned by a SQL query. It is equivalent to the LIMIT
clause used in other SQL dialects. By using ROWNUM
, you can easily restrict the number of results returned by a query and improve the performance of your application.