Sub topic 11
Limiting Rows with `FETCH FIRST`
This chapter assumes you understand tables, rows, columns, and basic SELECT statements. You should also be able to use ORDER BY to sort query results and run SQL in an Oracle Database session.
When a query returns many rows, you may need only the first few. Oracle SQL provides the row-limiting clause for this purpose:
FETCH FIRST 5 ROWS ONLYThe clause is usually placed after ORDER BY. The database sorts the rows first, then returns the requested number of rows.
Returning the top rows
Suppose the HR sample schema contains an employees table. This query returns the five employees with the highest salaries:
SELECT employee_id, last_name, salary
FROM employees
ORDER BY salary DESC
FETCH FIRST 5 ROWS ONLY;DESC sorts the highest salaries first. FETCH FIRST 5 ROWS ONLY then keeps only the first five rows from that sorted result.
The clause was introduced in Oracle Database 12c. It is often clearer than older techniques based on ROWNUM, especially when a query needs both sorting and row limiting.
| Clause | Purpose |
|---|---|
| `ORDER BY salary DESC` | Places the highest salaries first |
| `FETCH FIRST 5 ROWS ONLY` | Returns only five rows |
| `FETCH FIRST 5 ROWS WITH TIES` | Includes rows tied with the final row |
Why `ORDER BY` matters
Without ORDER BY, Oracle is not required to return rows in a predictable order. This query limits the result to five rows, but it does not define which five:
SELECT employee_id, last_name, salary
FROM employees
FETCH FIRST 5 ROWS ONLY;The database may return different rows after an execution-plan change, an index change, or a change in the table data. If “first” has a business meaning, specify that meaning with ORDER BY.
A stable ordering is also useful when two rows have the same sort value. Add a second column as a tie breaker:
SELECT employee_id, last_name, salary
FROM employees
ORDER BY salary DESC, employee_id
FETCH FIRST 5 ROWS ONLY;The result is sorted by salary from highest to lowest. Employees with equal salaries are then sorted by employee_id.
Including ties
ONLY stops after exactly the requested number of rows. If the last selected row shares the same ordering value with additional rows, those additional rows are excluded.
Use WITH TIES when all rows tied with the final row should be included:
SELECT employee_id, last_name, salary
FROM employees
ORDER BY salary DESC
FETCH FIRST 5 ROWS WITH TIES;This can return more than five rows. For example, if the fifth and sixth employees have the same salary, both rows may appear. The tie is based on the expressions in the ORDER BY clause, so choose those expressions carefully.
Skipping rows with `OFFSET`
You can skip rows before taking the next set. This is useful for simple pagination:
SELECT employee_id, last_name, salary
FROM employees
ORDER BY salary DESC, employee_id
OFFSET 5 ROWS
FETCH NEXT 5 ROWS ONLY;This query skips the first five sorted rows and returns the next five. FETCH NEXT and FETCH FIRST express the same limiting idea in this position.
Pagination can become inconsistent when rows are inserted or deleted between requests. Use a deterministic ORDER BY, and remember that page boundaries can still move when the underlying data changes.
Exercise
Write a query that returns the three lowest-paid employees from the employees table, breaking salary ties with employee_id.
Show answer
SELECT employeeid, lastname, salary FROM employees ORDER BY salary ASC, employee_id FETCH FIRST 3 ROWS ONLY;
The ORDER BY clause sorts employees from the highest salary to the lowest. FETCH FIRST 5 ROWS ONLY then returns only the first five rows of that sorted result.
Without ORDER BY, Oracle may return any five rows. A table does not have an inherent display order, so a query without sorting does not define which rows are “first.”
Syntax and options
The general form is:
SELECT column_list
FROM table_name
[WHERE condition]
[ORDER BY sort_expression]
[OFFSET number ROWS]
FETCH {FIRST | NEXT} number {ROW | ROWS} {ONLY | WITH TIES};| Clause | Purpose |
|---|---|
| `OFFSET n ROWS` | Skips the first `n` rows |
| `FETCH FIRST n ROWS ONLY` | Returns at most `n` rows |
| `FETCH NEXT n ROWS ONLY` | Same meaning as `FETCH FIRST` |
| `WITH TIES` | Includes rows tied with the final sort value |
FIRST and NEXT are interchangeable. ROW and ROWS are also interchangeable.
Skip rows with OFFSET
OFFSET is useful for paging through results. This query skips the first ten employees after sorting and returns the next ten:
SELECT employee_id, last_name, salary
FROM employees
ORDER BY salary DESC, employee_id
OFFSET 10 ROWS
FETCH NEXT 10 ROWS ONLY;The second sort expression, employee_id, makes the ordering more stable when two employees have the same salary. A stable ordering helps prevent rows from moving between pages when values are equal.
The first page would use OFFSET 0 ROWS. The second page uses OFFSET 10 ROWS, and the third page uses OFFSET 20 ROWS.
Include tied rows
ONLY limits the result to the requested number of rows. WITH TIES can return more rows when additional rows have the same values as the final row according to the ORDER BY clause.
SELECT employee_id, last_name, salary
FROM employees
ORDER BY salary DESC
FETCH FIRST 5 ROWS WITH TIES;If the fifth and sixth employees have the same salary, both appear. This means the query can return more than five rows. Use WITH TIES when completeness for tied rankings matters more than an exact row count.
Logical processing order
Oracle conceptually processes this query in the following order:
- Read rows from
FROM. - Restrict rows with
WHERE. - Sort the remaining rows with
ORDER BY. - Skip rows with
OFFSET. - Return the requested rows with
FETCH.
The database optimizer may execute operations differently internally, but the result follows this logical order. Therefore, FETCH FIRST limits the ordered result, not the unsorted table.
Exercise
Write a query against employees that returns the three employees with the lowest salaries, including all employees tied with the third-lowest salary.
Show answer
SELECT employeeid, lastname, salary FROM employees ORDER BY salary ASC FETCH FIRST 3 ROWS WITH TIES;