The LIMIT clause in MySQL restricts the number of rows returned by a query. It's commonly used for pagination, sampling results during testing, and improving performance when only a subset of data is needed.
LIMIT without ORDER BY can produce arbitrary rows; pair with ORDER BY for deterministic results.
-- Syntax for LIMIT: select desired columns and limit rows
SELECT column1, column2, ... FROM table_name LIMIT number_of_rows;
-- Select the first 5 records from the 'students' table
SELECT * FROM students LIMIT 5;
-- Select 10 records starting from the 11th record (skip first 10)
SELECT * FROM students LIMIT 10 OFFSET 10;
ORDER BY when you need consistent ordering; otherwise row order is not guaranteed.LIMIT with ORDER BY to paginate consistently.OFFSET values for deep pagination — consider keyset pagination for performance.students table.OFFSET.LIMIT with ORDER BY created_at DESC to fetch the latest N records deterministically.