ROWNUM function in SQL is used to return the row number in a result set. It is typically used to limit the number of rows returned in a result set. Here are some common uses of the ROWNUM function:
- Retrieve the first N rows of data.
SELECT *
FROM table_name
WHERE ROWNUM <= N;
- Retrieve data within a specified range.
SELECT *
FROM (
SELECT *, ROWNUM AS rnum
FROM table_name
)
WHERE rnum BETWEEN X AND Y;
- Return the first N unique rows in the result set.
SELECT DISTINCT column_name
FROM table_name
WHERE ROWNUM <= N;
It is important to note that ROWNUM is a specific function in the Oracle database and may have different implementations in other databases.