SELECT
Applies to: ✅ Data 360 SQL ✅ Tableau Hyper API
Retrieves data from a table or view.
SELECT query syntax uses the required SELECT statement with one or more optional clauses.
with_query is defined as:
from_item is one of:
grouping_element is defined as:
This set of rules describes how the clauses defined in the SELECT statement are processed.
-
The queries in the
WITHlist are temporary tables that are referenced in theFROMlist. AWITHis computed only one time. -
All elements in the
FROMlist are computed. If more than one element is specified in theFROMlist, they’re cross-joined. -
If the
WHEREclause is specified, all rows that don’t satisfy the condition are removed from the output. -
If there’s a
GROUP BYclause or aggregate function calls, the output is combined into groups and the results of aggregate functions are computed. If theHAVINGclause is present, it eliminates groups that don’t satisfy the given condition. -
The output rows are computed by using the
SELECToutput expressions for each selected row or row group. -
SELECT DISTINCTremoves duplicate rows from the result.SELECT DISTINCT ONremoves rows that match the specified expressions.SELECT ALL(the default) returns candidate rows, including duplicates. -
By using the operators
UNION,INTERSECT, andEXCEPT, the output of more than oneSELECTstatement can be combined to form a single result set. -
If the
ORDER BYclause is specified, the returned rows are sorted in the specified order. -
If the
LIMIT(orFETCH FIRST) orOFFSETclause is specified, theSELECTstatement only returns a subset of the result rows.
WITHClauseThe
WITHclause allows you to specify one or more subqueries that can be referenced by name in the primary query. See WITH Clause.FROMClauseThe
FROMclause specifies one or more source tables for theSELECT. See FROM Clause.WHEREClauseThe optional
WHEREclause is any expression that evaluates to a result of typeboolean. Any row that doesn’t satisfy this condition is eliminated from the output.GROUP BYClauseGROUP BYcombines all selected rows that share values for the grouped expressions into one row. See GROUP BY Clause.HAVINGClauseHAVINGremoves group rows that don’t satisfy the condition. Each<condition>reference a grouping column, unless the reference appears in an aggregate function.HAVINGturns a query into a grouped query even if there’s noGROUP BYclause. TheSELECTlist andHAVINGclause can only reference table columns from aggregate functions. A row is emitted if theHAVINGcondition is true and zero rows for false.WINDOWClauseThe
WINDOWclause returns a partition of the data returned by the command. See WINDOW Clause.SELECTListThe
SELECTlist defines expressions that form the output rows of theSELECTstatement. The expressions can refer to columns computed in theFROMclause. UsingSELECT DISTINCT, output rows can be deduplicated. See SELECT Clause and DISTINCT Clause.UNION,INTERSECT, andEXCEPTOperatorsThe
UNIONoperator computes the set union of the rows returned by the involvedSELECTstatements.The
INTERSECToperator computes the set intersection of the rows returned by the involvedSELECTstatements.The
EXCEPToperator computes the set of rows that are in the result of the leftSELECTstatement but not in the result of the right one.ORDER BYClauseThe
ORDER BYclause sorts the result rows by the specified expressions. See ORDER BY Clause.LIMIT,FETCH, andOFFSETClausesThe
LIMITclause specifies the maximum number of rows to return.OFFSETspecifies the number of rows to skip before starting to return rows. See LIMIT and OFFEST Clauses.
The SELECT statement is compatible with the SQL standard but excludes some features and extends some functionality.
-
You can omit the
FROMclause in Data 360 SQL. For example, you can compute the results of simple expressions.If you don’t specify a
FROMclause, the query can’t reference any database tables. For example, this query is invalid.
-
The list of output expressions after
SELECTcan be empty, producing a zero-column result table. However, you can use an empty list withDISTINCT. -
ASis required if the new column name matches any keyword. To prevent conflicts with keyword additions, useASor double-quote output column names.In
FROMitems, you can omitASbefore an unreserved keyword alias. -
With Data 360 SQL you can write a function call as a member of the
FROMlist. -
Data 360 SQL supports ORDER BY and GROUP BY to specify expressions as input-column names.
-
The clauses
LIMITandOFFSETare specific to Data 360 SQL. -
DISTINCT ON ( ... )is an extension of the SQL standard.