MySQL LIMIT Clause
The LIMIT
clause in MySQL is used to specify the number of records to return.
Basic Syntax:
Example: Basic LIMIT Syntax
SELECT column1, column2, ...
FROM table_name
LIMIT number;
This SQL command selects specific columns from table_name
and limits the result set to number
of rows.
Example Using LIMIT:
Example: Limiting Results
SELECT * FROM users
LIMIT 10;
This SQL command selects all columns from the users
table and limits the result set to 10 rows.
Using LIMIT with OFFSET:
Example: LIMIT with OFFSET
SELECT * FROM users
LIMIT 5 OFFSET 10;
This SQL command selects all columns from the users
table, skips the first 10 rows, and then returns the next 5 rows.
Alternative Syntax for LIMIT with OFFSET:
Example: Alternative Syntax
SELECT * FROM users
LIMIT 10, 5;
This SQL command is equivalent to the previous example. It selects all columns from the users
table, skips the first 10 rows, and then returns the next 5 rows.
Conclusion
The LIMIT
clause in MySQL is a powerful tool for controlling the number of rows returned in a query result set, allowing for precise data retrieval and pagination.