Simple pagination with LIMIT and OFFSET

Page 3 with 2 rows per page skips 4 rows. Always sort so pages are stable.

SQL · Simple pagination with LIMIT and OFFSETBeginner
SET @page = 3;
SET @per  = 2;

SELECT id, name
FROM products
ORDER BY id
LIMIT 2 OFFSET 4;                 -- (page - 1) * per_page = 4

SELECT COUNT(*) AS total_rows FROM products;     -- to calculate the page count
limitoffsetpagination