# QUERY The `` statement provides a means to create ``s derived from persisted and/or cached ``s and/or constants. Data rows can be joined based on predicates, specific columns can be selected, and the resulting rows can be filtered by predicate. The full syntax involves complex manipulations at the row level through scalar functions, data aggregation across preliminary rows via aggregate functions, filtering by aggregation, and row ordering. NOTE: scalar and aggregate functions are currently under development and not available. Also, these are subject to change. ``` ::= [ FROM [ [AS] ] { { JOIN | LEFT JOIN | RIGHT JOIN | OUTER JOIN } [ [AS] ] ON } [ ...n ] | CROSS JOIN [ [AS] ] ] [ { SCALAR [ AS ] } [ ...n ] ] [ WHERE ] [ GROUP BY { | | } [ ,...n ] [ HAVING ] ] SELECT [ TOP ] [ BOTTOM ] { * | { [] | }.* | [ [ AS ] ] } [ ,...n ] [ ORDER BY { { | | } { ASC | DESC } } [ ,...n ] ] ``` `JOIN` is an inner join returning all matching pairs of rows. `LEFT JOIN` is a left outer join returning all rows from the left table not meeting the join condition, along with all matching pairs of rows. `RIGHT JOIN` is a right outer join returning all rows from the right table not meeting the join condition, along with all matching pairs of rows. `OUTER JOIN` is a full outer join returning matching pairs of rows, as well as all rows from both tables not meeting the join condition. `CROSS JOIN` is a cartesian join of two tables. Cross database joins are permitted, but not cross ship joins. `HAVING ` filters aggregated rows returned from the ``. The column references in the predicate must be either one of the grouping columns or be contained in an aggregate function. Avoid using `ORDER BY` in CTEs or in any query prior to the last step in a ``, unless required by `TOP` or `BOTTOM` specified in the `SELECT` statement or, in the case of CTEs, it is used in an `` expecting a scalar result. ``` ::= { [ NOT ] | [ ( ] [ ) ] } [ { { AND | OR } [ NOT ] { | [ ( ] [ ) ] } [ ...n ] ] ``` ``` ::= { expression expression | expression [ NOT ] EQUIV expression | expression [ NOT ] IN { | ( ,...n ) } | expression { ALL | ANY} { ( ) | ( ,...n ) } | expression [ NOT ] BETWEEN expression [ AND ] expression | [ NOT ] EXISTS { | } } ``` When applied to a column `EXISTS` tests whether the returned `` includes the required column. In the case of ``, it tests whether a CTE returns any rows. `[ NOT ] EQUIV` is a binary operator, similar to (not) equals `<>`, `=`. However, comparing two `NOT EXISTS` yields true. `` is a CTE that selects for one column. Depending on whether the operator expects a set or a value, it operates on the entire result set or on the first row returned, respectively. If the CTE is not ordered, results may be unpredictable. ``` ::= { = | <> | != | > | >= | !> | < | <= | !< | EQUIV | NOT EQUIV} ``` Whitespace is not required between operands and binary-operators, except when the left operand is a numeric literal, in which case whitespace is required. `` is any `` other than equality and `EQUIV`. ``` ::= IF THEN { | } ELSE { | } ENDIF | CASE WHEN { | } THEN { | } [ ...n ] [ ELSE { | } ] END | COALESCE ( [ ,...n ] ) | | | | | ``` 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). (NOTE: This is preliminary design subject to change.) `COALESCE` returns the first `` in the list that exists. Non-existence occurs when a selected `` value is not returned due to an outer join not matching or `` not returning rows. See CH 8 Functions for full documentation on Scalars. ``` ::= { | | | | ( { | } ) } ``` `` is a CTE that returns only one column. The first returned value is accepted and subsequent values ignored. Ordering the CTE may be required for predictable results. ``` ::= { AVG | MAX | MIN | SUM | COUNT | AND | OR | } ``` ``` ::= { | | } ``` ``` ::= [ [ ] | ]. ``` ## API ``` +$ 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) == ``` ## Arguments **` [ [AS] ]`** Any valid ``. `` allows short-hand reference to the `` in the `SELECT` clause and subsequent ``. **`{ | | }`** Used to select columns for ordering and grouping. ``s are 1-based. **`[ TOP ] [ BOTTOM ]`** `SELECT` only the first and/or last `n` rows returned by the rest of the query. If the result set is less than `n`, the entire set of rows is returned. `TOP` and `BOTTOM` require the presence of an `ORDER BY` clause. ## Remarks The `SELECT` clause may choose columns from a single CTE, in which case the `FROM` clause is absent. It may also choose only constants and `SCALAR` functions on constants, in which case it returns a result set of one row. The simplest possible query is `SELECT 0`. `` alone does not change the Obelisk agent state. ## Produced Metadata `@@ROWCOUNT` returns the total number of rows returned. ## Exceptions Provided `` attempts execution (i.e., syntax and internal consistency checks pass), the only exceptions possible are performance-related, such as timeouts and memory constraints.