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.
| id | name | channel | spend | start_date | end_date | status | target_segment |
|---|---|---|---|---|---|---|---|
| 1 | Spring Launch | google_ads | 55000.00 | 2024-01-15 | 2024-03-31 | active | smb |
| 2 | Retention Webinar | 45000.00 | 2024-02-10 | 2024-04-15 | active | enterprise | |
| 3 | Finance Retargeting | 50000.00 | 2024-03-12 | 2024-05-31 | active | enterprise | |
| 4 | Enterprise Search | google_ads | 60000.00 | 2024-04-01 | 2024-06-30 | active | enterprise |
Sort first, then skip 2Step 1 of 2
ORDER BY spend DESC OFFSET 2
-- ORDER BY spend DESC (no OFFSET yet)| position | name | channel | spend |
|---|---|---|---|
| 1 | Enterprise Search | google_ads | 60,000 |
| 2 | Spring Launch | google_ads | 55,000 |
| 3 | Finance Retargeting | 50,000 | |
| 4 | Retention Webinar | 45,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
marketingPrefix tables with marketing.table_name.
idsourcelead_scorecreated_atmarketing.leads| Column | Type |
|---|---|
| id | integer |
| campaign_id | integer |
| text | |
| created_at | timestamp with time zone |
| qualified_at | timestamp with time zone |
| converted_at | timestamp with time zone |
| lead_score | integer |
| source | text |
| country | text |
| archive_status | text |
query.sql
Sign up free to try it on a real business scenario