April 19, 2023 By Matthew Rathbone

SQL Server is a powerful and widely used relational database management system that helps organizations manage their data in an efficient and organized manner. With a wide variety of data management tasks, it can be easy to retrieve large amounts of data, which can slow down your system and cause performance issues. In this article, we will look at how to limit the number of rows returned in SQL Server to improve performance and make your queries more efficient.

The Importance of Limiting Rows

When working with large datasets, it is often necessary to limit the number of rows returned by a query. This is because retrieving too many rows can slow down your system and consume large amounts of memory, which can lead to performance issues. Limiting the number of rows returned is also useful for testing purposes, as it allows you to retrieve a small sample of data for testing and debugging.

Additionally, limiting the number of rows returned can help you save time and resources. For example, if you are working on a dashboard that displays data from multiple sources, you may not need to retrieve all the data at once. By limiting the number of rows returned, you can speed up your queries and ensure that your system remains responsive.

The TOP Keyword

SQL Server provides a simple way to limit the number of rows returned by a query using the TOP keyword. The TOP keyword allows you to specify the number of rows you want to retrieve from a query. For example, the following query retrieves the first 10 rows from the Customers table:

SELECT TOP 10 * 
FROM Customers;

In this example, the TOP 10 keyword specifies that you want to retrieve the first 10 rows from the Customers table. You can replace 10 with any positive integer to specify the number of rows you want to retrieve.

The OFFSET-FETCH Clause

SQL Server 2012 introduced a new way to limit the number of rows returned by a query using the OFFSET-FETCH clause. The OFFSET-FETCH clause allows you to specify the starting point and the number of rows you want to retrieve. For example, the following query retrieves the 11th to 20th rows from the Customers table:

SELECT * 
FROM Customers
ORDER BY CustomerID
OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;

In this example, the OFFSET 10 ROWS clause specifies the starting point (the 11th row), and the FETCH NEXT 10 ROWS ONLY clause specifies the number of rows you want to retrieve (the next 10 rows).

Limiting Rows with a row number variable

The TOP keyword and the OFFSET-FETCH clause are not the only ways to limit the number of rows returned in SQL Server. You can also use the T-SQL language to limit the number of rows returned by a query. For example, the following query retrieves the first 10 rows from the Customers table using T-SQL:

DECLARE @rownum INT = 0;

SELECT @rownum = @rownum + 1 AS RowNumber, *
FROM Customers
WHERE @rownum <= 10;

In this example, the @rownum variable is used to keep track of the row number, and the WHERE clause is used to limit the number of rows returned to 10.

Beekeeper Studio Is A Free & Open Source Database GUI

Best SQL query & editor tool I have ever used. It provides everything I need to manage my database. - ⭐⭐⭐⭐⭐ Mit

Beekeeper Studio is fast, intuitive, and easy to use. Beekeeper supports loads of databases, and works great on Windows, Mac and Linux.

Beekeeper's Linux version is 100% full-featured, no cut corners, no feature compromises.