Categories:

Query Syntax

SELECT

SELECT 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.

See also:

Query Syntax.

Syntax

[ ... ]
SELECT [ { ALL | DISTINCT } ]
    {
          [{<object_name>|<alias>}.]*
        | [{<object_name>|<alias>}.]<col_name>
        | [{<object_name>|<alias>}.]$<col_position>
        | <expr>
        [ [ AS ] <col_alias> ]
    }
    [ , ... ]
[ ... ]

For more information about SELECT as a statement, and the other clauses within the statement, see Query Syntax.

Parameters

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

*

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.

object_name or . alias

Specifies the object identifier or object alias as defined in the FROM clause.

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.

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.

Examples

A few simple examples are provided below.

Many additional examples are included in other parts of the documentation, including the detailed descriptions of Query Syntax.

Some of the queries below use the following tables and data:

CREATE TABLE employee_table (
    employee_ID INTEGER,
    last_name VARCHAR,
    first_name VARCHAR,
    department_ID INTEGER
    );

CREATE TABLE department_table (
    department_ID INTEGER,
    department_name VARCHAR
    );
INSERT INTO employee_table (employee_ID, last_name, first_name, department_ID) VALUES
    (101, 'Montgomery', 'Pat', 1),
    (102, 'Levine', 'Terry', 2),
    (103, 'Comstock', 'Dana', 2);

INSERT INTO department_table (department_ID, department_name) VALUES
    (1, 'Engineering'),
    (2, 'Customer Support'),
    (3, 'Finance');

This example shows how to look up an employee’s last name if you know their ID.

SELECT last_name FROM employee_table WHERE employee_ID = 101;
+------------+
| LAST_NAME  |
|------------|
| Montgomery |
+------------+

This example lists each employee and the name of the department that each employee works in. The output is in order by department name, and within each department the employees are in order by name. This query uses a JOIN to relate the information in one table to the information in another table.

SELECT department_name, last_name, first_name
    FROM employee_table INNER JOIN department_table
        ON employee_table.department_ID = department_table.department_ID
    ORDER BY department_name, last_name, first_name;
+------------------+------------+------------+
| DEPARTMENT_NAME  | LAST_NAME  | FIRST_NAME |
|------------------+------------+------------|
| Customer Support | Comstock   | Dana       |
| Customer Support | Levine     | Terry      |
| Engineering      | Montgomery | Pat        |
+------------------+------------+------------+

This example shows how to use $ to specify a column by column number, rather than by column name:

SELECT $2 FROM employee_table ORDER BY $2;
+------------+
| $2         |
|------------|
| Comstock   |
| Levine     |
| Montgomery |
+------------+

This example shows that the output columns do not need to be taken directly from the tables in the FROM clause; the output columns can be general expressions. This example calculates the area of a circle that has a radius of 2.0. This example also shows how to use a column alias so that the output has a meaningful column name:

SELECT pi() * 2.0 * 2.0 AS area_of_circle;
+----------------+
| AREA_OF_CIRCLE |
|----------------|
|   12.566370614 |
+----------------+