SQL LIMIT
Caps how many rows come back — almost always paired with ORDER BY to mean something specific.
What & Why
LIMIT n returns at most n rows. On its own it just truncates whatever order the rows happened to arrive in — paired with ORDER BY first, it becomes "top N."
See How It Works
BUSINESS QUESTION
The two highest-spend campaigns.
| 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 cut off at 2Step 1 of 2
ORDER BY spend DESC LIMIT 2
-- ORDER BY spend DESC (no LIMIT 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
LIMIT 2;Sign up free to try it on a real business scenario