In MySQL, The LIMIT clause is used to restrict the number of rows returned by a SELECT query.
It is often used for pagination or when we only want to retrieve a specific number of rows from the result set.
SELECT column1, column2, ... FROM table_name LIMIT number_of_rows;
column1, column2, ...: The columns we want to retrieve from the table.
table_name: The name of the table from which we want to retrieve data.
LIMIT: The keyword used to restrict the number of rows.
number_of_rows: The maximum number of rows to be returned.
SELECT * FROM orders LIMIT 5;
This query retrieves all columns from the orders table but limits the result set to only the first 5 rows.
We can use the "OFFSET" clause along with "LIMIT" to skip a certain number of rows and retrieve the next set of rows. This is commonly used for implementing pagination.
SELECT * FROM orders LIMIT 5 OFFSET 5;
Note: This query retrieves all columns from the orders table, but it starts from the 6th row (skipping the first 5 rows) and returns the next 5 rows.
SELECT * FROM orders LIMIT 5, 5;
The LIMIT clause can be used with or without the OFFSET clause.
The OFFSET value specifies the number of rows to skip before starting to return rows.
Different database systems may have variations in the syntax. For example, in some databases, we might see LIMIT 5 OFFSET 5 written as LIMIT 5, 5.