Description
The SQL keyword LIMIT is used to limit the rows returned by a SELECT statement when we are not interested in rows more than a certain limit.
- The LIMIT clause is supported by the MySQL database system.
- An equivalent clause TOP is supported by the SQL Server and MS Access database systems.
- An equivalent clause ROWNUM is supported by the Oracle database system.
NOTE: Always use the LIMIT clause along with the ORDER BY clause on a SELECT statement for desired results.
LIMIT Number
The below SQL statement returns the top three highest-paid employees.
SELECT * FROM employees
ORDER BY salary DESC LIMIT 3;
LIMIT With Offset
The below SQL statement returns the three highest-paid employees, starting from the 2nd row.
- The offset value for the 2nd row is 1, from where the 3 records must be returned.
SELECT * FROM employees
ORDER BY salary DESC LIMIT 1, 3;