--- layout: global title: SELECT displayTitle: SELECT license: | Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --- Spark supports a `SELECT` statement and conforms to the ANSI SQL standard. Queries are used to retrieve result sets from one or more tables. The following section describes the overall query syntax and the sub-sections cover different constructs of a query along with examples. ### Syntax {% highlight sql %} [ WITH with_query [ , ... ] ] SELECT [ hints , ... ] [ ALL | DISTINCT ] { named_expression [ , ... ] } FROM { from_item [ , ...] } [ WHERE boolean_expression ] [ GROUP BY expression [ , ...] ] [ HAVING boolean_expression ] [ ORDER BY { expression [ ASC | DESC ] [ NULLS { FIRST | LAST } ] [ , ...] } ] [ SORT BY { expression [ ASC | DESC ] [ NULLS { FIRST | LAST } ] [ , ...] } ] [ CLUSTER BY { expression [ , ...] } ] [ DISTRIBUTE BY { expression [, ...] } ] { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ] [ WINDOW { named_window [ , WINDOW named_window, ... ] } ] [ LIMIT { ALL | expression } ] {% endhighlight %} ### Parameters
with_query
SELECT
query block.
These table expressions are allowed to be referenced later in the main query. This is useful to abstract
out repeated subquery blocks in the main query and improves readability of the query.
hints
ALL
DISTINCT
named_expression
expression [AS] [alias]
from_item
WHERE
GROUP BY
HAVING
ORDER BY
SORT BY
,
CLUSTER BY
and DISTRIBUTE BY
and can not be specified together.
SORT BY
ORDER BY
and CLUSTER BY
and can not be specified together.
CLUSTER BY
DISTRIBUTE BY
and SORT BY
together.
DISTRIBUTE BY
ORDER BY
and CLUSTER BY
and can not be specified together.
LIMIT
ORDER BY
to produce a deterministic result.
boolean_expression
expression
named_window