ROWNUM is a virtual column automatically added to the result set of every SELECT statement executed in Oracle Database. Evaluation happens before ORDER BY and after the rows are fetched, meaning the numbering reflects the row generation order, not any later sort. Because ROWNUM is calculated on the fly, it is neither stored nor indexed and resets each time the query runs. Key behaviors:- Starts at 1 for the first returned row and increments by 1 for each subsequent row.- Evaluated before ORDER BY, so combining a simple WHERE ROWNUM <= N with ORDER BY may yield unexpected rows. Use a subquery that orders first, then filter by ROWNUM in an outer query.- Cannot test for ROWNUM = n directly except in hierarchical queries; use ROWNUM <= n or wrap in a subquery and compare a derived alias.- Works only in Oracle (and compatible engines such as Amazon Aurora for Oracle); other databases use LIMIT, TOP, or the ROW_NUMBER() analytic function instead.
ROW_NUMBER analytic function, FETCH FIRST, LIMIT, TOP, SQL ORDER BY, Oracle ROWID
Oracle Database V2 (1979)
ROWNUM is a pseudocolumn calculated before ORDER BY; ROW_NUMBER() is an analytic function calculated after ORDER BY, allowing partitioning and deterministic ordering.
Nest two subqueries: order the results in the innermost query, filter with ROWNUM <= high_value in the middle query, then pick rows greater than low_value in the outer query.
ROWNUM itself is virtually cost-free, but wrapping queries in subqueries to use it can prevent index usage and may increase memory. Use FETCH FIRST or OFFSET in newer Oracle versions when possible.
No. ROWNUM is generated at query runtime and cannot be inserted, updated, or indexed. Use a real column or sequence if persistent numbering is required.