Beginner

23. FETCH FIRST

Learn how FETCH FIRST can be used to limit the number of rows returned by a SQL query.

📘 Lesson 23 of 728 ⏱ 7 min 🎯 Beginner

FETCH FIRST is a SQL standard-based approach for limiting the number of rows returned by a query. It is supported by several modern relational database systems and can be used after an ORDER BY clause to retrieve a specified number of rows.

SQL syntax for limiting rows varies between database systems. Understanding FETCH FIRST helps analysts recognize another commonly used approach for retrieving a limited result set.

If you want to retrieve the five employees with the highest salaries, you can sort salaries in descending order and then fetch the first five rows.

SQL
SELECT
    EmployeeName,
    Salary
FROM Employees
ORDER BY Salary DESC
FETCH FIRST 5 ROWS ONLY;

FETCH FIRST can be used in ranking reports, top-N analysis, financial analysis, operational reporting, and data exploration.

  • FETCH FIRST can limit returned rows.
  • It can be used with ORDER BY.
  • It is supported by several database systems.
  • It is useful for top-N analysis.
  • Row-limiting syntax varies between databases.
What is FETCH FIRST used for?

FETCH FIRST is used to limit the number of rows returned by a SQL query.

Why might row-limiting syntax differ between databases?

Different database systems implement SQL standards with their own syntax and platform-specific features.

Have you finished this lesson?

Introduction to SQL – All Lessons