- At the top level of a query, like other .
- Between parentheses as a .
- As operand to other statements that take tabular data as input, for example , , , or .
Synopsis
Parameters
| Parameter | Description |
|---|---|
common\_table\_expr | See . |
select\_clause | A valid selection clause, either simple or using set operations. |
sort\_clause | An optional ORDER BY clause. See for details. |
limit\_clause | An optional LIMIT clause. See for details. |
offset\_clause | An optional OFFSET clause. See for details. |
for\_locking\_clause | The FOR UPDATE and FOR SHARE clauses are used to lock SELECT statements. For more information, see . |
LIMIT and OFFSET clauses can appear in any order, but if also present, must appear after ORDER BY.
Because the
WITH, ORDER BY, LIMIT, and OFFSET sub-clauses are all optional, any simple selection clause is also a valid selection query.Selection clauses
A selection clause is the main component of a selection query. A selection clause defines tabular data. There are four specific syntax forms:| Form | Usage |
|---|---|
SELECT | Load or compute tabular data from various sources. This is the most common selection clause. |
VALUES | List tabular data by the client. |
TABLE | Load tabular data from the database. |
| Set operations | Combine tabular data from two or more selection clauses. |
To perform joins or other relational operations over selection clauses, use a and convert it back into a selection clause with
TABLE or SELECT.Synopsis
VALUES clause
Syntax
AVALUES clause defines tabular data defined by the expressions listed within parentheses. Each parenthesis group defines a single row in the resulting table.
The columns of the resulting table data have automatically generated names. When the VALUES clause is used as a , you can modify these names with .
Example
TABLE clause
Syntax
ATABLE clause reads tabular data from a specified table. The columns of the resulting table data are named after the schema of the table.
TABLE x is equivalent to SELECT * FROM x.
Any between parentheses is a valid operand for
TABLE, not just .Example
employee into a new table. However, the TABLE clause does not preserve the indexing, foreign key, or constraint and default information from the schema of the table it reads from, so in this example, the new table employee_copy will likely have a simpler schema than employee.
Other examples:
SELECT clause
See for more details.
Set operations
Set operations combine data from two selection clauses. They are valid as operand to other set operations or as main component in a selection query.Synopsis
Set operators
SQL lets you compare the results of multiple selection clauses. You can think of each of the set operators as representing a Boolean operator:UNION=ORINTERSECT=ANDEXCEPT=NOT
SELECT DISTINCT). However, to display duplicate values, you can add an ALL to the clause.
UNION: Combine two queries
UNION combines the results of two queries into one result.
ALL.
INTERSECT: Retrieve intersection of two queries
INTERSECT selects only values that are present in both query operands.
EXCEPT: Exclude one query’s results from another
EXCEPT selects values that are present in the first query operand but not the second.
Order results
The following sections provide examples. For more details, see .Order retrieved rows by one column
Order retrieved rows by multiple columns
Columns are sorted in the order you list them insortby_list. For example, ORDER BY a, b sorts the rows by column a and then sorts rows with the same a value by their column b values.
Limit row count
You can reduce the number of results withLIMIT.
Row-level locking for concurrency control with SELECT FOR UPDATE
SELECT... FOR UPDATE exclusively locks the rows returned by a , such that other transactions trying to access those rows must wait for the transaction that locked the rows to commit or rollback.
SELECT... FOR UPDATE can be used to:
-
Strengthen the isolation of a transaction. If you need to read and later update a row within a transaction, use
SELECT... FOR UPDATEto acquire an exclusive lock on the row. This guarantees data integrity between the transaction’s read and write operations. For details, see . -
Order transactions by controlling concurrent access to one or more rows of a table. These other transactions are placed into a queue based on when they tried to read the values of the locked rows.
Because this queueing happens during the read operation, the thrashing that would otherwise occur if multiple concurrently executing transactions attempt to
SELECTthe same data and thenUPDATEthe results of that selection is prevented. By preventing thrashing,SELECT... FOR UPDATEalso prevents that would otherwise occur due to . As a result, usingSELECT... FOR UPDATEleads to increased throughput and decreased tail latency for contended operations.
SELECT... FOR UPDATE does not completely eliminate the chance of . These errors can also arise due to . To eliminate the need for application-level retry logic, in addition to SELECT FOR UPDATE your application also needs to use a .
By default, CockroachDB uses the SELECT... FOR UPDATE locking mechanism during the initial row scan performed in and statement execution. To turn off implicit SELECT... FOR UPDATE locking for UPDATE and UPSERT statements, set the enable_implicit_select_for_update to false.
For an example, see .
Composability
Selection clauses are defined in the context of selection queries. are defined in the context of theFROM sub-clause of . Nevertheless, you can integrate them with one another to form more complex queries or statements.
Use a selection clause as a selection query
You can use a selection clause as a selection query with no change. For example, the construct is a selection clause. It is also a valid selection query, and thus can be used as a stand-alone statement by appending a semicolon:VALUES (1), (2), (3) is also a selection clause and thus can also be used as a selection query on its own:
Use a table expression as selection clause
You can use a as a selection clause (and thus also a selection query) by prefixing it withTABLE or by using it as an operand to SELECT * FROM.
For example, the customers is a table expression, which designates all rows in that table. The expressions and are valid selection clauses.
Likewise, the customers c JOIN orders o ON c.id = o.customer_id is a table expression. You can turn it into a valid selection clause, and thus a valid selection query as follows:
Use a selection query as table expression
You can use a selection query (or selection clause) as a by enclosing it between parentheses, which forms a . For example, the following construct is a selection query, but is not a valid table expression:FROM or another table expression, you can enclose it between parentheses as follows:
Use selection queries with other statements
Selection queries are also valid as operand in contexts that require tabular data. For example:| Statement | Example using SELECT | Example using VALUES | Example using TABLE |
|---|---|---|---|
INSERT INTO foo SELECT \* FROM bar | INSERT INTO foo VALUES (1), (2), (3) | INSERT INTO foo TABLE bar | |
UPSERT INTO foo SELECT \* FROM bar | UPSERT INTO foo VALUES (1), (2), (3) | UPSERT INTO foo TABLE bar | |
CREATE TABLE foo AS SELECT \* FROM bar``CREATE TABLE foo AS VALUES (1),(2),(3) | CREATE TABLE foo AS TABLE bar | ||
ALTER TABLE foo SPLIT AT SELECT \* FROM bar``ALTER TABLE foo SPLIT AT VALUES (1),(2),(3) | ALTER TABLE foo SPLIT AT TABLE bar | ||
| Subquery in a | SELECT \* FROM (SELECT \* FROM bar) | SELECT \* FROM (VALUES (1),(2),(3)) | SELECT \* FROM (TABLE bar) |
| Subquery in a | SELECT \* FROM foo WHERE x IN (SELECT \* FROM bar) | SELECT \* FROM foo WHERE x IN (VALUES (1),(2),(3)) | SELECT \* FROM foo WHERE x IN (TABLE bar) |

