FETCH FIRST is part of the SQL standard row-limiting syntax supported by several database systems. It can be used to restrict the number of rows returned after ordering.
Understanding standard row-limiting syntax helps analysts write more portable SQL across supported database platforms.
An analyst can return the first 10 employees after sorting them by salary from highest to lowest.
SELECT
EmployeeName,
Salary
FROM Employees
ORDER BY Salary DESC
FETCH FIRST 10 ROWS ONLY;
FETCH FIRST is useful for ranking, reporting, previews, pagination, and database systems that support standard row-limiting syntax.
- FETCH FIRST can limit query results.
- It is supported by several database systems.
- It is commonly used with ORDER BY.
- It provides standard-style row limiting.
- Exact support depends on the database system.
What is FETCH FIRST used for?
FETCH FIRST limits the number of rows returned by a query in database systems that support the syntax.
Why use ORDER BY with FETCH FIRST?
ORDER BY defines which rows should appear first before the result is limited.