Warming up the neural circuits...
When you scroll through products on Amazon, you're seeing — only 20-50 items at a time. By the end of this module you will:
What it is: LIMIT restricts the number of rows returned by a query. Without it, a query returns all matching rows — which could be millions.
Why we use it: Applications display data in pages (pagination), show "top N" results, or need just a sample. LIMIT controls how much data is returned, improving performance and user experience.
When we use it: In paginated APIs, "top N" queries, random sampling, or any time you don't need all matching rows.
-- Show only the first 5 products
SELECT * FROM products
ORDER BY price DESC
LIMIT 5;Always ORDER BY before LIMIT. Without ORDER BY, "Page 1" and "Page 2" might contain the same items because the database doesn't guarantee a specific order.
What it is: OFFSET skips a specified number of rows before starting to return results. Combined with LIMIT, it implements pagination — showing a different "page" of results each time.
Why we use it: When a user clicks "Page 2" in a paginated list, OFFSET skips the rows from Page 1 and returns the next batch. This is the standard way to implement pagination in .
When we use it: In paginated APIs, paginated UIs, or any time you need to access a specific "page" of results.
-- Skip the first 5, show the next 5 (Page 2)
SELECT * FROM products
ORDER BY price DESC
LIMIT 5 OFFSET 5;
-- Skip the first 10, show the next 5 (Page 3)
SELECT * FROM products
ORDER BY price DESC
LIMIT 5 OFFSET 10;What it is: The pagination formula calculates the correct LIMIT and OFFSET values for any page number. It's a simple mathematical relationship: LIMIT S OFFSET (N - 1) * S where S is items per page and N is the page number.
Why we use it: Every paginated needs this formula to return the correct page. Without it, you'd have to manually calculate offsets for each page.
When we use it: In every paginated endpoint — user lists, product catalogs, search results, admin dashboards, etc.
To get Page N with S items per page:
LIMIT S OFFSET (N - 1) * S| Page | Formula | Result |
|---|---|---|
| Page 1 | LIMIT 10 OFFSET 0 | Items 1-10 |
| Page 2 | LIMIT 10 OFFSET 10 | Items 11-20 |
| Page 3 | LIMIT 10 OFFSET 20 | Items 21-30 |
| Page N | LIMIT 10 OFFSET (N-1) * 10 | Items (N-1)10+1 to N10 |
What it is: Using LIMIT without OFFSET returns just the first N rows. This is simpler than full pagination — you're not skipping rows, just taking a fixed number from the top.
Why we use it: When you only need a subset — "top 3 products", "latest 10 posts", "5 random users". It's also useful for quick data sampling during development.
When we use it: In "top N" queries, random sampling, limiting API response sizes, or quick data exploration.
-- Get the top 3 most expensive products
SELECT * FROM products
ORDER BY price DESC
LIMIT 3;
-- Get a random sample of 5 products
SELECT * FROM products
ORDER BY RANDOM()
LIMIT 5;What it is: FETCH FIRST N ROWS ONLY is the SQL standard equivalent of LIMIT. PostgreSQL supports both, but LIMIT is more commonly used in the PostgreSQL ecosystem.
Why we use it: If you're writing SQL that needs to work across multiple databases (PostgreSQL, MySQL, Oracle), FETCH is more portable. For PostgreSQL-only projects, LIMIT is preferred.
When we use it: When writing cross-database compatible SQL, or when following strict SQL standard conventions.
-- These are equivalent:
SELECT * FROM products ORDER BY price FETCH FIRST 5 ROWS ONLY;
SELECT * FROM products ORDER BY price LIMIT 5;OFFSET gets slower as the number increases. To skip 1,000,000 rows, Postgres still has to "read" them all before throwing them away. For very large datasets, senior engineers use Keyset Pagination (also called "seek method"):
-- Keyset pagination (much faster for large offsets)
-- Instead of: LIMIT 10 OFFSET 1000000
SELECT * FROM products
WHERE id > 1000000 -- last seen id
ORDER BY id
LIMIT 10;Using your courses table:
ORDER BY RANDOM() LIMIT 1Next up: You can read data — now let's modify it. In the next module, you'll learn UPDATE to change existing records.