2022-08-10 23:23:12 +03:00
```
< query > ::=
2022-09-25 18:52:01 +03:00
[WITH (< query > ) AS < alias > [ ,...n ] ]
FROM [ < ship-qualifer > ]< table-view > [ [AS] < alias > ]
2022-09-13 23:29:49 +03:00
[ { { JOIN | LEFT JOIN | RIGHT JOIN | OUTER JOIN [ALL] }
2022-09-25 18:52:01 +03:00
[ < ship-qualifer > ]< table-view > [ [AS] < alias > ]
ON < predicate >
2022-10-05 23:00:47 +03:00
} [ ...n ]
2022-09-25 18:52:01 +03:00
| CROSS JOIN
2022-08-10 23:23:12 +03:00
]
2022-10-05 23:00:47 +03:00
[ { SCALAR < scalar-name > [ AS ] < scalar-function > } [ ...n ] ]
2022-08-10 23:23:12 +03:00
[ WHERE < predicate > ]
2022-10-13 18:51:22 +03:00
SELECT [ TOP < n > ] [ BOTTOM < n > ] [ DISTINCT ]
2022-08-10 23:23:12 +03:00
{ *
| {
2022-09-12 22:38:25 +03:00
{ [< ship-qualifer > ]< table-view > | < alias > }.*
| { < qualified-column > | < constant > } [ [ AS ] < column-alias > ]
2022-10-05 23:00:47 +03:00
| < scalar-name >
2022-08-10 23:23:12 +03:00
} [ ,...n ]
}
2022-09-12 22:38:25 +03:00
[ GROUP BY { < column > | < column-ordinal > } [ ,...n ]
[ HAVING < predicate > ]
[ AGGREGATE [ [ AS ] { [^..^]< column-name > | [^..^]< column-alias > | < ordinal > } ]
{ < expression > | (TBD) *hoon }
]
]
2022-08-10 23:23:12 +03:00
[ INTO < new-table > ]
2022-10-16 02:09:57 +03:00
[ ORDER BY { < column > | < column-ordinal > } [ ASC | DESC ] [ ,...n ] ]
2022-09-13 23:29:49 +03:00
[ { UNION
| COMBINE
2022-09-02 03:14:57 +03:00
| EXCEPT
| INTERSECT
| DIVIDED BY [ WITH REMAINDER ]
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
2022-09-25 18:52:01 +03:00
| expression [ NOT ] BETWEEN expression [ AND ] expression
2022-08-10 23:23:12 +03:00
| expression IS [ NOT ] DISTINCT FROM expression
| expression [ NOT ] IN
2022-09-25 18:52:01 +03:00
{ < cte-one-column-query > | ( < value > ,...n ) }
| expression < inequality-operator > { ALL | ANY} ( < cte-one-column-query > )
| [ NOT ] EXISTS { < column-value > | < cte-one-column-query > } }
2022-08-10 23:23:12 +03:00
```
2022-08-06 17:06:18 +03:00
2022-10-05 23:00:47 +03:00
```
< scalar-function > ::=
IF < predicate > THEN { < expression > | < scalar-function > } ELSE { < expression > | < scalar-function > } ENDIF
| CASE < expression >
WHEN { < expression > | < predicate > } THEN { < expression > | < scalar-function > } [ ...n ]
[ ELSE { < expression > | < scalar-function > } ]
END
| COALESCE ( < expression > [ ,...n ] )
| BEGIN < arithmetic on expressions and scalar functions > END
| *hoon (TBD)
```
2022-08-10 23:23:12 +03:00
```
< expression > ::=
{
constant
2022-10-05 23:00:47 +03:00
| < column >
2022-08-10 23:23:12 +03:00
| < scalar-function >
}
```
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-10-05 23:00:47 +03:00
```
< qualified-column > ::=
[ [ < ship-qualifer > ]< table-view > | < alias > } ].< column >
```
2022-08-10 23:23:12 +03:00
```
`< column > ::=
{ [ { < alias > . | < table-view > . } ]< column-name >
| < constant >
| < column-alias > }
```
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-09-12 22:38:25 +03:00
`ORDER BY` is not recommended in Common Table Experessions (CTE, WITH clause) or in any query joined by set operators prior to the last of the queries, except when `TOP` or `BOTTOM` is specified.
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.