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 this article, we will learn how to limit query results in SQL using different examples. A MySQL supports the LIMIT clause to select a limited number of records. If we want to LIMIT the number of results that will return us a nmoumber of rows then we simply use the LIMIT command.
Understanding the LIMIT
Clause in SQL
The LIMIT
clause is a SQL construct used to restrict the number of rows returned by a query. This is particularly useful when dealing with large tables and datasets where we only need a subset of the data. It simplifies data retrieval and reduces the overhead of handling excessive results.
Syntax:
SELECT column1, column2, ...
FROM table_name
[WHERE conditions]
[ORDER BY expression [ASC | DESC]]
LIMIT count;
Key Terms
WHERE conditions
: Optional filters to specify which rows should be included in the result set.ORDER BY expression
: Optional clause to sort the results in ascending (ASC
) or descending (DESC
) order.LIMIT count
: Specifies the maximum number of rows to return.
To demonstrate the LIMIT
clause, we will use a sample table named Participant. This table contains data about participants, including their ID, Name, Percent, and Branch. Refer to the attached image of the sample table for details.

Example 1: Retrieving Top Rows
To fetch the top 2 participants with the highest percent. This query sorts the participants by their percent scores in descending order using the ORDER BY
clause. Then, the LIMIT 2
clause ensures that only the first two rows from the sorted results are returned.
Query:
SELECT *
FROM Participant
ORDER BY Percent DESC
LIMIT 2;
Output

Explanation:
This query will return the top 2 participants with the highest scores.
ORDER BY Percent DESC
sorts the rows in descending order based on thePercent
column.LIMIT 2
ensures only the first 2 rows from the sorted results are returned.
Example 2: Using LIMIT
with WHERE
Clause
Suppose we want to exclude participants from the Civil branch and fetch the first 2 participants with the lowest scores. This query first filters out participants from the Civil branch using the `WHERE` clause. Then, it sorts the remaining participants by their scores in ascending order and limits the result to the first two rows using the `LIMIT 2` clause.
Query:
SELECT *
FROM Participant
WHERE Branch != 'Civil'
ORDER BY Percent
LIMIT 2;
Output

Explanation:
The query selects participants who are not in the Civil branch, sorts them by their scores in ascending order, and returns the top 2 rows.
WHERE Branch != 'Civil'
: Filters out rows where theBranch
column isCivil
.
ORDER BY Percent
: Sorts the remaining rows in ascending order byPercent
.LIMIT 2
: Restricts the results to the first 2 rows after sorting.
Conclusion
The LIMIT
clause is a simple yet powerful feature in SQL that enables precise control over query results. Whether we’re sorting, filtering, or paginating data, understanding how to apply LIMIT
can significantly enhance query performance and usability. By combining it with ORDER BY
and WHERE
clauses, we can retrieve tailored subsets of data to suit our specific requirements. Mastering this feature is essential for anyone working with SQL databases, particularly when managing large datasets.