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 } ]
       [ TOP <n> ]
       {

         -- Selecting all columns.
         [{<object_name>|<alias>}.]*
         [ EXCLUDE
           {
             <col_name>
             | ( <col_name>, <col_name>, ... )
           }
         ]
         [ RENAME
           {
             <col_name> AS <col_alias>
             | ( <col_name> AS <col_alias>, <col_name> AS <col_alias>, ... )
           }
         ]

       |

         -- Selecting specific columns.
         {
           [{<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

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.

EXCLUDE col_name . EXCLUDE (col_name, col_name, ...)

When you select all columns (SELECT * or SELECT table_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:

SELECT table_a.* EXCLUDE column_in_table_a ,
  table_b.* EXCLUDE column_in_table_b
  ...
RENAME col_name AS col_alias . RENAME (col_name AS col_alias, col_name AS col_alias, ...)

When you select all columns (SELECT * or SELECT table_name.*), 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:

SELECT table_a.* RENAME column_in_table_a AS col_alias_a,
  table_b.* RENAME column_in_table_b AS col_alias_b
  ...

Note

If you specify both EXCLUDE and RENAME after SELECT *:

  • You must specify EXCLUDE before RENAME:

    SELECT * EXCLUDE col_a RENAME col_b AS alias_b ...
    
  • You cannot specify the same column in EXCLUDE and RENAME.

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.

Setting Up the Data for the Examples

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');

Examples of Selecting All Columns (SELECT *)

Selecting All Columns in the Table

This example shows how to select all columns in employee_table:

SELECT * FROM employee_table;

+-------------+------------+------------+---------------+
| EMPLOYEE_ID | LAST_NAME  | FIRST_NAME | DEPARTMENT_ID |
|-------------+------------+------------+---------------|
|         101 | Montgomery | Pat        |             1 |
|         102 | Levine     | Terry      |             2 |
|         103 | Comstock   | Dana       |             2 |
+-------------+------------+------------+---------------+

Selecting All Columns Except One Column

This example shows how to select all columns in employee_table except for the department_id column:

SELECT * EXCLUDE department_id FROM employee_table;

+-------------+------------+------------+
| EMPLOYEE_ID | LAST_NAME  | FIRST_NAME |
|-------------+------------+------------|
|         101 | Montgomery | Pat        |
|         102 | Levine     | Terry      |
|         103 | Comstock   | Dana       |
+-------------+------------+------------+

Selecting All Columns Except Two or More Columns

This example shows how to select all columns in employee_table except for the department_id and employee_id columns:

SELECT * EXCLUDE (department_id, employee_id) FROM employee_table;

+------------+------------+
| LAST_NAME  | FIRST_NAME |
|------------+------------|
| Montgomery | Pat        |
| Levine     | Terry      |
| Comstock   | Dana       |
+------------+------------+

Selecting All Columns and Renaming One Column

This example shows how to select all columns in employee_table and rename the department_id column:

SELECT * RENAME department_id AS department FROM employee_table;
+-------------+------------+------------+------------+
| EMPLOYEE_ID | LAST_NAME  | FIRST_NAME | DEPARTMENT |
|-------------+------------+------------+------------|
|         101 | Montgomery | Pat        |          1 |
|         102 | Levine     | Terry      |          2 |
|         103 | Comstock   | Dana       |          2 |
+-------------+------------+------------+------------+

Selecting All Columns and Renaming Multiple Columns

This example shows how to select all columns in employee_table and rename the department_id and employee_id columns:

SELECT * RENAME (department_id AS department, employee_id AS id) FROM employee_table;

+-----+------------+------------+------------+
|  ID | LAST_NAME  | FIRST_NAME | DEPARTMENT |
|-----+------------+------------+------------|
| 101 | Montgomery | Pat        |          1 |
| 102 | Levine     | Terry      |          2 |
| 103 | Comstock   | Dana       |          2 |
+-----+------------+------------+------------+

Selecting All Columns, Excluding a Column, and Renaming Multiple Columns

This example shows how to select all columns in employee_table, exclude the first_name column, and rename the department_id and employee_id columns:

SELECT * EXCLUDE first_name RENAME (department_id AS department, employee_id AS id) FROM employee_table;

+-----+------------+------------+
|  ID | LAST_NAME  | DEPARTMENT |
|-----+------------+------------|
| 101 | Montgomery |          1 |
| 102 | Levine     |          2 |
| 103 | Comstock   |          2 |
+-----+------------+------------+

Selecting All Columns From Multiple Tables, Excluding a Column, and Renaming a Column

This example joins two tables and selects all columns from both tables except one column from employee_table. The example also renames one of the columns selected from department_table.

SELECT
  employee_table.* EXCLUDE department_id,
  department_table.* RENAME department_name AS department
FROM employee_table INNER JOIN department_table
  ON employee_table.department_id = department_table.department_id
ORDER BY department, last_name, first_name;

+-------------+------------+------------+---------------+------------------+
| EMPLOYEE_ID | LAST_NAME  | FIRST_NAME | DEPARTMENT_ID | DEPARTMENT       |
|-------------+------------+------------+---------------+------------------|
|         103 | Comstock   | Dana       |             2 | Customer Support |
|         102 | Levine     | Terry      |             2 | Customer Support |
|         101 | Montgomery | Pat        |             1 | Engineering      |
+-------------+------------+------------+---------------+------------------+

Examples of Selecting Specific Columns (SELECT colname)

Selecting a Single Column by Name

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 |
+------------+

Selecting Multiple Columns by Name From Joined Tables

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        |
+------------------+------------+------------+

Selecting a Column by Position

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 |
+------------+

Specifying an Alias for a Column in the Output

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 |
+----------------+
Back to top