urQL/docs/query.md

100 lines
2.9 KiB
Markdown
Raw Normal View History

2022-08-10 23:23:12 +03:00
```
<query> ::=
[WITH <common-table-expression> ]
FROM { [<ship-qualifer>]<table-view> [ [AS] <alias> ]
2022-09-02 03:14:57 +03:00
[ { JOIN | LEFT JOIN | RIGHT JOIN | OUTER JOIN [ALL] }
2022-08-10 23:23:12 +03:00
<ship-qualifer><table-view> [ [AS] <alias> ]
ON <predicate>
]
} [ ,...n ]
[ WHERE <predicate> ]
SELECT [ TOP <n> ] [ DISTINCT ]
{ *
| {
{ [<ship-qualifer>]<table-view> | <alias> }. ] *
| <expression> [ [ AS ] column_alias ]
| column_alias = expression
} [ ,...n ]
}
[ GROUP BY { <column> | <column-ordinal> } [ ,...n ] ]
[ HAVING <predicate> ]
[ INTO <new-table> ]
[ ORDER BY { <column> | <column-ordinal> } [ ,...n ] ]
2022-09-02 03:14:57 +03:00
[ { UNION [ WITH DUPS ]
| EXCEPT
| INTERSECT
| DIVIDED BY [ WITH REMAINDER ]
| CROSS JOIN [ ( { { 1 | 2 }:{ * | 1..n } } ) ] } <query> ] [ ...n ]
2022-08-10 23:23:12 +03:00
```
2022-08-06 17:06:18 +03:00
2022-08-10 23:23:12 +03:00
```
<predicate> ::=
{ [ NOT ] <predicate> | ( <simple-predicate> ) }
[ { { AND | OR } [ NOT ] { <predicate> | ( <simple-predicate> ) }
[ ...n ]
]
```
2022-08-06 17:06:18 +03:00
2022-08-10 23:23:12 +03:00
```
<simple-predicate> ::=
{ expression <binary-operator> expression
| expression [ NOT ] BETWEEN expression AND expression
| expression IS [ NOT ] NULL
| expression IS [ NOT ] DISTINCT FROM expression
| expression [ NOT ] IN
( { <one-column-query> ) | expression [ ,...n ] } )
| expression <binary-operator> { ALL | ANY} ( <one-column-query> )
| EXISTS ( <query> ) }
```
2022-08-06 17:06:18 +03:00
2022-08-10 23:23:12 +03:00
```
<expression> ::=
{
constant
| <scalar-function>
| <column>
| [ <unary-operator> ] <expression>
| <expression> <binary-operator> <expression>
}
```
2022-08-06 17:06:18 +03:00
2022-08-10 23:23:12 +03:00
```
<binary-operator> ::=
{ = | <> | != | > | >= | !> | < | <= | !< }
```
2022-08-10 21:45:17 +03:00
2022-08-10 23:23:12 +03:00
```
`<column> ::=
{ [ { <alias>. | <table-view>. } ]<column-name>
| <constant>
| <column-alias> }
```
```
<scalar-function> ::=
IF <predicate> THEN <expression> ELSE <expression> ENDIF
| CASE <expression>`
WHEN { <predicate> | <expression> } THEN <expression> [ ...n ]
[ ELSE <expression> ]
END
| COALESCE ( <expression> [ ,...n ] )
| native hoon (stretch goal)
```
2022-08-06 17:06:18 +03:00
Discussion:
2022-08-10 23:23:12 +03:00
Not shown in diagrams, parentheses distinguish order of operations for binary conjunctions `AND` and `OR`.
2022-08-06 17:06:18 +03:00
Set operators apply the previous result set to the next query unless otherwise qualified by parentheses.
2022-08-10 23:23:12 +03:00
2022-08-06 17:06:18 +03:00
`ORDER BY` is not allowed in Common Table Experessions (CTE, WITH clause) or in any query joined by set operators except for the last of the queries.
2022-08-10 21:45:17 +03:00
2022-08-06 17:06:18 +03:00
`SELECT INTO` targets an existing table not otherwise in the query.
2022-08-10 21:45:17 +03:00
`COALESCE` returns the first `<expression>` in the list that does not evaluate to `~` (in the case of unit) or not in the selected `<expression>` due to `LEFT` or `RIGHT JOIN`.
If a `CASE WHEN` expression is a `<predicate>`, the expected boolean (or loobean) logic applies. If it is a <expression> atom value 0 is treated as false and any other value as true (not loobean).
Cross database joins are allowed, but not cross ship joins.
2022-08-10 23:23:12 +03:00
`DISTINCT FROM` is like equals, `=`, except comparing two nulls will yield false.