SQL – Limit

The SQL – LIMIT clause is used to limit the number of rows returned by a SELECT, UPDATE, or DELETE statement. The basic syntax for the LIMIT clause is as follows:

SELECT column1, column2, ...
FROM table_name
LIMIT number_of_rows;

For example, the following SQL query selects the first 10 rows from the employees table:

SELECT first_name, last_name, salary
FROM employees
LIMIT 10;

In this example, the query selects the first_name, last_name, and salary columns from the employees table, and returns only the first 10 rows.

Another example, the following SQL query selects the last 5 rows from the employees table:

SELECT first_name, last_name, salary
FROM employees
ORDER BY salary DESC
LIMIT 5;

In this example, the query first sorts the employees by salary in descending order, and then returns only the last 5 rows from the result set.

It’s important to note that the LIMIT clause should be used after the SELECT, UPDATE, or DELETE statement and before any other clauses such as WHERE, JOIN, GROUP BY, and ORDER BY. Also, the behavior of the LIMIT clause can vary depending on the specific database management system you are using.