2022-10-31 05:52:27 +03:00
# Query
2022-08-10 23:23:12 +03:00
```
< query > ::=
2022-09-25 18:52:01 +03:00
[WITH (< query > ) AS < alias > [ ,...n ] ]
2022-10-31 05:52:27 +03:00
[ { ]
2022-09-25 18:52:01 +03:00
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 > ]
2023-01-05 21:25:45 +03:00
ON < predicate >
2022-10-05 23:00:47 +03:00
} [ ...n ]
2022-09-25 18:52:01 +03:00
| CROSS JOIN
2022-10-31 05:52:27 +03:00
[ < ship-qualifer > ]< table-view > [ [AS] < alias > ]
2022-08-10 23:23:12 +03:00
]
2023-01-05 21:25:45 +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-10-31 05:52:27 +03:00
{ * | { { [<ship-qualifer>]<table-view> | <alias> }.*
| { < qualified-column > | < constant > } [ [ AS ] < column-alias > ]
| < scalar-name >
| < aggregate-name > ( { < column > | < scalar-name > } )
} [ ,...n ]
2022-08-10 23:23:12 +03:00
}
2023-01-05 21:25:45 +03:00
[ GROUP BY { < qualified-column > | < column-alias > | < column-ordinal > } [ ,...n ]
2022-10-31 05:52:27 +03:00
[ HAVING < predicate > ] ]
2023-01-05 21:25:45 +03:00
[ ORDER BY { { < qualified-column > | < column-alias > | < column-ordinal > }
2022-10-31 05:52:27 +03:00
[ ASC | DESC ] } [ ,...n ] ]
2022-10-16 22:09:53 +03:00
[ INTO < table > ]
2023-01-05 21:25:45 +03:00
[ { UNION
| COMBINE
| EXCEPT
| INTERSECT
| DIVIDED BY [ WITH REMAINDER ]
2022-10-31 05:52:27 +03:00
}
< query > ] [ } ] [ ...n ]
2022-11-23 22:35:25 +03:00
[ AS OF { Now
| < timestamp >
| n { SECONDS | MINUTES | HOURS | DAYS | WEEKS | MONTHS | YEARS } AGO
| < inline-scalar >
} ]
2022-08-10 23:23:12 +03:00
```
2022-10-31 05:52:27 +03:00
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 `{ ... }` .
2022-08-06 17:06:18 +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
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
2023-01-12 00:09:20 +03:00
{ < single-column-query > | ( < value > ,...n ) }
| expression < inequality operator > { ALL | ANY} ( < single-column-query > )
| [ NOT ] EXISTS { < column value > | < single-column-query > } }
2022-08-10 23:23:12 +03:00
```
2022-10-31 05:52:27 +03:00
`DISTINCT FROM` is like equals `=` except comparing two `NOT EXISTS` yields false.
2023-01-12 00:09:20 +03:00
`<single-column-query>` is defined in a CTE and must return only one column.
2022-08-06 17:06:18 +03:00
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 ] )
| BEGIN < arithmetic on expressions and scalar functions > END
| *hoon (TBD)
```
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
2022-08-10 23:23:12 +03:00
```
< expression > ::=
2022-10-31 05:52:27 +03:00
{ < column >
2022-08-10 23:23:12 +03:00
| < scalar-function >
2023-01-12 00:09:20 +03:00
| < scalar-query >
2022-10-16 22:09:53 +03:00
| < aggregate-name > ( { < column > | < scalar-name > } )
2022-08-10 23:23:12 +03:00
}
```
2023-01-12 00:09:20 +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
2022-10-31 05:52:27 +03:00
```
2022-11-23 22:35:25 +03:00
< column > ::=
2022-10-31 05:52:27 +03:00
{ [ < qualified-column >
| < column-alias >
| < constant > }
```
2022-08-10 23:23:12 +03:00
```
< binary-operator > ::=
{ = | < > | != | > | >= | !> | < | < = | !< }
```
2023-01-05 21:25:45 +03:00
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
```
2023-01-05 21:25:45 +03:00
< qualified-column > ::=
2022-10-22 01:37:24 +03:00
[ [ < ship-qualifer > ]< table-view > | < alias > } ].< column-name >
2022-10-05 23:00:47 +03:00
```