LIMIT and OFFSET are MySQL clauses used to control how many rows are returned and from which position. In PHP-based applications, they are widely used for pagination and performance optimization.
-- Basic LIMIT syntax
SELECT column1, column2
FROM table_name
LIMIT number_of_rows;
-- Fetch first 5 employees
SELECT * FROM employees
LIMIT 5;
-- Skip first 5 rows and fetch next 5
SELECT * FROM employees
LIMIT 5 OFFSET 5;
If a table has 20 records, using LIMIT 5 OFFSET 5 will return records from position 6 to 10. This is commonly used when moving between pages.
// PHP pagination using LIMIT and OFFSET
$page = 2;
$limit = 5;
$offset = ($page - 1) * $limit;
$sql = "SELECT * FROM employees LIMIT $limit OFFSET $offset";
ORDER BY with LIMIT for predictable resultsproducts table