AI Hints
Homeโ€บSQL Tutorialโ€บSorting Results
SQL ยท Lesson 19 of 126

Top N and Paging

The newest, the next three.

โฑ 2 min readโ–ถ Video at 26:33๐ŸŽ“ Free
Watch this lesson in the SQL courseWatch on YouTube โ†—Full SQL course

Two everyday questions now. Who are my three newest customers? And, show me the next three after that. These are top N, and paging, and you'll see them everywhere, from leaderboards to search results.

Top N and Paging
From the video: Top N and Paging
  • Who joined most recently?
  • Show the next page
  • Leaderboards and search results

The newest customers

The joined column holds the date each customer signed up. Sorting by it, newest first, then limiting to three, gives the newest customers. Run it. Lina Dubois joined most recently, in June twenty twenty five.

query.sqlsql
SELECT first_name, last_name, joined
FROM customers
ORDER BY joined DESC
LIMIT 3;
Output
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ first_name โ”‚ last_name โ”‚   joined   โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Lina       โ”‚ Dubois    โ”‚ 2025-06-19 โ”‚
โ”‚ Will       โ”‚ Haddad    โ”‚ 2025-06-15 โ”‚
โ”‚ Gita       โ”‚ Brown     โ”‚ 2025-06-12 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The next page

Offset skips some rows before limit starts counting. Limit three, offset three, skips the top three, then takes the next three. That's page two. Run it. These are the fourth, fifth, and sixth most expensive books.

query.sqlsql
SELECT title, price
FROM books
ORDER BY price DESC
LIMIT 3 OFFSET 3;
Output
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚       title       โ”‚ price โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Spice Roads       โ”‚ 27.99 โ”‚
โ”‚ The Quiet Kitchen โ”‚ 24.99 โ”‚
โ”‚ The Pricing Game  โ”‚ 23.99 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Running low

Your turn. Helen worries about books running out. Show the title and stock of the three books with the lowest stock. Pause and try. Here's a solution. Order by stock, smallest first, limit three. The Quiet Kitchen is completely sold out.

โœ๏ธ Exercise: Running low

Show the 3 books with the lowest stock

Show hint

ORDER BY stock, then LIMIT

Show solution
solution.sqlsql
SELECT title, stock
FROM books
ORDER BY stock
LIMIT 3;
Output
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚       title       โ”‚ stock โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ The Quiet Kitchen โ”‚ 0     โ”‚
โ”‚ Spice Roads       โ”‚ 2     โ”‚
โ”‚ Data for Everyone โ”‚ 3     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

Order by and limit give you a top N. Offset gives you the next page. You've finished the first module. Next, we'll make new columns with calculations.

Key takeaways
  • ORDER BY + LIMIT = top N
  • OFFSET skips rows
  • Page two: LIMIT 3 OFFSET 3
Next up: Naming Columns with AS โ€” friendly column names