Mastering MySQL for Data AnalystsLesson 14 of 42
Lesson 14 of 42

Pagination & Record Slicing: LIMIT and OFFSET in MySQL

Overview & Learning Objectives

Web applications and data dashboards rarely display millions of records at once; they paginate results into digestible chunks (e.g. 20 or 50 records per page). MySQL pioneered the intuitive `LIMIT count OFFSET offset` and `LIMIT offset, count` syntax. Analysts must also understand why deep pagination (`OFFSET 1000000`) degrades performance and how to replace it with keyset (seek) pagination.

  • Extract top-performing records using `ORDER BY ... LIMIT n`.
  • Implement web application pagination using `LIMIT count OFFSET offset` and `LIMIT offset, count`.
  • Understand the performance degradation of large offsets and implement keyset seek pagination.

Industry Standard

Standard: High-scale consumer platforms at Swiggy and Twitter mandate Keyset (Cursor) Pagination rather than OFFSET for infinite-scroll APIs to guarantee $O(1)$ query latency.

Real-World Example: ScoopCraft Web Portal Order History Pagination

ScoopCraft customer app paginates order history at 10 orders per page.

Page NumberFormula (offset = (page-1)*10)SQL ClauseRecords Returned
Page 1Offset = 0LIMIT 10 OFFSET 0Rows 1 to 10
Page 2Offset = 10LIMIT 10 OFFSET 10Rows 11 to 20
Page 3Offset = 20LIMIT 10 OFFSET 20Rows 21 to 30

Syntax & Core SQL Implementation

-- Standard MySQL LIMIT OFFSET
SELECT order_id, customer_id, order_total, order_date
FROM orders
ORDER BY order_date DESC
LIMIT 10 OFFSET 20;

-- Shorthand MySQL syntax: LIMIT offset, count
SELECT * FROM orders ORDER BY order_id LIMIT 20, 10;

-- High-performance Keyset (Seek) Pagination for deep pages
SELECT order_id, order_total, order_date
FROM orders
WHERE order_id < 98450  -- Last seen ID from previous page
ORDER BY order_id DESC
LIMIT 10;

Parameters & Arguments Breakdown

  • LIMIT count: Maximum number of rows to return.
  • OFFSET offset: Number of rows to skip before beginning to return rows.
  • LIMIT offset, count: Alternative MySQL shorthand where first number is offset and second is count.

Sample Walkthrough (Q&A)

Question: Why does "LIMIT 10 OFFSET 1000000" run slowly on large MySQL tables?

SQL Pattern: LIMIT 10 OFFSET 1000000

Result: High disk and CPU latency (takes seconds or minutes)

Explanation: MySQL must read and discard all 1,000,000 preceding rows before returning the 10 target rows. Keyset pagination solves this.

Common Mistakes & Limitations

  • Using `LIMIT` without an `ORDER BY` clause; without sorting, MySQL returns arbitrary non-deterministic rows.
  • Confusing the comma shorthand `LIMIT 20, 10` (skips 20, takes 10) with `LIMIT 10 OFFSET 20`.

Keyboard Shortcuts (MySQL Workbench)

ActionWindowsMac
Execute QueryCtrl + EnterCmd + Enter

Pro Tips & Modern Best Practices

  • Always prefer the explicit `LIMIT count OFFSET offset` syntax over comma shorthand `LIMIT offset, count` to avoid parameter order confusion.
  • For deep pagination in analytics, use seek-based pagination on an indexed column (`WHERE id > last_seen_id ORDER BY id ASC LIMIT 50`).