---
title: SELECT (subselect)
description: Compose a result set from one or more table references with the SELECT subquery syntax in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select
---

# `SELECT` (subselect)

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime

Composes a result set from one or more [table references](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-table-reference.md).
The `SELECT` clause can be part of a query which also includes common table expressions (CTE), set operations, and various other clauses.

## Syntax

```
  SELECT clause
  FROM table_reference [, ...]
  [ LATERAL VIEW clause ]
  [ WHERE clause ]
  [ GROUP BY clause ]
  [ HAVING clause]
  [ QUALIFY clause ]

named_expression
   expression [ column_alias ]

star_clause
   [ { table_name | view_name } . ] * [ except_clause ]

except_clause
   EXCEPT ( { column_name | field_name } [, ...] )
```

## Parameters

* **[SELECT clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-column-list.md)**

  Collects the columns to be returned from the subquery, including the execution of expressions, aggregations, and deduplication.

* **[table\_reference](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-table-reference.md)**

  A source of input for the `SELECT`. This input reference can be turned into a streaming reference by using the `STREAM` keyword prior to the reference.

  If the `table_reference` is a [metric view](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-create-view.md), there must be no other table reference in the `FROM` clause.

* **[LATERAL VIEW](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-lateral-view.md)**

  Used in conjunction with generator functions such as `EXPLODE`, which generates a virtual table containing one or more rows. `LATERAL VIEW` applies the rows to each original output row.

  In Databricks SQL, and starting with Databricks Runtime 12.2 this clause is deprecated. You should invoke a table valued generator function as a [table\_reference](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-table-reference.md).

* **[WHERE](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-where.md)**

  Filters the result of the `FROM` clause based on the supplied predicates.

* **[GROUP BY](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-groupby.md)**

  The expressions that are used to group the rows. This is used in conjunction with aggregate functions (`MIN`, `MAX`, `COUNT`, `SUM`, `AVG`) to group rows based on the grouping expressions and aggregate values in each group. When a `FILTER` clause is attached to an aggregate function, only the matching rows are passed to that function.

* **[HAVING](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-having.md)**

  The predicates by which the rows produced by `GROUP BY` are filtered. The `HAVING` clause is used to
  filter rows after the grouping is performed. If you specify `HAVING` without `GROUP BY`, it indicates a `GROUP BY` without grouping expressions (global aggregate).

* **[QUALIFY](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-qualify.md)**

  The predicates that are used to filter the results of window functions.
  To use `QUALIFY`, at least one [window function](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions.md) is required to be present in the SELECT list or the QUALIFY clause.

## Common error conditions

* [EXCEPT\_OVERLAPPING\_COLUMNS](https://docs.databricks.com/aws/en/error-messages/error-classes.md#except_overlapping_columns)
* [MISSING\_AGGREGATION](https://docs.databricks.com/aws/en/error-messages/missing-aggregation-error-class.md)
* [NESTED\_AGGREGATE\_FUNCTION](https://docs.databricks.com/aws/en/error-messages/error-classes.md#nested_aggregate_function)
* [WINDOW\_FUNCTION\_WITHOUT\_OVER\_CLAUSE](https://docs.databricks.com/aws/en/error-messages/error-classes.md#window_function_without_over_clause)

## Select on Delta table

In addition to the standard `SELECT` options, Delta tables support the time travel options described in this section. For details, see [Work with table history](https://docs.databricks.com/aws/en/tables/history.md).

### `AS OF` syntax

```
table_identifier TIMESTAMP AS OF timestamp_expression

table_identifier VERSION AS OF version
```

* `timestamp_expression` can be any one of:
  * `'2018-10-18T22:15:12.013Z'`, that is, a string that can be cast to a timestamp
  * `cast('2018-10-18 13:36:32 CEST' as timestamp)`
  * `'2018-10-18'`, that is, a date string
  * `current_timestamp() - interval 12 hours`
  * `date_sub(current_date(), 1)`
  * Any other expression that is or can be cast to a timestamp
* `version` is a long value that can be obtained from the output of `DESCRIBE HISTORY table_spec`.

Neither `timestamp_expression` nor `version` can be subqueries.

#### Example

```sql
> SELECT * FROM events TIMESTAMP AS OF '2018-10-18T22:15:12.013Z'

> SELECT * FROM events VERSION AS OF 123
```

### `@` syntax

Use the `@` syntax to specify the timestamp or version. The timestamp must be in `yyyyMMddHHmmssSSS` format. You can specify a version after `@` by prepending a `v` to the version. For example, to query version `123` for the table `events`, specify `events@v123`.

#### Example

```sql
> SELECT * FROM events@20190101000000000

> SELECT * FROM events@v123
```

## Examples

```sql
-- select all referencable columns from all tables
> SELECT * FROM VALUES(1, 2) AS t1(c1, c2), VALUES(3, 4) AS t2(c3, c4);
  1   2   3   4

-- select all referencable columns from one table
> SELECT t2.* FROM VALUES(1, 2) AS t1(c1, c2), VALUES(3, 4) AS t2(c3, c4);
  3   4

-- select all referencable columns from all tables except t2.c4
> SELECT * EXCEPT(c4) FROM VALUES(1, 2) AS t1(c1, c2), VALUES(3, 4) AS t2(c3, c4);
  1   2   3

-- select all referencable columns from a table, except a nested field.
> SELECT * EXCEPT(c2.b) FROM VALUES(1, named_struct('a', 2, 'b', 3)) AS t(c1, c2);
  1  { "a" : 2 }

-- Removing all fields results in an empty struct
> SELECT * EXCEPT(c2.b, c2.a) FROM VALUES(1, named_struct('a', 2, 'b', 3)) AS t(c1, c2);
  1  { }

-- Overlapping names result in an error
> SELECT * EXCEPT(c2, c2.a) FROM VALUES(1, named_struct('a', 2, 'b', 3)) AS t(c1, c2);
  Error: EXCEPT_OVERLAPPING_COLUMNS

-- Non-aggregated column without `GROUP BY`.
> SELECT c1, sum(c2) FROM VALUES(1, 2), (1, 3) AS t(c1, c2);
  Error: MISSING_AGGREGATION
```

## Related articles

* [CLUSTER BY clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-clusterby.md)
* [Common table expression (CTE)](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-cte.md)
* [DISTRIBUTE BY clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-distributeby.md)
* [GROUP BY clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-groupby.md)
* [HAVING clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-having.md)
* [QUALIFY clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-qualify.md)
* [Hints](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-hints.md)
* [VALUES clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-values.md)
* [JOIN](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-join.md)
* [LATERAL VIEW clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-lateral-view.md)
* [LIMIT clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-limit.md)
* [OFFSET clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-offset.md)
* [ORDER BY clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-orderby.md)
* [PIVOT clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-pivot.md)
* [Query](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-query.md)
* [SELECT clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-column-list.md)
* [Set operators](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-setops.md)
* [SORT BY clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-sortby.md)
* [Star clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-star.md)
* [TABLESAMPLE clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-sampling.md)
* [Table-valued function (TVF)](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-tvf.md)
* [table reference](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-table-reference.md)
* [UNPIVOT clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-unpivot.md)
* [WHERE clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-where.md)
* [WINDOW clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-named-window.md)
* [Window functions](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions.md)
