SELECT
Function
The SELECT statement is used to query data from tables and is the most basic query statement in Spark SQL. Spark 4.0 supports the complete SELECT syntax, including LATERAL, PIVOT/UNPIVOT, OFFSET pagination, and other features.
Syntax
[ WITH with_query [, ...] ]
SELECT [ ALL | DISTINCT ] { * | projectItem [, ...] }
FROM table_reference [, ...]
[ WHERE boolean_expression ]
[ GROUP BY { expression | GROUPING SETS ( ... ) | ROLLUP ( ... ) | CUBE ( ... ) } [, ...] ]
[ HAVING boolean_expression ]
[ PIVOT ( aggregate_expr FOR pivot_column IN ( value1 [, ...] ) ) ]
[ UNPIVOT ( value_column FOR name_column IN ( column1 [, ...] ) ) ]
[ LATERAL VIEW function_expr AS alias [, ...] ]
[ LATERAL ( subquery ) ]
[ ORDER BY { expression [ ASC | DESC ] [ NULLS { FIRST | LAST } ] } [, ...] ]
[ SORT BY { expression [ ASC | DESC ] [ NULLS { FIRST | LAST } ] } [, ...] ]
[ CLUSTER BY expression [, ...] ]
[ DISTRIBUTE BY expression [, ...] ]
[ OFFSET integer_expression ]
[ LIMIT integer_expression ] Where projectItem is:
expression [ [ AS ] alias ]
Where table_reference is:
{ table_name [ TABLE ] [ alias ]
| join_table | LATERAL ( subquery ) [ alias ]
| ( subquery ) [ alias ]
| VALUES ( { expression } [, ...] ) [, ...] [ alias ]
| UNNEST(expression) [ alias ] } Parameters
| Parameter | Description |
|---|---|
| WITH with_query | Defines a CTE to simplify complex queries and improve readability. See WITH...AS for details. |
| ALL | Returns all rows, including duplicate rows. This is the default option. |
| DISTINCT | Removes duplicate rows from the result set. |
| * | Queries all columns. |
| projectItem | Specifies the columns or expressions to query. An alias can be specified using AS. |
| FROM table_reference | Specifies the data source for the query. Can be a table name, subquery, JOIN result, LATERAL subquery, etc. See FROM for details. |
| WHERE | Specifies filter conditions. Only rows that satisfy the conditions are returned. See WHERE for details. |
| GROUP BY | Groups rows by the specified expression, typically used with aggregate functions. Supports GROUPING SETS, ROLLUP, and CUBE extensions. |
| HAVING | Filters the results of GROUP BY. See HAVING for details. |
| PIVOT | Rotates rows to columns. Converts row data into columns based on the values of a specified column. |
| UNPIVOT | Rotates columns to rows. Converts column data into rows. |
| LATERAL VIEW | Used with table-generating functions (such as explode) to expand function output into multiple rows. |
| LATERAL (subquery) | A LATERAL subquery allows the subquery to reference columns from the outer query. |
| ORDER BY | Global sorting. See ORDER BY for details. |
| SORT BY | Local sorting. Sorts within each Reducer. See SORT BY for details. |
| CLUSTER BY | Buckets and sorts within buckets. Equivalent to DISTRIBUTE BY + SORT BY on the same field. See CLUSTER BY for details. |
| DISTRIBUTE BY | Buckets data by the specified field, distributing rows with the same key value to the same Reducer. See DISTRIBUTE BY for details. |
| OFFSET | Skips the first N rows of results. Used for pagination queries. |
| LIMIT | Limits the number of rows returned. |
| NULLS FIRST / NULLS LAST | Specifies the position of NULL values in the sort result. NULLS FIRST places NULLs first, and NULLS LAST places NULLs last. |
Precautions
- The table being queried must already exist. Otherwise, a query error is reported.
- When using NOT IN in a WHERE clause, ensure that the subquery result set does not contain any NULL values. If a NULL value exists, the entire query result will be an empty set. See the FAQ below for details.
Examples
- Basic query:
SELECT * FROM student;
- Query with DISTINCT deduplication:
SELECT DISTINCT name FROM student;
- Query with WHERE condition:
SELECT name, score FROM student WHERE score > 90;
- Query with GROUP BY and HAVING:
SELECT class_id, AVG(score) AS avg_score FROM student GROUP BY class_id HAVING AVG(score) > 80;
- Query with OFFSET pagination (Spark 4.0):
SELECT * FROM student ORDER BY score DESC LIMIT 10 OFFSET 20;
- Query with PIVOT row-to-column (Spark 4.0):
SELECT * FROM sales PIVOT (SUM(amount) FOR year IN (2023, 2024, 2025));
- Query with LATERAL subquery (Spark 4.0):
SELECT * FROM orders, LATERAL (SELECT * FROM items WHERE items.order_id = orders.id);
:: Type Cast
SELECT '123' :: INT;
-- Equivalent to CAST('123' AS INT) COLLATE Collation
SELECT name FROM t ORDER BY name COLLATE unicode_ci;
FAQ: What Can I Do If the Query Result Is Empty When the Not In Subquery Contains Null Values?
- Issue overview: In SQL syntax, if a NOT IN subquery contains a single NULL value, the entire query result will be an empty set (no data returned).
- Example: In the following example, the expected result is 3, but an empty set is returned.
-- Table A -- Table B id value --- ----- 1 1 2 2 3
Run the following SQL statement:
SELECT * FROM A WHERE id NOT IN (SELECT value FROM B);
- Cause analysis:
id NOT IN (1, 2, NULL) is equivalent to id <> 1 AND id <> 2 AND id <> NULL.
id <> NULL evaluates to UNKNOWN (SQL's three-valued logic), making the entire expression FALSE. Therefore, no data is returned.
- Solutions:
Solution 1: Use NOT EXISTS instead of NOT IN.
SELECT * FROM A WHERE NOT EXISTS ( SELECT 1 FROM B WHERE B.value = A.id );
Solution 2: Filter out NULL values in the subquery.
SELECT * FROM A WHERE id NOT IN ( SELECT value FROM B WHERE value IS NOT NULL );
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot