6.1. SELECT
Used for
Retrieving data
Available in
DSQL, ESQL, PSQL
Global syntax
SELECT
[WITH [RECURSIVE] <cte> [, <cte> ...]]
SELECT
[FIRST m] [SKIP n]
[DISTINCT | ALL] <columns>
FROM
<source> [[AS] alias]
[<joins>]
[WHERE <condition>]
[GROUP BY <grouping-list>
[HAVING <aggregate-condition>]]
[PLAN <plan-expr>]
[UNION [DISTINCT | ALL] <other-select>]
[ORDER BY <ordering-list>]
[ROWS <m> [TO <n>]]
[FOR UPDATE [OF <columns>]]
[WITH LOCK]
[INTO <variables>]
<variables> ::= [:]varname [, [:]varname ...]
Description
The SELECT
statement retrieves data from the database and hands them to the application or the enclosing SQL statement. Data are returned in zero or more rows, each containing one or more columns or fields. The total of rows returned is the result set of the statement.
The only mandatory parts of the SELECT
statement are:
The
SELECT
keyword, followed by a columns list. This part specifies what you want to retrieve.The
FROM
keyword, followed by a selectable object. This tells the engine where you want to get it from.
In its most basic form, SELECT
retrieves a number of columns from a single table or view, like this:
select id, name, address
from contacts
Or, to retrieve all the columns:
select * from sales
In practice, the rows retrieved are often limited by a WHERE
clause. The result set may be sorted by an ORDER BY
clause, and FIRST
, SKIP
or ROWS
may further limit the number of output rows. The column list may contain all kinds of expressions instead of just column names, and the source need not be a table or view: it may also be a derived table, a common table expression (CTE) or a selectable stored procedure (SP). Multiple sources may be combined in a JOIN
, and multiple result sets may be combined in a UNION
.
The following sections discuss the available SELECT
subclauses and their usage in detail.