Ravindra BagaleCourses & study guides Track your progress

5. ORDER BY, DISTINCT and Top-N Results

5.5 OFFSET for Simple Pagination (Idea Only)

Why. Apps show page 2 of results — OFFSET is the idea behind that.

What. OFFSET skips rows after sorting. LIMIT 5 OFFSET 5 ≈ page 2 of size 5.

How. ORDER BY … LIMIT 5 OFFSET 5;

Steps

  1. Get top 5.
  2. Get next 5 with OFFSET 5.
  3. Compare the two pages.

Examples

Example 1 · dataset: raja_coffee

SELECT order_id, amount_inr
FROM orders
ORDER BY amount_inr DESC
LIMIT 5 OFFSET 5;

Output

order_idamount_inr
1019900.0
1003890.0
1006890.0
1012890.0
1022890.0

Meaning. Rows 6–10 by amount — second page idea.

Ravindra Bagale's Tip

Deep OFFSET on huge tables can be slow later — idea only for now.