FETCH

SQL · Limiting Rows

Theory

OFFSET ... FETCH is the SQL-standard way to limit rows. OFFSET m ROWS skips the first m rows of the sorted result and FETCH NEXT n ROWS ONLY returns the following n. FETCH FIRST and FETCH NEXT mean the same thing, as do ROW and ROWS. Page 3 with 10 rows per page is OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY.

It is supported by PostgreSQL, Oracle, Db2 and SQL Server (where it needs an ORDER BY). MySQL and SQLite use LIMIT n OFFSET m for the same job. Some engines also support FETCH FIRST n ROWS WITH TIES, which keeps extra rows that tie with the last one on the ORDER BY value. Because the engine still has to walk past the skipped rows, very large offsets get slow.

Why we use it

Applications show long lists one page at a time. OFFSET/FETCH expresses "skip these, give me the next page" in portable, standard syntax that works across several major engines.

When to use it

Use OFFSET/FETCH for pagination and top-N queries when you want standard SQL or work on Oracle or SQL Server. On MySQL or SQLite use LIMIT/OFFSET. For very deep pages prefer keyset paging (WHERE id > last_seen_id).

How it works

  1. Sort the result with ORDER BY
  2. Skip the first m rows (OFFSET m ROWS)
  3. Return the next n rows (FETCH NEXT n ROWS ONLY)
  4. WITH TIES requested? Yes: Also keep rows tied with the last one. No: Stop exactly at n rows.
  5. Return the page

Syntax

SELECT columns FROM table
OFFSET m ROWS FETCH NEXT n ROWS ONLY;

Example

SELECT * FROM employees
OFFSET 10 ROWS FETCH NEXT 5 ROWS ONLY;

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