LIMIT in SQL

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Video Tutorial

 LIMIT, OFFSET

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:

IDNameSalary
1Ram50000
2Shyam40000
3Mohan58000
4Kishan10000
5Mahesh23000
6Dinesh45000
7Amit99000
8Jatin87000

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.

IDNameSalary
1Ram50000
2Shyam40000
3Mohan58000
4Kishan10000
5Mahesh23000
6Dinesh45000

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.