LIMIT

SQL · Limiting Rows

Theory

LIMIT n tells the database to return at most n rows of the result. It is applied at the very end, after filtering, grouping and sorting, so ORDER BY salary DESC LIMIT 3 gives the three highest-paid employees. LIMIT n OFFSET m skips the first m rows and then returns n, which is the basic form of pagination.

Without ORDER BY, LIMIT returns an arbitrary set of rows, which may change between runs. LIMIT is not part of the SQL standard, so syntax varies: MySQL, PostgreSQL, SQLite and Snowflake use LIMIT; SQL Server uses SELECT TOP n; Oracle, Db2 and newer SQL Server/PostgreSQL support the standard FETCH FIRST n ROWS ONLY. Ties at the cut-off are broken arbitrarily unless you add a tie-breaker column to ORDER BY.

Why we use it

You rarely need every row: a top-10 list, a preview of a big table or one page of results is enough. LIMIT keeps output small, cuts network transfer and lets the engine stop early.

When to use it

Use LIMIT for top-N reports, quick previews and simple paging, always with ORDER BY when the choice of rows matters. Use TOP on SQL Server and FETCH FIRST for portable code. For deep pages, keyset paging (WHERE id > last_id) scales better than large OFFSETs.

How it works

  1. Query produces its full sorted result
  2. Is there an OFFSET m? Yes: Skip the first m rows. No: Start from the first row.
  3. Take rows one by one
  4. Have n rows been taken? Yes: Stop reading. No: Take the next row.
  5. Return at most n rows

Syntax

SELECT columns
FROM table
LIMIT n;

Example

SELECT * FROM employees ORDER BY salary DESC LIMIT 5;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic