urQL/docs/ref-ch05-query.md

144 lines
4.6 KiB
Markdown
Raw Normal View History

2023-05-18 05:51:26 +03:00
# QUERY
2022-10-31 05:52:27 +03:00
2022-08-10 23:23:12 +03:00
```
<query> ::=
2023-05-15 17:39:13 +03:00
[ FROM <table-set> [ [AS] <alias> ]
2023-05-12 00:30:15 +03:00
{ JOIN | LEFT JOIN | RIGHT JOIN | OUTER JOIN }
2023-05-15 17:39:13 +03:00
<table-set> [ [AS] <alias> ]
2023-05-12 00:30:15 +03:00
ON <predicate>
[ ...n ]
2023-05-15 17:39:13 +03:00
| CROSS JOIN <table-set> [ [AS] <alias> ]
2023-02-10 23:29:54 +03:00
]
2023-05-23 00:08:37 +03:00
[ { SCALAR <scalar> [ AS ] <scalar-function> } [ ...n ] ]
2023-02-10 23:29:54 +03:00
[ WHERE <predicate> ]
2023-05-15 17:39:13 +03:00
[ GROUP BY { <qualified-column> | <column-alias> | <column-ordinal> }
[ ,...n ]
2023-02-10 23:29:54 +03:00
[ HAVING <predicate> ]
]
SELECT [ TOP <n> ] [ BOTTOM <n> ] [ DISTINCT ]
{ * | { [<ship-qualifer>]<table-view> | <alias> }.*
2023-03-16 23:49:28 +03:00
| <expression> [ [ AS ] <column-alias> ]
2023-02-10 23:29:54 +03:00
} [ ,...n ]
2023-05-12 00:30:15 +03:00
[ ORDER BY
{
{ <qualified-column> | <column-alias> | <column-ordinal> } { ASC | DESC }
} [ ,...n ]
2023-02-10 23:29:54 +03:00
]
2022-08-10 23:23:12 +03:00
```
2023-02-15 01:22:54 +03:00
`JOIN` Inner join returns all matching pairs of rows.
2023-05-12 00:30:15 +03:00
`LEFT JOIN` Left outer join returns all rows from the left table not meeting the join condition long with all matching pairs of rows.
`RIGHT JOIN` Right outer join returns all rows from the right table not meeting the join condition long with all matching pairs of rows.
`OUTER JOIN` Full outer join returns matching pairs of rows as well as all rows from both tables not meeting the join condition.
2023-02-15 01:22:54 +03:00
`CROSS JOIN` Cross join is a cartesian join of two tables.
2022-10-31 05:52:27 +03:00
Cross database joins are allowed, but not cross ship joins.
2023-02-15 01:22:54 +03:00
`HAVING <predicate>` any column reference in the predicate must be one of the grouping columns or be contained in an aggregate function.
2023-02-08 19:56:24 +03:00
`SELECT ... INTO` targets an existing table not otherwise in the query, and completes the command.
2022-10-31 05:52:27 +03:00
2023-03-05 01:51:02 +03:00
Do not use `ORDER BY` in Common Table Expressions (CTE, WITH clause) or in any query manipulated by set operators prior to the last of the queries, except when `TOP` or `BOTTOM` is specified.
2022-10-31 05:52:27 +03:00
2022-08-10 23:23:12 +03:00
```
2023-01-05 21:25:45 +03:00
<predicate> ::=
2022-10-31 05:52:27 +03:00
{ [ NOT ] <predicate> | [ ( ] <simple-predicate> [ ) ] }
[ { { AND | OR } [ NOT ] { <predicate> | [ ( ] <simple-predicate> [ ) ] }
2022-08-10 23:23:12 +03:00
[ ...n ]
]
```
2022-08-06 17:06:18 +03:00
2022-08-10 23:23:12 +03:00
```
<simple-predicate> ::=
{ expression <binary-operator> expression
2023-02-17 22:07:58 +03:00
| expression [ NOT ] EQUIV expression
2022-08-10 23:23:12 +03:00
| expression [ NOT ] IN
{ <single-column-query> | ( <value> ,...n ) }
| expression <inequality operator> { ALL | ANY} ( <single-column-query> )
2023-02-17 22:07:58 +03:00
| expression [ NOT ] BETWEEN expression [ AND ] expression
| [ NOT ] EXISTS { <column value> | <single-column-query> } }
2022-08-10 23:23:12 +03:00
```
2023-02-16 22:49:29 +03:00
Since nullable table columns are not allowed, `NOT EXISTS` can only yield `true` on the column of an outer join that is not in a returned row or a `<scalar-query>` that returns nothing. `NULL` is a marker for this case.
2023-02-17 22:07:58 +03:00
`[ NOT ] EQUIV` is a binary operator like [ NOT ] equals `<>`, `=` except comparing two `NOT EXISTS` yields true.
2023-02-16 22:49:29 +03:00
`<single-column-query>` is defined in a CTE and must return only one column.
2022-08-06 17:06:18 +03:00
2023-02-17 22:07:58 +03:00
```
<binary-operator> ::=
{ = | <> | != | > | >= | !> | < | <= | !< }
```
Whitespace is not required between operands and binary-operators, except when the left operand is a numeric literal, in which case whitespace is required.
2022-10-05 23:00:47 +03:00
```
<scalar-function> ::=
2023-01-05 21:25:45 +03:00
IF <predicate> THEN { <expression> | <scalar-function> }
2022-10-31 05:52:27 +03:00
ELSE { <expression> | <scalar-function> } ENDIF
2022-10-05 23:00:47 +03:00
| CASE <expression>
2023-01-05 21:25:45 +03:00
WHEN { <expression> | <predicate> }
2022-10-31 05:52:27 +03:00
THEN { <expression> | <scalar-function> } [ ...n ]
2022-10-05 23:00:47 +03:00
[ ELSE { <expression> | <scalar-function> } ]
END
| COALESCE ( <expression> [ ,...n ] )
2023-03-05 01:51:02 +03:00
| <arithmetic>
| <bitwise>
2023-02-16 22:49:29 +03:00
| <predicate>
2023-03-05 01:51:02 +03:00
| <boolean>
| <scalar>
2022-10-05 23:00:47 +03:00
```
2023-01-05 21:25:45 +03:00
If a `CASE` expression uses `<predicate>`, the expected boolean (or loobean) logic applies.
2022-10-31 05:52:27 +03:00
If it uses `<expression>` `@`0 is treated as false and any other value as true (not loobean).
`COALESCE` returns the first `<expression>` in the list that exists where not existing occurs when selected `<expression>` value is not returned due to `LEFT` or `RIGHT JOIN` not matching.
2022-10-05 23:00:47 +03:00
2023-03-05 01:51:02 +03:00
See CH 8 Functions for full documentation on Scalars.
2022-08-10 23:23:12 +03:00
```
<expression> ::=
2023-03-16 23:49:28 +03:00
{ <qualified-column>
| <constant>
2023-05-23 00:08:37 +03:00
| <scalar>
| <scalar-query>
2023-05-23 00:08:37 +03:00
| <aggregate-function>( { <column> | <scalar> } )
2022-08-10 23:23:12 +03:00
}
```
`<scalar-query>` is defined in a CTE and must return only one column. The first returned value is accepted and subsequent values ignored.
2022-08-06 17:06:18 +03:00
2023-02-16 22:49:29 +03:00
```
<aggregate-function> ::=
{ AVG | MAX | MIN | SUM | COUNT | AND | OR | <user-defined> }
```
2022-10-31 05:52:27 +03:00
```
2022-11-23 22:35:25 +03:00
<column> ::=
2023-05-18 05:51:26 +03:00
{ <qualified-column>
2022-10-31 05:52:27 +03:00
| <column-alias>
| <constant> }
```
2022-10-05 23:00:47 +03:00
```
2023-01-05 21:25:45 +03:00
<qualified-column> ::=
2023-05-23 00:08:37 +03:00
[ [ <ship-qualifer> ]<table-view> | <alias> ].<column>
2023-05-18 05:51:26 +03:00
```
2023-05-22 07:24:31 +03:00
## API
2022-10-05 23:00:47 +03:00
```
2023-05-18 05:51:26 +03:00
+$ query
$:
%query
from=(unit from)
scalars=(list scalar-function)
predicate=(unit predicate)
group-by=(list grouping-column)
having=(unit predicate)
selection=select
order-by=(list ordering-column)
==
```
2023-05-22 07:24:31 +03:00
## Arguments
** **
2023-05-18 05:51:26 +03:00
## Exceptions