FETCH is used with ORDER BY and OFFSET in supported database systems to limit the number of rows returned.
FETCH provides a portable approach for row limiting across systems that support the relevant SQL syntax.
Return the first ten orders after sorting by order amount.
SELECT
c.CustomerName,
o.OrderID,
o.OrderAmount
FROM Customers c
INNER JOIN Orders o
ON c.CustomerID = o.CustomerID
ORDER BY o.OrderAmount DESC
FETCH FIRST 10 ROWS ONLY;
FETCH is useful for pagination, reporting, and controlled retrieval of large joined datasets.
- FETCH can limit query results.
- FETCH is often used with ORDER BY.
- OFFSET can be used for pagination.
- Database support and exact syntax can vary.
- FETCH can be used with JOIN queries.
What is FETCH used for?
FETCH can limit the number of rows returned by a query in database systems that support the syntax.
Can FETCH be used with JOIN?
Yes. FETCH can limit the final result of a joined query.