SELECT statement

Queries the contents of a public window.

Syntax

simple [ { {UNION [ALL] | INTERSECT | EXCEPT } simple } [...] ] [ORDER BY column [ ASC[ENDING] | DESC[ENDING] ] [, ...]] [LIMIT {limit [OFFSET offset | {offset, limit} ]

Components

simple

A simple select statement. See simple for more information.

column

The name of a column by which to sort the output.

limit

The maximum number of rows to publish. If negative or omitted, place no limit on the number of rows published.

offset

The number of rows of the result to skip before publishing.

simple

select_clause from_clause [WHERE expression] [GROUP BY expression [, ...]] [HAVING expression]

Components

select_clause

The select list. See select_clause for more information.

from_clause

The data sources. See from_clause for more information.

expression

An expression specifying selection, grouping, or filtering conditions. See Usage for more information.

select_clause

SELECT [ ALL | DISTINCT ] { expression [[AS] alias] } [, ...]

Components

expression

A SQL-92 expression.

alias

An alias for the output.

from_clause

FROM single | inner | outer

Components

single

A single data source expression. See single for more information.

inner

An inner join. See inner for more information.

outer

An outer join. See outer for more information.

single

{ pub_window [AS alias] } | { ( sub_select ) [AS alias] }

Components

pub_window

The name of a public window. If the window is in a submodule, you must identify the submodule by specifying path/submodule/window, where path includes the names of any additional parent submodules, separated by slashes, and submodule is the name of the module containing the window.

alias

An alias for the data source.

sub_select

A nested select statement (a subquery).

inner

single [, ...] |single [INNER] JOIN single [ ON expression | USING ( ids ) ] |single NATURAL [INNER] JOIN single |single NATURAL [INNER] JOIN single

Components

single

A single data source expression. See single for more information.

expression

A SQL-92 expression specifying the join condition.

ids

A list of column IDs specifying the join condition.

outer

single { RIGHT |LEFT |FULL} OUTER JOIN single [ ON expression | USING ( ids ) ]

Components

single

A single data source expression. See single for more information.

expression

A SQL-92 expression specifying the join condition.

ids

A list of column IDs specifying the join condition.

Usage

The SQL Select statement queries the current contents of the public windows listed in its FROM clause and generates a result of zero or more rows of data, each of which has a fixed number of columns. In addition to any columns explicitly queried in the statement, the query result includes an initial TIMESTAMP column, containing the row timestamp.

The Select statement includes one or more simple-select components, each of which can contain the following clauses.

A compound-select is formed from two or more simple-selects connected by one of the operators described in the following table. All simple-selects in a compound-select must specify the same number of result columns. The following operators can be used to create a compound-select:

UNION

The results to the left and to the right of the UNION operator are combined into a single table. All results are distinct, as duplicate rows are eliminated from the results. NULL values are not treated as being distinct from one another.

UNION ALL

The results to the left and to the right of the UNION operator are combined into a single table. Any duplicate rows are retained in the results.

INTERSECT

Only the intersection between the parts of the compound-select to the left and to the right of the INTERSECT operator are included in the results.

EXCEPT

The results to the left of the EXCEPT operator are published, after removing from them the results to the right of the EXCEPT operator.

When three or more simple-selects are connected into a compound-select, they group from left to right.

The Select statement can also contain no more than one of each of the following clauses:

Select Clause

Every simple select of the Select statement must include one SELECT clause. This clause specifies a select-list, which is used to generate query results. The number of columns in the result is determined by the number of items in the query's select-lists. In compound selects, the connect operator also partially determines the number of columns in the query result.

A select-list consists of one or more comma-separated items, each of which is an arbitrary SQL-92 expression:

The select-list can be prefaced by a DISTINCT keyword. If two or more rows contain the same values in all queried columns, the DISTINCT keyword causes only one of the rows to be included in the result. In the absence of the DISTINCT keyword, result rows are returned regardless of whether or not they are distinct. This default behavior can be specified explicitly by using the ALL keyword.

From Clause

The FROM clause specifies the data sources (public windows or subqueries) against which the Select statement is executed. Rows from these sources are filtered by the WHERE clause, if one is specified, and are then passed to the query's SELECT clause.

The FROM clause can refer to a single data source, or to multiple joined data sources. Each data source must be one of the following:

When your SQL FROM clause refers to a public window that is located in the project's main module, refer to the window by the name you defined for it in the Create Window statement. When referring to a public window in one of the main module's submodules, use the following syntax in your name reference:

[ [ parent/ [...] ] submodule/window]

Data sources specified in the FROM clause can include an SQL alias, specified with the syntax data-source AS alias. This alias can be used to refer to the data source anywhere in the query where the data source name would otherwise be used. This clause can also be used without the AS keyword.

When the FROM clause refers to joined data sources, the joins can be specified using either the comma-separated format, or the JOIN keyword format. SQL supports standard SQL-92 syntax, which is summarized below.

An inner join publishes all possible combinations of rows from the intersection of two or more specified data sources, according to the limitations of the selection condition (if one is present). Joined data sources can refer only to public windows in the same module or in the module's submodules. SQL provides four options for specifying an inner join:

Comma-separated join

Multiple data sources can be listed one after the other, separated by commas. An optional WHERE clause in the query creates the join condition for this type of join (the ON and USING subclauses of the FROM clause are not used to create a selection condition for comma-separated joins). In the absence of a WHERE clause, the join selects all values from all selected data source columns. This join syntax can include more than two data sources.

JOIN or INNER JOIN

Two data sources can be joined with the JOIN keyword, which can also be explicitly qualified as an INNER JOIN. A join condition for this join can be specified by an expression in the ON subclause, or a column ID list in the USING subclause. A WHERE clause selection condition for rows can also be included in the query, but is not used for the join condition specification.

NATURAL JOIN or NATURAL INNER JOIN

A join created with the keywords JOIN or INNER JOIN can be further defined as being NATURAL. A natural join is created on any and all columns that have the same names and data types in both data sources. Natural joins do not include a further join condition, though the query can include a WHERE clause selection condition for row filtering.

CROSS JOIN or CROSS INNER JOIN

A join created with the keywords JOIN or INNER JOIN can be further defined as being a CROSS join. A cross join returns the Cartesian product from its two data sources. No join condition is used with a cross join, though the WHERE clause selection condition for rows can be included in the query. This join type can be thought of as functioning identically to a comma-separated join with no selection condition and limited to only two data sources.

SQL also supports the following standard syntax for outer joins. An ON or USING subclause is required with outer joins to establish the join condition.

RIGHT OUTER JOIN

All possible combinations of rows from the intersection of both data sources (limited by the selection condition, if one is specified) are published. All the other rows from the right data source are also published. Unmatched columns in the left data source publish a value of NULL.

LEFT OUTER JOIN

All possible combinations of rows from the intersection of both data sources (limited by the selection condition, if one is specified) are published. All the other rows from the left data source are also published. Unmatched columns in the right data source publish a value of NULL.

FULL OUTER JOIN

All possible combinations of rows from the intersection of both data sources (limited by the selection condition, if one is specified) are published. All other rows from both data sources are published as well. Unmatched columns in either data source publish a value of NULL.

Where Clause

The WHERE clause can be used to specify an expression, which is used for one of two purposes:

Expressions specified inside the WHERE clause cannot refer to aggregate functions.

Group By Clause

The expressions specified in the GROUP BY clause cause one or more rows of the result to be combined into a single row of output. GROUP BY is often used when the query result contains aggregate functions. GROUP BY clause expressions do not have to appear in the result, however.

Having Clause

The HAVING clause is similar to the WHERE clause, but is applied after grouping has occurred. The HAVING expression can refer to aggregate functions and to values that are not included in the result.

Order Clause

The ORDER BY clause sorts the query's output rows according to the specified list of expressions. These expressions do not have to be part of the result when used with a simple-select. When used with a compound-select, however, each sort expression must exactly match one of the result columns. Each expression can optionally be followed by either or both of the following:

Only one ORDER BY clause can appear inside a SELECT statement.

Limit Clause

The LIMIT clause places an upper limit on the number of rows returned by the query. A negative LIMIT indicates no upper bound. Optionally, this clause can also indicate how many rows should be skipped at the beginning of the result set. Three syntax forms are permitted for the LIMIT clause:

LIMIT limit-number

The clause used without an offset specifier simply indicates an integer limiting the number of rows returned by the query.

LIMIT limit-number OFFSET offset-number

An additional OFFSET subclause specifies the number of rows that should be skipped (offset) at the beginning of the result set.

LIMIT offset-number, limit-number

An offset can also be specified with a comma. Notice, that, when this syntax is used, the first integer is the offset number, and the second integer is the limit number. This opposite order from the OFFSET subclause form is an intentional feature of SQL-92, provided to maximize compatibility with legacy SQL database systems.

When used in conjunction with a compound select the LIMIT clause can be used only once, but is applied to all the simple selects in the query.