SQL OFFSET

Skips a number of rows before starting to return results — the tool that makes pagination possible.

What & Why

OFFSET n skips the first n rows of the (sorted) result before returning anything. Paired with LIMIT, it's how "page 2 of results" gets built.

See How It Works

BUSINESS QUESTION

Skip the two highest-spend campaigns and show the remaining rows.

idnamechannelspendstart_dateend_datestatustarget_segment
1Spring Launchgoogle_ads55000.002024-01-152024-03-31activesmb
2Retention Webinaremail45000.002024-02-102024-04-15activeenterprise
3Finance Retargetinglinkedin50000.002024-03-122024-05-31activeenterprise
4Enterprise Searchgoogle_ads60000.002024-04-012024-06-30activeenterprise
Sort first, then skip 2Step 1 of 2
ORDER BY spend DESC OFFSET 2
-- ORDER BY spend DESC (no OFFSET yet)
positionnamechannelspend
1Enterprise Searchgoogle_ads60,000
2Spring Launchgoogle_ads55,000
3Finance Retargetinglinkedin50,000
4Retention Webinaremail45,000

The campaigns are sorted by spend, highest first.

EXAMPLE QUERY
SELECT name, spend FROM marketing.campaigns
ORDER BY spend DESC
OFFSET 2;
Now You Try

Practice this concept

Marketing wants the second page of ten leads when newest leads are shown first.

Available schema
marketing

Prefix tables with marketing.table_name.

idsourcelead_scorecreated_at
marketing.leads
ColumnType
idinteger
campaign_idinteger
emailtext
created_attimestamp with time zone
qualified_attimestamp with time zone
converted_attimestamp with time zone
lead_scoreinteger
sourcetext
countrytext
archive_statustext
query.sql
Beginner business practice

Sign up free to try it on a real business scenario