SQL
Implementing Pagination with LIMIT and OFFSET
Learn how to efficiently retrieve a specific page of results from a large dataset using SQL's LIMIT and OFFSET clauses for pagination in web applications.
SELECT id, name, created_at
FROM products
ORDER BY created_at DESC
LIMIT 10 OFFSET 20;
How it works: This SQL snippet demonstrates how to paginate results. It selects 10 records from the 'products' table, skipping the first 20 records (which would be page 3 if each page has 10 items). The 'ORDER BY' clause is crucial for consistent pagination, ensuring the order of results remains the same across pages. 'LIMIT' defines the number of rows to return, and 'OFFSET' defines the starting point.