---
title: ORDER BY, LIMIT and OFFSET
summary: Sorting on one or more features, ascending and descending, the original file order, spatial sequencing with GEOMETRY, and paging with LIMIT and OFFSET.
order: 50
see-also: [reference/bql/group-by, reference/bql/get, reference/bql/spatial-and-advanced]
---

# ORDER BY, LIMIT and OFFSET

`ORDER BY` sorts the records; `LIMIT` and `OFFSET` page through them. Both come after `GROUP BY` and before `USING`.

## Syntax {#syntax}

```text {#order-by-syntax}
ORDER BY <feature> [ASC|DESC] [, <feature> [ASC|DESC] ...]
ORDER BY ORIGINAL
ORDER BY GEOMETRY(<condition>)
LIMIT <n>
OFFSET <n>
```

## ORDER BY features {#features}

Each sort key is a plain feature (a path, or `BIN(...)`); aggregates, aliases and arithmetic are not sort keys. `ASC` is the default. Several keys sort by the first, then the second.

```bql {#order-by-basic}
GET qid, attribute.Name, aabb.height WHERE type = IFCDOOR ORDER BY aabb.height DESC, attribute.Name
```

```bql {#order-by-storey}
GET attribute.Name, attribute.Elevation WHERE type = IFCBUILDINGSTOREY ORDER BY attribute.Elevation
```

A grouped query cannot be ordered by its aggregates (`ORDER BY COUNT(qid)` is a syntax error); order the groups by their key or sort the records in the client.

## ORDER BY ORIGINAL {#original}

`ORDER BY ORIGINAL` returns the objects in the order they have in the model file. It is not available inside a nested relation query, where it raises [[error:BimWorksQueryError.ORDER_BY_ORIGINAL_ORDER_NOT_SUPPORTED]].

```bql {#order-by-original}
GET qid, attribute.Name WHERE type = IFCWALL ORDER BY ORIGINAL
```

## ORDER BY GEOMETRY {#geometry}

`ORDER BY GEOMETRY(<condition>)` sequences the result spatially: starting from the first object that matches the condition, each next record is the nearest remaining object by bounding-box centre. It exists for walk-throughs and sequences (BSL frames, animations) rather than for reports.

```bql {#order-by-geometry}
GET qid, attribute.Name WHERE type = IFCDOOR ORDER BY GEOMETRY(attribute.Name = "Main entrance")
```

## LIMIT {#limit}

`LIMIT n` keeps the first *n* records after sorting. `LIMIT 0` and `LIMIT -1` mean no limit (the validator suggests dropping the clause). A limit below -1 returns nothing, and the validator warns. Inside a nested relation query, `LIMIT` applies per parent object ([[bql:get#nested]]).

```bql {#limit}
GET qid, attribute.Name, aabb.height WHERE type = IFCWALL ORDER BY aabb.height DESC LIMIT 10
```

## OFFSET {#offset}

`OFFSET n` skips the first *n* records; with `LIMIT` it pages. A negative offset has no effect.

```bql {#offset}
GET qid, attribute.Name WHERE type = IFCWALL ORDER BY attribute.Name LIMIT 50 OFFSET 100
```

## Used in {#used-in}

- [[page:tutorials/bql/grouping-and-aggregates]] — sorted groups and the top-n idiom.
- [[page:how-to/query/run-a-bql-query]] — paging a large result.
