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. 


How to Limit Rows in a SQL Server?

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.

Similar Reads

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....

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....