---
title: table reference
description: Reference tables, views, subqueries, and other intermediate result sets in a query with table references in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-table-reference
---

# table reference

**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

A table reference is an intermediate result table within SQL.
It can be *derived* from other operators, such as functions, joins or a subquery, reference a base table directly, or be constructed as an inline table.

## Syntax

```
table_reference
  { table_name [ TABLESAMPLE clause ] [ table_alias ] |
    { STREAM table_name [ table_alias ] | STREAM ( table_name ) [ table_alias ] } |
    view_name [ table_alias ] |
    table_reference JOIN clause |
    table_reference NEAREST BY JOIN clause |
    table_reference PIVOT clause |
    table_reference UNPIVOT clause |
    table_reference MATCH_RECOGNIZE clause |
    [ STREAM ] table_valued_function [ table_alias ] |
    [ LATERAL ] table_valued_function [ table_alias ] |
    VALUES clause |
    [ LATERAL ] ( query ) [ TABLESAMPLE clause ] [ table_alias ] }
```

## Parameters

* **[table\_name](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-names.md#table-name)**

  Identifies a table that may contain a [temporal specification](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-names.md#table-name).
  See [Work with table history](https://docs.databricks.com/aws/en/tables/history.md) for details.

  If the table cannot be found Databricks raises a [TABLE\_OR\_VIEW\_NOT\_FOUND](https://docs.databricks.com/aws/en/error-messages/table-or-view-not-found-error-class.md) error.

  See [Column, field, parameter, and variable resolution](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-name-resolution.md#column-field-parameter-and-variable-resolution) for more information on name resolution.

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

  `table_name` may include an [options specification](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-names.md#table-name).

* **[view\_name](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-names.md#view-name)**

  Identifies a view or a common table expression (CTE).
  If the view cannot be found Databricks raises a [TABLE\_OR\_VIEW\_NOT\_FOUND](https://docs.databricks.com/aws/en/error-messages/table-or-view-not-found-error-class.md) error.

  See [Column, field, parameter, and variable resolution](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-name-resolution.md#column-field-parameter-and-variable-resolution) for more information on name resolution.

* **STREAM**

  Returns a table or a table valued function as a streaming source. Tables cannot be provided with a temporal specification when used with the `STREAM` keyword. Streaming sources are most commonly used in the definitions of [streaming tables](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-ddl-create-streaming-table.md).

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

  Combines two or more relations using a join.

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

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

  > [!NOTE]
  >
  > Databricks Runtime 18 is newer than Databricks Runtime 18.0, 18.1, and 18.2. Features that would previously have shipped as a later numbered version now ship as dated updates to Databricks Runtime 18 instead. For details, see [About unified release notes](https://docs.databricks.com/aws/en/release-notes/runtime/18.md#unified-format).

  Returns the closest matches from one relation per row of another, ranked by a custom distance or similarity expression.

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

  **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 12.2 LTS and above.

  Used for data perspective; you can get the aggregated values based on specific column value.

  Prior to Databricks Runtime 12.0 [PIVOT](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-pivot.md) is limited to [SELECT](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select.md) following the `FROM` clause.

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

  **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 12.2 LTS and above.

  Used for data perspective; you can split multiple column groups into rows.

* **[MATCH\_RECOGNIZE](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-match-recognize.md)**

  **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 19 and above.

  Finds and filters patterns in the rows of the preceding `table_reference`.

* **\[LATERAL]** **[table\_valued\_function\_invocation](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-tvf.md)**

  Invokes a table valued function.
  To refer to columns exposed by a preceding `table_reference` in the same `FROM` clause you must specify `LATERAL`.

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

  Defines an inline table.

* **\[LATERAL] ( [query](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-query.md) )**

  Computes a table reference using a query. A query prefixed by `LATERAL` may reference columns exposed by a preceding `table_reference` in the same `FROM` clause.
  Such a construct is called a correlated or dependent query.

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

  Optionally reduce the size of the result set by only sampling a fraction of the rows.

* **[table\_alias](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-names.md#table-alias)**

  Optionally specifies a label for the `table_reference`. If the `table_alias` includes `column_identifier`s their number must match the number of columns in the `table_reference`.

## 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
```

## Common error conditions

* [NUM\_TABLE\_VALUE\_ALIASES\_MISMATCH](https://docs.databricks.com/aws/en/error-messages/error-classes.md#num_table_value_aliases_mismatch)
* [TABLE\_OR\_VIEW\_NOT\_FOUND](https://docs.databricks.com/aws/en/error-messages/table-or-view-not-found-error-class.md)

## Examples

```sql

-- Return a data set from a storage location using a credential.
> SELECT * FROM `csv`.`spreadsheets/data.csv` WITH(CREDENTIAL some_credential);

-- Select from a jdbc data source while controlling the fetch size.
> SELECT * FROM jdbcTable WITH(fetchSize = 0);

-- 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
```

## Related articles

* [JOIN](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-join.md)
* [NEAREST BY clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-nearest-by.md)
* [PIVOT clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-pivot.md)
* [`SELECT` (subselect)](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select.md)
* [UNPIVOT clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-unpivot.md)
* [MATCH\_RECOGNIZE clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-match-recognize.md)
* [`VALUES` clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-values.md)
* [Table-valued function (TVF) invocation](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-tvf.md)
* [TABLESAMPLE clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-sampling.md)
