Skip to main content
A table expression defines a data source in the FROM sub-clause of a SELECT clause or as parameter to a TABLE clause. A join is a particular kind of table expression.

Synopsis

table_ref syntax diagram

Parameters

Table expressions language

The synopsis defines a mini-language to construct complex table expressions from simpler parts. The following sections provide details on each of these options.

Table expressions that generate data

The following sections describe primary table expressions that produce data.

Table and view names

Syntax

A single SQL identifier in a table expression designates the contents of the table, view, or sequence with that name in the current database, as configured by SET DATABASE. If the name is composed of two or more identifiers, name resolution rules apply. For example:

Force index selection

By using the explicit index annotation, you can override CockroachDB’s index selection and use a specific index when reading from a named table. This is called an index hint. Index selection can impact performance, but does not change the result of a query.
You can rewrite inline hints to apply index hints without modifying the original query text.
Force index scan
To force a scan of a specific index:
This is equivalent to the longer expression:
Force reverse scan
To force a reverse scan of a specific index:
Forcing a reverse scan can help with performance tuning. To choose an index and its scan direction:
where the optional DIRECTION is either ASC (ascending) or DESC (descending). When a direction is specified, that scan direction is forced; otherwise the cost-based optimizer is free to choose the direction it calculates will result in the best performance. You can verify that the optimizer is choosing your desired scan direction using EXPLAIN (OPT). For example, given the table
you can check the scan direction with:

Force inverted index scan

To force a scan of any inverted index of the hinted table:
The FORCE_INVERTED_INDEX hint does not allow specifying an inverted index. If no query plan can be generated, the query will result in the error:
Force partial index scan
To force a partial index scan, your statement must have a WHERE clause that implies the partial index filter.
Force partial GIN index scan
To force a partial GIN index scan, your statement must have a WHERE clause that:
  • Implies the partial index.
  • Constrains the GIN index scan.
Prevent full scan
  • To prevent the optimizer from planning a full scan for a specific table, specify the NO_FULL_SCAN index hint. For example:
  • To prevent a full scan of a partial index for a specific table, you must specify NO_FULL_SCAN in combination with the index name using FORCE_INDEX. For example:
    This forces a constrained scan of the partial index. If a constrained scan of the partial index is not possible, an error is returned.
For other ways to prevent full scans, refer to Prevent the optimizer from planning full scans.
You can also force index selection for DELETE and UPDATE statements.

Access a common table expression

A single identifier in a table expression can refer to a common table expression defined earlier. For example:

Result from a function

A table expression can use the result from a function application as a data source.

Syntax

The name of a function, followed by an opening parenthesis, followed by zero or more scalar expressions, followed by a closing parenthesis. The resolution of the function name follows the same rules as the resolution of table names. See Name Resolution for more details.

Scalar function as data source

When a function returning a single value is used as a table expression, it is interpreted as tabular data with a single column and single row containing the function result. For example:

Table generator functions

Some functions directly generate tabular data with multiple rows from a single function application. This is also called a set-returning function (SRF). For example:
You access SRFs using (SRF).x where x is one of the following:
  • The name of a column returned from the function.
  • *, to denote all columns.
For example (the output of queries against information_schema will vary per database):
CockroachDB supports the generator functions compatible with the PostgreSQL set-generating functions with the same names.

Operators that extend a table expression

The following sections describe table expressions that change the metadata around tabular data, or add more data, without modifying the data of the underlying operand.

Aliased table expressions

Aliased table expressions rename tables and columns temporarily in the context of the current query.

Syntax

In the first form, the table expression is equivalent to its left operand with a new name for the entire table, and where columns retain their original name. In the second form, the columns are also renamed. For example:

Ordinality annotation

Appends a column named ordinality, whose values describe the ordinality of each row, to the data source specified in the table expression operand.

Syntax

For example:
WITH ORDINALITY necessarily prevents some optimizations of the surrounding query. Use it sparingly if performance is a concern, and always check the output of EXPLAIN in case of doubt.

JOIN expressions

JOIN expressions combine the results of two or more table expressions based on conditions on the values of particular columns. See JOIN expressions for more details.

Use another query as a table expression

The following sections describe how to use the result produced by another SQL query or statement as a table expression.

Use a subquery

You can use a selection query enclosed between parentheses () as a table expression. This is called a subquery.

Syntax

For example:

Use the output of another statement

Syntax

A WITH query designates the output of executing a statement as a row source. The following statements are supported as row sources for table expressions: For example:
The following statement inserts Albert in the employee table and immediately creates a matching entry in the management table with the auto-generated employee ID, without requiring a round trip with the SQL client:

Composability

You can use table expressions in the SELECT clause and TABLE clause variants of selection clauses. Thus they can appear everywhere where a selection clause is possible. For example:
For more options to compose query results, see Selection Queries.

See also