LIMIT in SQL
Video Tutorial
What is a Limit in SQL?
A Limit clause is commonly used in SQL when dealing with large datasets to limit the number of records that are returned from the SELECT statement.
How to Use the Limit Clause in SQL?
Limit clauses are often used nested inside a SELECT statement, and the only purpose they satisfy is to return the specified number of records only. The syntax for the same is given below.
The parameter in the above SELECT statement is number_rows, which is the limiting number of rows it is expected to return.
What is a Select LIMIT Statement, its Syntax, and Parameter Values?
As the name suggests, a SELECT limit statement is a standard statement with a LIMIT clause added to it, which is aimed at limiting the number of records selected.
The syntax for the same is given below.
The number_rows parameter can have any positive integer value.
Example of Limit Clause in SQL
Consider the example table below:
ID | Name | Salary |
---|---|---|
1 | Ram | 50000 |
2 | Shyam | 40000 |
3 | Mohan | 58000 |
4 | Kishan | 10000 |
5 | Mahesh | 23000 |
6 | Dinesh | 45000 |
7 | Amit | 99000 |
8 | Jatin | 87000 |
Let's say we want the top 6 records to be selected from this table, the limit clause, along with the SELECT statement, will help us achieve it.
The result of the following select statement is given below.
ID | Name | Salary |
---|---|---|
1 | Ram | 50000 |
2 | Shyam | 40000 |
3 | Mohan | 58000 |
4 | Kishan | 10000 |
5 | Mahesh | 23000 |
6 | Dinesh | 45000 |
What are the Advantages of the LIMIT Clause in SQL?
According to the LIMIT clause, only the maximum number of rows is included in the result collection. No additional rows are returned that satisfy the question collection condition, which saves us time and memory.
Learn More
Conclusion
- LIMIT in SQL is useful for selecting a specific number of rows from the table.
- It is used inside a SELECT statement.
- It can also be followed by clauses like ORDER BY which will give the ordered result for a specified number of rows.