# Query ``` ::= [WITH () AS [ ,...n ] ] [ { ] FROM [ ] [ [AS] ] [ { { JOIN | LEFT JOIN | RIGHT JOIN | OUTER JOIN [ALL] } [ ] [ [AS] ] ON } [ ...n ] | CROSS JOIN [ ] [ [AS] ] ] [ { SCALAR [ AS ] } [ ...n ] ] [ WHERE ] SELECT [ TOP ] [ BOTTOM ] [ DISTINCT ] { * | { { [] | }.* | { | } [ [ AS ] ] | | ( { | } ) } [ ,...n ] } [ GROUP BY { | | } [ ,...n ] [ HAVING ] ] [ ORDER BY { { | | } [ ASC | DESC ] } [ ,...n ] ] [ INTO ] [ { UNION | COMBINE | EXCEPT | INTERSECT | DIVIDED BY [ WITH REMAINDER ] } ] [ } ] [ ...n ] [ AS OF { Now | | n { SECONDS | MINUTES | HOURS | DAYS | WEEKS | MONTHS | YEARS } AGO | } ] ``` Cross database joins are allowed, but not cross ship joins. `SELECT ... INTO` targets an existing table not otherwise in the query. Do not use `ORDER BY` in Common Table Experessions (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. Set operators apply the previous result set to the next query unless otherwise qualified by brackets `{ ... }`. ``` ::= { [ NOT ] | [ ( ] [ ) ] } [ { { AND | OR } [ NOT ] { | [ ( ] [ ) ] } [ ...n ] ] ``` ``` ::= { expression expression | expression [ NOT ] BETWEEN expression [ AND ] expression | expression IS [ NOT ] DISTINCT FROM expression | expression [ NOT ] IN { | ( ,...n ) } | expression { ALL | ANY} ( ) | [ NOT ] EXISTS { | } } ``` `DISTINCT FROM` is like equals `=` except comparing two `NOT EXISTS` yields false. `` is defined in a CTE and must return only one column. ``` ::= IF THEN { | } ELSE { | } ENDIF | CASE WHEN { | } THEN { | } [ ...n ] [ ELSE { | } ] END | COALESCE ( [ ,...n ] ) | BEGIN END | *hoon (TBD) ``` If a `CASE` expression uses ``, the expected boolean (or loobean) logic applies. If it uses `` `@`0 is treated as false and any other value as true (not loobean). `COALESCE` returns the first `` in the list that exists where not existing occurs when selected `` value is not returned due to `LEFT` or `RIGHT JOIN` not matching. ``` ::= { | | | ( { | } ) } ``` `` is defined in a CTE and must return only one column. The first returned value is accepted and subsequent values ignored. ``` ::= { [ | | } ``` ``` ::= { = | <> | != | > | >= | !> | < | <= | !< } ``` Whitespace is not required between operands and binary-operators, except when the left operand is a numeric literal, in which case whitespace is required. ``` ::= [ [ ] | } ]. ```