# The query pipeline

A query is a chain of stages checked word by word before anything is read, so a wrong stage is refused by name instead of answered wrongly.

A query is stages joined by pipes. The first stage reads records in. Each later stage filters, orders, bounds or finishes. Every word is checked against the served word table before a single record is read.

## Stages joined by pipes

**A pipeline**

```text
collection("patients") | sort(.admitted; desc) | limit(2)
```


This reads the patients collection, orders by admission date with the newest first, and keeps two.

## Checked before it runs

Before anything is read, the service checks every word against the [served words](/haematite/query-language/stages/). A word that is not served, a stage in the wrong place, or an argument of the wrong form is refused.

The refusal carries a code, a sentence, and the span of text that caused it. It is part of the language, not an afterthought.

## Quoted jq where you need it

Anything the stages do not cover goes in a quoted jq expression as the last stage. Inside the quotes, jq's own keywords are available.

## Bounded pages

Answers come in pages bounded by rows and bytes. Every page of one query is read at the version the first page saw, so a long answer stays consistent while the branch moves.

> A hospital example throughout
> The examples on this site use patients, wards and nurses because that is the shape of data a store like this is asked to hold. The words are the language's; the data is a sample.
