Skip to content

Commit fba0007

Browse files
Document LIMIT and COUNT (#178)
Closes #177.
1 parent 7e46931 commit fba0007

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

docs/sql/index.md

+31-2
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,15 @@ SELECT * FROM Inventory WHERE price > {X} AND amount < {Y}
157157
### SELECT
158158

159159
```ebnf
160-
SELECT projection FROM relation [ WHERE predicate ]
160+
SELECT projection FROM relation [ WHERE predicate ] [LIMIT NUM]
161161
```
162162

163163
The query languge is a strict superset of the subscription language.
164164
The main differences are seen in column projections and [joins](#from-clause).
165165

166166
The subscription api only supports `*` projections,
167-
but the query api supports individual column projections.
167+
but the query api supports both individual column projections,
168+
as well as aggregations in the form of `COUNT`.
168169

169170
The subscription api limits the number of tables you can join,
170171
and enforces index constraints on the join columns,
@@ -177,11 +178,16 @@ projection
177178
= '*'
178179
| table '.' '*'
179180
| projExpr { ',' projExpr }
181+
| aggExpr
180182
;
181183
182184
projExpr
183185
= column [ [ AS ] alias ]
184186
;
187+
188+
aggExpr
189+
= COUNT '(' '*' ')' [AS] alias
190+
;
185191
```
186192

187193
The `SELECT` clause determines the columns that are returned.
@@ -196,6 +202,16 @@ SELECT * FROM Inventory;
196202
SELECT item_name, price FROM Inventory
197203
```
198204

205+
It also allows for counting the number of input rows via the `COUNT` function.
206+
`COUNT` always returns a single row, even if the input is empty.
207+
208+
##### Example
209+
210+
```sql
211+
-- Count the items in my inventory
212+
SELECT COUNT(*) AS n FROM Inventory
213+
```
214+
199215
#### FROM Clause
200216

201217
```ebnf
@@ -219,6 +235,19 @@ WHERE product.name = {product_name}
219235

220236
See [Subscriptions](#where).
221237

238+
#### LIMIT clause
239+
240+
Limits the number of rows a query returns by specifying an upper bound.
241+
The `LIMIT` may return fewer rows if the query itself returns fewer rows.
242+
`LIMIT` does not order or transform its input in any way.
243+
244+
##### Examples
245+
246+
```sql
247+
-- Fetch an example row from my inventory
248+
SELECT * FROM Inventory LIMIT 1
249+
```
250+
222251
### INSERT
223252

224253
```ebnf

0 commit comments

Comments
 (0)