FROM sub-clause of a or as parameter to a .
A is a particular kind of table expression.
Synopsis
Parameters
| Parameter | Description |
|---|---|
table_name | A table or view name. |
table_alias_name | A name to use in an aliased table expression. |
name | One or more aliases for the column names, to use in an aliased table expression. |
index_name | Optional syntax to force index selection. |
func_application | Result from a function. |
row_source_extension_stmt | Result rows from a . |
select_stmt | A to use as subquery. |
joined_table | A . |
Table expressions language
The synopsis defines a mini-language to construct complex table expressions from simpler parts.| Construct | Description | Examples |
|---|---|---|
table_name [@ scan_parameters] | Access a table or view. | accounts, accounts@name_idx |
function_name ( exprs ... ) | Generate tabular data using a scalar function or table generator function. | sin(1.2), generate_series(1,10) |
[AS] name [( name [, ...] )] | Rename a table and optionally columns. | accounts a, accounts AS a, accounts AS a(id, b) |
WITH ORDINALITY | Enumerate the result rows. | accounts WITH ORDINALITY |
JOIN ON ... | . | orders o JOIN customers c ON o.customer_id = c.id |
(... subquery ...) | A used as subquery. | (SELECT * FROM customers c) |
[... statement ...] | Use the result rows of an . This is a CockroachDB extension. However, Cockroach Labs recommends that you use the standard SQL instead. See Use the output of another statement for an example. | [SHOW COLUMNS FROM accounts] |
Table expressions that generate data
The following sections describe primary table expressions that produce data.Table and view names
Syntax
Force index selection
By using the explicit index annotation, you can override CockroachDB’s index selection and use a specific when reading from a named table.Index selection can impact , but does not change the result of a query.
Force index scan
The syntax to force a scan of a specific index is:Force reverse scan
The syntax to force a reverse scan of a specific index is:DIRECTION is either ASC (ascending) or DESC (descending).
When a direction is specified, that scan direction is forced; otherwise the 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 . For example, given the table
Force partial index scan
To force a , your statement must have aWHERE clause that implies the partial index filter.
Force partial GIN index scan
To force a scan, your statement must have aWHERE 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 table, specify theNO_FULL_SCAN index hint. For example:
NO_FULL_SCAN in combination with the partial index using FORCE_INDEX=index_name.
If you specify only NO_FULL_SCAN, a full scan of a partial index may be planned.
You can also force index selection for and statements.
Access a common table expression
A single identifier in a table expression can refer to a defined earlier. For example:Result from a function
A table expression can use the result from a function application as a data source.Syntax
Scalar function as data source
When a 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:(SRF).x where x is one of the following:
- The name of a column returned from the function.
*, to denote all columns.
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
Ordinality annotation
Appends a column namedordinality, whose values describe the ordinality of each row, to the data source specified in the
table expression operand.
Syntax
WITH ORDINALITY necessarily prevents some optimizations of the surrounding query. Use it sparingly if performance is a concern, and always check the output of 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 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 enclosed between parentheses()
as a table expression. This is called a .
Syntax
- See for more details and performance best practices.
- To use other statements that produce data in a table expression, for example
SHOW, see Use the output of another statement.
Use the output of another statement
Syntax
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:

