SQL LIMIT

LIMIT restricts how many rows a query returns. Combined with ORDER BY it answers "top N" questions: the five most expensive products, the ten newest customers.

OFFSET skips rows before LIMIT starts counting, which is how simple pagination works.

Syntax

SELECT columns FROM table_name
ORDER BY column DESC
LIMIT n OFFSET m;

Try it yourself

Run the example below on a real PostgreSQL database, then change it and run it again. Nothing to install, no sign-up.

limit.sql · PostgreSQL
Loading editor…

Examples

1. Top 5

SELECT name, price FROM products ORDER BY price DESC LIMIT 5;

The five most expensive products.

2. Second page

SELECT name, price FROM products ORDER BY price DESC LIMIT 5 OFFSET 5;

Products ranked 6 to 10.

Common mistakes

  • Using LIMIT without ORDER BY and expecting a meaningful "top".
  • Expecting LIMIT to work in SQL Server. There it is TOP or OFFSET ... FETCH.

Practice: test yourself

This challenge is from our interactive course. Write the query and press Check answer.

Challenge · 10 pts

Show the 5 most expensive products: name and price, highest price first.

challenge.sql · PostgreSQL
Loading editor…

FAQ

What is the SQL Server equivalent of LIMIT?

SELECT TOP 5 ... or ORDER BY ... OFFSET 0 ROWS FETCH NEXT 5 ROWS ONLY.

Learn SQL step by step

65 hands-on exercises, streaks, a leaderboard and a certificate.

Start free →