SELECT [hints, ...] [ALL|DISTINCT] named_expression[, named_expression, ...] FROM relation[, relation, ...] [lateral_view[, lateral_view, ...]] [WHERE boolean_expression] [aggregation [HAVING boolean_expression]] [ORDER BY sort_expressions] [CLUSTER BY expressions] [DISTRIBUTE BY expressions] [SORT BY sort_expressions] [WINDOW named_window[, WINDOW named_window, ...]] [LIMIT num_rows] named_expression: : expression [AS alias] relation: | join_relation | (table_name|query|relation) [sample] [AS alias] : VALUES (expressions)[, (expressions), ...] [AS (column_name[, column_name, ...])] expressions: : expression[, expression, ...] sort_expressions: : expression [ASC|DESC][, expression [ASC|DESC], ...]
Output data from one or more relations.
A relation refers to any source of input data. It could be the contents of an existing table (or view), the joined result of two existing tables, or a subquery (the result of another
(Delta Lake on Databricks) In Delta Lake, you specify a relation either by specifying:
table_name. In addition, you can specify a time travel version using
TIMESTAMP AS OF,
VERSION AS OF, or
@ syntax, after your table identifier. See Query an older snapshot of a table (time travel) for details.
Select all matching rows from the relation. Enabled by default.
Select all matching rows from the relation then remove duplicate results.
Filter rows by predicate.
Filter grouped result by predicate.
Impose total ordering on a set of expressions. Default sort direction is ascending. You cannot use this with
CLUSTER BY, or
Repartition rows in the relation based on a set of expressions. Rows with the same expression values will be hashed to the same worker. You cannot use this with
ORDER BY or
Impose ordering on a set of expressions within each partition. Default sort direction is ascending. You cannot use this with
ORDER BY or
Repartition rows in the relation based on a set of expressions and sort the rows in ascending order based on the expressions. In other words, this is a shorthand for
DISTRIBUTE BY and
SORT BY where all expressions are sorted in ascending order. You cannot use this with
DISTRIBUTE BY, or
Assign an identifier to a window specification. See Window functions.
Limit the number of rows returned.
Explicitly specify values instead of reading them from a relation.
SELECT * FROM boxes SELECT width, length FROM boxes WHERE height=3 SELECT DISTINCT width, length FROM boxes WHERE height=3 LIMIT 2 SELECT * FROM VALUES (1, 2, 3) AS (width, length, height) SELECT * FROM VALUES (1, 2, 3), (2, 3, 4) AS (width, length, height) SELECT * FROM boxes ORDER BY width SELECT * FROM boxes DISTRIBUTE BY width SORT BY width SELECT * FROM boxes CLUSTER BY length
sample: | TABLESAMPLE ([integer_expression | decimal_expression] PERCENT) : TABLESAMPLE (integer_expression ROWS)
Sample the input data. This can be expressed in terms of either a percentage (must be between 0 and 100) or a fixed number of input rows.
join_relation: | relation join_type JOIN relation [ON boolean_expression | USING (column_name, column_name) ] : relation NATURAL join_type JOIN relation join_type: | INNER | [LEFT | RIGHT] SEMI | [LEFT | RIGHT | FULL] [OUTER] : [LEFT] ANTI
Select all rows from both relations where there is match.
Select all rows from both relations, filling with null values on the side that does not have a match.
Select only rows from the side of the
SEMI JOIN where there is a match. If one row matches multiple rows, only the first match is returned.
LEFT ANTI JOIN
Select only rows from the left side that match no rows on the right side.
lateral_view: : LATERAL VIEW [OUTER] function_name (expressions) table_name [AS (column_name[, column_name, ...])]
Generate zero or more output rows for each input row using a table-generating function. The most common built-in function used with
LATERAL VIEW is
LATERAL VIEW OUTER
Generate a row with null values even when the function returned zero rows.
aggregation: : GROUP BY expressions [WITH ROLLUP | WITH CUBE | GROUPING SETS (expressions)]
Group by a set of expressions using one or more aggregate functions. Common built-in aggregate functions include count, avg, min, max, and sum.
Create a grouping set at each hierarchical level of the specified expressions. For instance, For instance,
GROUP BY a, b, c WITH ROLLUP is equivalent to
GROUP BY a, b, c GROUPING SETS ((a, b, c), (a, b), (a), ()). The total number of grouping sets will be
N + 1, where
N is the number of group expressions.
Create a grouping set for each possible combination of set of the specified expressions. For instance,
GROUP BY a, b, c WITH CUBE is equivalent to
GROUP BY a, b, c GROUPING SETS ((a, b, c), (a, b), (b, c), (a, c), (a), (b), (c), ()). The total number of grouping sets will be
N is the number of group expressions.
Perform a group by for each subset of the group expressions specified in the grouping sets. For instance,
GROUP BY x, y GROUPING SETS (x, y) is equivalent to the result of
GROUP BY x unioned with that of
GROUP BY y.
SELECT height, COUNT(*) AS num_rows FROM boxes GROUP BY height SELECT width, AVG(length) AS average_length FROM boxes GROUP BY width SELECT width, length, height FROM boxes GROUP BY width, length, height WITH ROLLUP SELECT width, length, avg(height) FROM boxes GROUP BY width, length GROUPING SETS (width, length)
window_expression: : expression OVER window_spec named_window: : window_identifier AS window_spec window_spec: | window_identifier : ( [PARTITION | DISTRIBUTE] BY expressions [[ORDER | SORT] BY sort_expressions] [window_frame]) window_frame: | [RANGE | ROWS] frame_bound : [RANGE | ROWS] BETWEEN frame_bound AND frame_bound frame_bound: | CURRENT ROW | UNBOUNDED [PRECEDING | FOLLOWING] : expression [PRECEDING | FOLLOWING]
Compute a result over a range of input rows. A windowed expression is specified using the
OVER keyword, which is followed by either an identifier to the window (defined using the
WINDOW keyword) or the specification of a window.
Specify which rows will be in the same partition, aliased by
Specify how rows within a window partition are ordered, aliased by
Express the size of the window in terms of a value range for the expression.
Express the size of the window in terms of the number of rows before and/or after the current row.
Use the current row as a bound.
Use negative infinity as the lower bound or infinity as the upper bound.
If used with a
RANGE bound, this defines the lower bound of the value range. If used with a
ROWS bound, this determines the number of rows before the current row to keep in the window.
If used with a
RANGE bound, this defines the upper bound of the value range. If used with a
ROWS bound, this determines the number of rows after the current row to keep in the window.
hints: : /*+ hint[, hint, ...] */ hint: : hintName [(expression[, expression, ...])]
Hints can be used to help Spark execute a query better. For example, you can hint that a table is small enough to be broadcast, which would speed up joins.
You add one or more hints to a
SELECT statement inside /*+ … */ comment blocks. Multiple hints can be specified inside the same comment block, in which case the hints are separated by commas, and there can be multiple such comment blocks. A hint has a name (for example,
BROADCAST) and accepts 0 or more parameters.
SELECT /*+ BROADCAST(customers) */ * FROM customers, orders WHERE o_custId = c_custId SELECT /*+ SKEW('orders') */ * FROM customers, orders WHERE o_custId = c_custId SELECT /*+ SKEW('orders'), BROADCAST(demographic) */ * FROM orders, customers, demographic WHERE o_custId = c_custId AND c_demoId = d_demoId
(Delta Lake on Databricks) See Skew Join optimization for more information about the