How to Limit Rows in a SQL Server?
Last Updated :
10 Jun, 2024
To limit rows in SQL Server, use the TOP clause in the SELECT statement. Using the TOP clause in SQL Server, users can limit the number of rows in the results set.
Here, we will understand how to limit rows in SQL Server with the help of different examples.
Steps to Limit Rows in SQL Server
Let’s check the steps to limit the number of rows on the SQL server. We will start by creating a table, then inserting records, and then using the TOP clause to limit rows.
Step 1: Creating the Database
Use the below SQL statement to create a database called GeeksForGeeksDatabase.
Query:
CREATE DATABASE GeeksForGeeksDatabase;
Step 2: Using the Database
Use the below SQL statement to switch the database context to GeeksForGeeksDatabase.
Query:
USE GeeksForGeeksDatabase;
Step 3: Create Table
Use the below SQL statement to create a table ‘Participant’
Query:
CREATE TABLE Participant (
ID INTEGER PRIMARY KEY,
Name TEXT NOT NULL,
Percentage INTEGER NOT NULL,
Branch TEXT NOT NULL
);
Step 4: Insert data into the table
Query:
INSERT INTO Participant VALUES
(55, 'BB', 81, 'Civil'),
(56, 'NN', 75, 'IT'),
(57, 'RR', 100, 'CSE'),
(58, 'SS', 94, 'Civil');
You can use the below statement to see the contents of the created table:
Query:
SELECT * FROM Participant;

Now let’s see how to use limit query here. To limit the resultant data for MS Access we use SELECT TOP syntax. LIMIT syntax works in MYSQL. In the Microsoft database, we use the below syntax (TOP which works similar to  LIMIT in MYSQL)
Step 5: Limit Rows Using TOP and ORDER BY
Use the TOP clause to limit rows in results set and ORDER BY statement to sort the results in ascending or descending order.
Syntax:
SELECT TOP(count) column1, column2, …,column n FROM table_name [WHERE your conditions] [ORDER BY expression [ ASC | DESC ]];
In the above syntax, WHERE conditions are optional conditions that must be true for the records to be selected.
ORDER BY expression is an optional statement in the query used to return the result in either ascending or descending order according to the keyword (ASC or DESC). Here count is the number of rows to be returned from the result.
Limit Rows in a SQL Server Examples
Let’s look at some SQL query examples on how to limit rows in SQL Server. We will use the above table in these examples.
Query 1:
This query limit the rows and returns only 2 rows.
SELECT TOP(2) *
FROM Participant
ORDER BY Percentage DESC;
Output:

Using the TOP query we found the 2 toppers participants from our table data having maximum percentage and do not want to use any conditional statements. ORDER BY Percent DESC has sorted the record in descending order and using LIMIT 2 we got the first 2 rows from the sorted result.
We can also include some situations using the WHERE clause in the above example. Suppose if we don’t want the ID 58 participant in our result set.
We can write queries like :
Query 2:
SELECT TOP(2) *
FROM Participant
WHERE ID != 58
ORDER BY Percentage;
Output:
The above query will select all the participants according to the imposed condition (i.e. all Participants except ID 58 participant will be selected) then the results would be sorted by Percentage in ascending order (The ORDER BY keyword sorts the records in ascending order by default). Finally, the first 2 rows would be returned by the above query as we mentioned TOP(2).

This is how we can limit the records from tables in SQL using TOP. We can further play with the SQL queries and get the resultant data according to different conditions and limitations.Â
Similar Reads
How to Join to First Row in SQL Server
Joining the first row in SQL Server can be a common requirement in various scenarios, such as when we need to retrieve specific data associated with the first occurrence of a particular group or when you want to fetch additional details related to the first record in a result set. In this article, W
4 min read
How to Limit Query Results in SQL?
SQL provides powerful features like the LIMIT clause to control the number of rows returned, making it an essential tool for optimizing queries and retrieving precise results. Whether weâre managing large datasets or working on performance tuning, understanding how to limit query results is key. In
4 min read
TOP Clause in Microsoft SQL Server
TOP clause in Microsoft SQL Server fetches a limited number of rows from a database. The SELECT TOP clause is very useful when dealing with large databases. The TOP clause is useful for fetching the data records in larger datasets as it reduces the complexity. SyntaxTOP clause syntax in Microsoft SQ
2 min read
How to Return Random Rows Efficiently in SQL Server?
In this article, we are going to learn an SQL Query to return random rows efficiently. To execute the query, we are going to first create a table and add data into it. We will then sort the data according to randomly created IDs(using the NEWID() method) and return the top rows after the sorting ope
2 min read
How to Select Row With Max Value in SQL Server
In SQL Server, retrieving rows that contain the maximum value for a specific column for each distinct value in another column can be a common and challenging task. This process is done by identifying the maximum value for each group and then selecting the corresponding rows. In this article, we'll e
6 min read
Select top in MS SQL Server
Prerequisite - Select in MS SQL Server Suppose that a user wants to extract the top students from the whole institution but has to use some complex queries to extract the data. To avoid complexity, the user can use 'Select Top'. 'Select Top' extracts the limited number of rows. This results in accur
2 min read
How to Perform Batch Updates in SQL Server
The Update statement is a SQL keyword to update data in the database. We can update all the rows in the database or some values with the help of conditions. The update is a SQL keyword, and it falls under Data Manipulation Language (DML), as the name suggests it is used to manipulate the data. Updat
5 min read
How to Join First Row in SQL?
SQL (Structured Query Language) is a powerful tool used to manage and manipulate data in relational databases. A common requirement when dealing with data relationships is to fetch only the first row from a table based on specific conditions or criteria. This can be particularly useful when joining
6 min read
How to Update Top 100 Records in SQL Server
SQL Server is a Relational database Management system which is developed by Microsoft and is one of the most used databases in the world. It provides the developer with a set of rich functionalities to create tables, insert data in them, and then manipulate and play with them as and when necessary.
5 min read
SQL Server INSERT Multiple Rows
SQL Server is a relational Database Management System(RDBMS). It offers various features for creating, and managing databases with its efficient tools. It can handle both small-scale and large-scale industry database applications. INSERT Statement in SQL ServerThe Insert statement is a command of th
4 min read