← Back to MySQL Course | Chapter 6: Filtering & Sorting | Lesson 8 of 8

LIMIT & OFFSET

LIMIT is like saying 'just show me the first five' and OFFSET says 'skip the first ten', which is how pages of results work.
Syntax
sql
SELECT column1, column2
FROM table_name
LIMIT row_count [OFFSET skip_rows];

Limiting Your Results

Sometimes your queries match thousands of rows, but you only need to show a handful at a time. We can use the LIMIT clause to cap the maximum number of rows returned. This keeps our queries fast and manageable, especially on large tables.

Example: Limiting Your Results

sql
SELECT * FROM products LIMIT 5;

Skipping Records with OFFSET

The OFFSET clause tells MySQL to skip a specific number of rows before it starts returning results, such as skipping the first 20 rows to show page two. It is always used together with the LIMIT clause to define both where to start and how many rows to return.

Example: Skipping Records with OFFSET

sql
SELECT * FROM products LIMIT 20 OFFSET 20;

The Short Hand Limit Syntax

MySQL has a shorthand way to write both LIMIT and OFFSET in one line. You write LIMIT, then the offset number, a comma, and finally the row count limit, e.g. LIMIT 20, 10 skips 20 rows and returns the next 10.

Example: The Short Hand Limit Syntax

sql
SELECT * FROM products LIMIT 20, 20;

Creating Pages of Results

LIMIT and OFFSET are the keys to building database pagination for things like search results or product listings. By changing the offset based on the page number, we can display data in structured pages without loading the entire table at once.

Example: Creating Pages of Results

sql
SELECT * FROM products LIMIT 10 OFFSET 20; -- page 3, 10 per page

Mixing Filters Sorts and Limits

You can combine WHERE, ORDER BY, and LIMIT in a single query to filter, sort, and cap results together. The rules of order are strict: write WHERE first, then ORDER BY, and place LIMIT at the very end, or MySQL will raise a syntax error.

Example: Mixing Filters Sorts and Limits

sql
SELECT * FROM products WHERE category = 'Books' ORDER BY price DESC LIMIT 5;
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using LIMIT without ORDER BY, so the rows returned are not predictable.
  2. Miscalculating the offset for a page, when page n needs OFFSET (n - 1) * page_size.
  3. Writing LIMIT 20, 20 and confusing it with LIMIT 20 OFFSET 20, when in the shorthand the offset comes first.
Chapter Summary
  • The WHERE clause filters rows, combined with AND, OR, NOT, IN, BETWEEN, and LIKE.
  • IS NULL and IS NOT NULL test for missing values.
  • ORDER BY sorts results, and LIMIT with OFFSET returns a specific portion of them.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.