SELECT
statementsupportedQueries & DMLSELECT can be used as either a statement or as a clause within other statements: - As a statement, the SELECT statement is the most commonly executed SQL statement; it queries the database and retrieves a set of rows. - As a clause, SELECT defines the set of columns returned by a query.
Syntax
supportedpartialnot supportednot scored
[ ... ]
SELECT [ { ALL | DISTINCT } ]
[ TOP <n> ]
[{<object_name>|<alias>}.]*
[ ILIKE '<pattern>' ]
[ EXCLUDE
{
<col_name> | ( <col_name>, <col_name>, ... )
}
]
[ REPLACE
{
( <expr> AS <col_name> [ , <expr> AS <col_name>, ... ] )
}
]
[ RENAME
{
<col_name> AS <col_alias>
| ( <col_name> AS <col_alias>, <col_name> AS <col_alias>, ... )
}
]
You can specify the following combinations of keywords after SELECT *. The keywords must be in the order shown below
SELECT * ILIKE ... REPLACE ...
You can specify the following combinations of keywords after SELECT *. The keywords must be in the order shown below
SELECT * ILIKE ... RENAME ...
You can specify the following combinations of keywords after SELECT *. The keywords must be in the order shown below
SELECT * ILIKE ... REPLACE ... RENAME ...
You can specify the following combinations of keywords after SELECT *. The keywords must be in the order shown below
SELECT * EXCLUDE ... REPLACE ...
You can specify the following combinations of keywords after SELECT *. The keywords must be in the order shown below
SELECT * EXCLUDE ... RENAME ...
You can specify the following combinations of keywords after SELECT *. The keywords must be in the order shown below
SELECT * EXCLUDE ... REPLACE ... RENAME ...
You can specify the following combinations of keywords after SELECT *. The keywords must be in the order shown below
SELECT * REPLACE ... RENAME ...
You can specify the following combinations of keywords after SELECT *. The keywords must be in the order shown below
[ ... ]
SELECT [ { ALL | DISTINCT } ]
[ TOP <n> ]
{
[{<object_name>|<alias>}.]<col_name>
| [{<object_name>|<alias>}.]$<col_position>
| <expr>
}
[ [ AS ] <col_alias> ]
[ , ... ]
[ ... ]
A trailing comma is supported in a column list. For example, the following SELECT statement is supported
SELECT emp_id,
name,
dept,
FROM employees;
Parameters
| Name | Required | Description |
|---|---|---|
ALL | DISTINCT | Specifies whether to perform duplicate elimination on the result set: ALL includes all values in the result set. DISTINCT eliminates duplicate values from the result set. Default: ALL | |
TOP n | Specifies the maximum number of results to return. See TOP <n>. | |
object_name or alias | Specifies the object identifier or object alias as defined in the FROM clause. | |
* | The asterisk is shorthand to indicate that the output should include all columns of the specified object, or all columns of all objects if * is not qualified with an object name or alias. The columns are returned in the order shown by executing the DESCRIBE command on the object. When you specify *, you can also specify ILIKE, EXCLUDE, REPLACE, and RENAME: Note When specifying a combination of keywords after SELECT *: You cannot specify both ILIKE and EXCLUDE. If you specify EXCLUDE with RENAME or REPLACE: You must specify EXCLUDE before RENAME or REPLACE: Copy code SELECT * EXCLUDE col_a RENAME col_b AS alias_b ... Copy code SELECT * EXCLUDE employee_id REPLACE ('DEPT-' || department_id AS department_id) ... You cannot specify the same column in EXCLUDE and RENAME. If you specify ILIKE with RENAME or REPLACE, you must specify ILIKE first: Copy code SELECT * ILIKE '%id%' RENAME department_id AS department ... Copy code SELECT * ILIKE '%id%' REPLACE ('DEPT-' || department_id AS department_id) ... If you specify REPLACE and RENAME: You must specify REPLACE first: Copy code SELECT * REPLACE ('DEPT-' || department_id AS department_id) RENAME employee_id as employee ... You can specify the same column name in REPLACE and RENAME: Copy code SELECT * REPLACE ('DEPT-' || department_id AS department_id) RENAME department_id as department ... | |
ILIKE 'pattern' | Specifies that only the columns that match pattern should be included in the results. In pattern, you can use the following SQL wildcards: Use an underscore (_) to match any single character. Use a percent sign (%) to match any sequence of zero or more characters. To match a sequence anywhere within the column name, begin and end the pattern with %. Matching is case-insensitive. If no columns match the specified pattern, a compilation error occurs (001080 (42601): ... SELECT with no columns). | |
EXCLUDE col_name EXCLUDE (col_name, col_name, ...) | Specifies the columns that should be excluded from the results. If you are selecting from multiple tables, use SELECT table_name.* to specify that you want to select all columns from a specific table, and specify the unqualified column name in EXCLUDE. For example:Copy code SELECT table_a.* EXCLUDE column_in_table_a , table_b.* EXCLUDE column_in_table_b ... | |
REPLACE (expr AS col_name [ , expr AS col_name, ...] ) | Replaces the value of col_name with the value of the evaluated expression expr. For example, to prepend the string 'DEPT-' to the values in the department_id column, use:Copy code SELECT * REPLACE ('DEPT-' || department_id AS department_id) ... For col_name: The column must exist and cannot be filtered out by ILIKE or EXCLUDE. You cannot specify the same column more than once in the list of replacements. If the column is in multiple tables (for example, in both tables in a join), the statement fails with an “ambiguous column” error. expr must evaluate to a single value. | |
RENAME col_name AS col_alias RENAME (col_name AS col_alias, col_name AS col_alias, ...) | Specifies the column aliases that should be used in the results. If you are selecting from multiple tables, use SELECT table_name.* to specify that you want to select all columns from a specific table, and specify the unqualified column name in RENAME. For example:Copy code SELECT table_a.* RENAME column_in_table_a AS col_alias_a, table_b.* RENAME column_in_table_b AS col_alias_b ... | |
col_name | Specifies the column identifier as defined in the FROM clause. | |
$col_position | Specifies the position of the column (1-based) as defined in the FROM clause. If a column is referenced from a table, this number can’t exceed the maximum number of columns in the table. | |
expr | Specifies an expression, such as a mathematical expression, that evaluates to a specific value for any given row. | |
[ AS ] col_alias | Specifies the column alias assigned to the resulting expression. This is used as the display name in a top-level SELECT list, and the column name in an inline view. Do not assign a column alias that is the same as the name of another column referenced in the query. For example, if you are selecting columns named prod_id and product_id, do not alias prod_id as product_id. See Error case: Specifying an alias that matches another column name. |
Usage notes
- Aliases and identifiers are case-insensitive by default. To preserve case, enclose them within double quotes ("). For more information, see Object identifiers.
- Without an ORDER BY clause, the results returned by SELECT are an unordered set. Running the same query repeatedly against the same tables might result in a different output order every time. If order matters, use the ORDER BY clause.
- SELECT can be used not only as an independent statement, but also as a clause in other statements, for example INSERT INTO ... SELECT ...;. SELECT can also be used in a subquery within a statement.
- In many cases, when you use a column alias for an expression (i.e. expr AS col_alias) in other parts of the same query (in JOIN, FROM, WHERE, GROUP BY, other column expressions, etc.), the expression is evaluated only once. However, note that in some cases, the expression can be evaluated multiple times, which can result in different values for the alias used in different parts of the same query.