# ORDER BY, LIMIT & OFFSET

> Sort results, break ties, choose where NULLs land, and fetch one page of rows with LIMIT and OFFSET.

Subject: SQL | Lesson 9 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/sql/order-by-limit-offset
Previous: [IN, BETWEEN & LIKE](https://codewithanimation.in/sql/in-between-and-like.md) | Next: [DISTINCT](https://codewithanimation.in/sql/distinct-values.md)

## Order and page a result

`ORDER BY` sorts the result. `ASC` (small to large) is the default, and `DESC` flips it. You can list several columns: later ones break ties in earlier ones, and each column has its own direction.

By default `NULL` counts as larger than any value. So NULLs come last in `ASC` order and first in `DESC` order. Add `NULLS FIRST` or `NULLS LAST` to choose.

`LIMIT` caps how many rows come back. `OFFSET` skips rows before the result starts. The skipped rows are removed first, then `LIMIT` counts.

## Lists need an order and a size

Without `ORDER BY`, PostgreSQL may return rows in any order, and that order can change from one run to the next. A leaderboard, a search page or a page of results all need an explicit order.

`LIMIT` and `OFFSET` turn a long result into pages, so a screen shows ten rows instead of ten thousand.

## Sort, break ties, take page 2

Page 2 of the leaderboard, three players per page, best score first, players with no points last:

```sql
SELECT name, points
FROM players
ORDER BY points DESC NULLS LAST, id
LIMIT 3 OFFSET 3;
```

The `id` after `points` is a tie-break. Without it, players with the same points could appear in a different order each time, and a player could be repeated or missed between pages.

## Common mistake: LIMIT without ORDER BY

`LIMIT 3` with no `ORDER BY` returns three rows, but which three is not defined. The result may change when the data or the query plan changes.

Paging only works when the order is fully decided, so end the `ORDER BY` with a unique column. Also remember that a large `OFFSET` can be slow, because the database still has to work out all the rows it skips.

## Tip: End every ORDER BY with a unique column

Add `id` (or another unique key) as the last sort column. It costs nothing and makes the order the same every time.

## Try it: Sort, tie-break and page the players

Choose a sort column, flip the direction, move the NULLs, add a tie-break and turn on `LIMIT` and `OFFSET`.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/order-by-limit-offset

## Check yourself

### Question 1

Where do NULL points appear in `ORDER BY points` (ascending) in PostgreSQL?

- A. Last, because NULL counts as larger than any value
- B. First, because NULL means nothing
- C. They are removed from the result

Correct answer: A

NULL is treated as larger than any value, so ASC puts it last and DESC puts it first. NULLS FIRST or NULLS LAST overrides this.

- Why not B: In PostgreSQL NULL sorts as the largest value. Some other databases differ.
- Why not C: ORDER BY never removes rows. Only WHERE does.

### Question 2

What does `ORDER BY team, points DESC` do?

- A. Sorts by team ascending, then by points descending inside each team
- B. Sorts both team and points descending
- C. Sorts by points first, then by team

Correct answer: A

Each column has its own direction, and later columns only break ties in earlier ones.

- Why not B: DESC only applies to the column it follows.
- Why not C: Columns are applied in the order you list them.

### Question 3

A page shows 3 rows. Which clause gives page 2 of a sorted result?

- A. LIMIT 3 OFFSET 3
- B. LIMIT 3 OFFSET 2
- C. LIMIT 6

Correct answer: A

OFFSET skips the rows of earlier pages, then LIMIT takes one page.

- Why not B: OFFSET counts rows to skip, so page 2 skips the first 3 rows, not 2.
- Why not C: That returns the first 6 rows, which includes page 1.

## Remember this

- ORDER BY sorts ASC by default, and each column can have its own direction.
- NULL counts as the largest value: last in ASC, first in DESC, unless NULLS FIRST or NULLS LAST is used.
- Without ORDER BY the order of rows is not promised, and LIMIT then returns an arbitrary few.
- For paging, end ORDER BY with a unique column so the order is the same every time.
