🔍
👶 Kids📝 Blog About Contact 🚀 Get Started Free

SQL – Limiting Rows

Control how many rows are returned using LIMIT (and TOP/FETCH in other databases).

In the real world, you rarely want all rows from a query. Dashboards show “top 10” lists. Pagination shows 20 results per page. APIs return batches of 100 records at a time. The LIMIT clause (SQLite/MySQL/PostgreSQL) restricts how many rows your query returns. Combined with ORDER BY, it becomes a powerful tool for ranking and leaderboard queries.

The OFFSET keyword pairs with LIMIT for pagination — LIMIT 10 OFFSET 20 means “skip the first 20 rows, then return the next 10” (page 3 of a 10-per-page list).

Database syntax differences

-- SQLite, MySQL, PostgreSQL
SELECT * FROM students LIMIT 5;

-- SQL Server
SELECT TOP 5 * FROM students;

-- Oracle / SQL Server 2012+
SELECT * FROM students
FETCH FIRST 5 ROWS ONLY;

Try It Yourself — Interactive SQL Editor

Edit the query below and click Run Query ▶ to see live results powered by SQLite running directly in your browser.

SQLite – edit & run
Results
← Click Run Query ▶ to see results

Key Points

  • LIMIT without ORDER BY returns arbitrary rows — always combine them
  • LIMIT + OFFSET enables efficient pagination
  • Performance tip: add indexes on ORDER BY columns for fast limits
  • LIMIT 1 is a common pattern to find the single best/worst record

Pro Tip from CodesCompiler: The best way to learn SQL is to break things intentionally — modify the query above, change the WHERE conditions, try different columns. Every error teaches you something the docs cannot.

In the next lesson, we continue exploring SQL’s powerful feature set to build your database mastery.