How to sort and paginate results using ORDER BY and LIMIT in MySQL?
Shows sorting data ascending/descending and implementing page offset pagination.
-- Sort employees by salary descending SELECT employee_id, first_name, salary FROM employees ORDER BY salary DESC; -- Paginate: Fetch top 2 records skipping the first 1 SELECT employee_id, first_name, salary FROM employees ORDER BY salary DESC LIMIT 2 OFFSET 1;
+-------------+------------+----------+ | employee_id | first_name | salary | +-------------+------------+----------+ | 2 | Alice | 62000.00 | | 1 | John | 55000.00 | +-------------+------------+----------+ 2 rows in set (0.00 sec)