SQL SELECT TOP / LIMIT
Chapter 12 of 40 · Estimated read time: 7 min
When a table has millions of rows, fetching everything is slow and often unnecessary. LIMIT (or SELECT TOP) caps the number of rows returned. It is also the foundation for building pagination — showing results page by page.
Syntax — Different Databases Use Different Keywords
MySQL / PostgreSQL / SQLite / BigQuery
LIMIT n
SELECT * FROM products LIMIT 3;SQL Server / Azure SQL
SELECT TOP n
SELECT TOP 3 * FROM products;Oracle DB
FETCH FIRST n ROWS ONLY
SELECT * FROM products FETCH FIRST 3 ROWS ONLY;-- MySQL / PostgreSQL (most common in analytics)
SELECT column1, column2
FROM table_name
ORDER BY column1
LIMIT n;
LIMIT without ORDER BY is unpredictable. Without ORDER BY, the database returns any n rows — the order is not guaranteed across runs. Always pair LIMIT with ORDER BY when row order matters.
Example 1 — Show Only the First 3 Products
SELECT product_id, product_name, price FROM products LIMIT 3;
Returns only 3 rows (insertion order — unpredictable)
| product_id | product_name | price |
|---|---|---|
| 1 | Wireless Mouse | 899 |
| 2 | USB-C Hub | 1499 |
| 3 | Notebook A5 | 99 |
Example 2 — Top 3 Most Expensive Products
-- Always sort BEFORE limiting so you get the meaningful top rows SELECT product_name, price FROM products ORDER BY price DESC LIMIT 3;
Top 3 most expensive
| product_name | price |
|---|---|
| Ergonomic Chair | 8999 |
| USB-C Hub | 1499 |
| Desk Lamp | 1199 |
Example 3 — Most Recent Order
SELECT order_id, customer_id, order_date, total_amount FROM orders ORDER BY order_date DESC LIMIT 1;
Single most recent order
| order_id | customer_id | order_date | total_amount |
|---|---|---|---|
| 1007 | C01 | 2026-10-08 | 490 |
OFFSET — Skip Rows for Pagination
OFFSET tells the database to skip a number of rows before starting to return results. Combined with LIMIT, this builds pagination — page 1, page 2, page 3, etc.
SELECT ... FROM table_name
ORDER BY column
LIMIT n -- rows per page
OFFSET skip_n; -- rows to skip (page - 1) * n
-- Products table — show 3 per page -- Page 1: rows 1-3 SELECT product_id, product_name, price FROM products ORDER BY price DESC LIMIT 3 OFFSET 0; -- Page 2: rows 4-6 (skip the first 3) SELECT product_id, product_name, price FROM products ORDER BY price DESC LIMIT 3 OFFSET 3; -- Page 3: rows 7-9 (skip the first 6) SELECT product_id, product_name, price FROM products ORDER BY price DESC LIMIT 3 OFFSET 6;
Page 1 (OFFSET 0)
| product_name | price |
|---|---|
| Ergonomic Chair | 8999 |
| USB-C Hub | 1499 |
| Desk Lamp | 1199 |
Page 2 (OFFSET 3)
| product_name | price |
|---|---|
| Wireless Mouse | 899 |
| HDMI Cable | 299 |
| Notebook A5 | 99 |
Pagination formula: for page number P with N rows per page, use
LIMIT N OFFSET (P-1)*N. Page 1 → OFFSET 0, Page 2 → OFFSET N, Page 3 → OFFSET 2N.Example 4 — LIMIT with WHERE
-- Top 2 cheapest Electronics products SELECT product_name, category, price FROM products WHERE category = 'Electronics' ORDER BY price ASC LIMIT 2;
| product_name | category | price |
|---|---|---|
| HDMI Cable | Electronics | 299 |
| Wireless Mouse | Electronics | 899 |
SQL Server: SELECT TOP
-- SQL Server syntax — TOP goes after SELECT, before column list SELECT TOP 3 product_name, price FROM products ORDER BY price DESC; -- SQL Server also supports TOP with PERCENT SELECT TOP 50 PERCENT product_name, price FROM products ORDER BY price DESC; -- Returns the 4 most expensive (50% of 7 rows rounded up)
✏️ Practice Exercises
1. Return the top 2 orders with the highest total_amount.
2. Show products 4 to 6 when sorted alphabetically by product_name. (Hint: use LIMIT with OFFSET)
3. Return the 3 most recently signed-up customers.
Show Answers
-- 1. SELECT * FROM orders ORDER BY total_amount DESC LIMIT 2; -- 2. SELECT product_name FROM products ORDER BY product_name ASC LIMIT 3 OFFSET 3; -- 3. SELECT customer_name, signup_date FROM customers ORDER BY signup_date DESC LIMIT 3;