Name

SELECT

Synopsis

The SELECT statement retrieves rows, columns, and derived values from one or many tables of a database.

Vendor

Command

SQL Server

Supported, with variations (ANSI joins supported)

MySQL

Supported, with variations (ANSI joins partially supported)

Oracle

Supported, with variations (ANSI joins not supported)

PostgreSQL

Supported, with variations (ANSI joins partially supported)

SQL99 Syntax and Description

The full syntax of the SELECT statement is powerful and complex, but can be broken down into these main clauses:

SELECT [ALL | DISTINCT] select_list
FROM table_name1 [,..., table_nameN]
[JOIN join_condition]
[WHERE search_condition]
[GROUP BY group_by_expression]
[HAVING search_condition]
[ORDER BY order_expression [ASC | DESC] ]

Each clause of the SELECT statement has a specific use. Thus, it is possible to speak individually of the FROM clause, the WHERE clause, or the GROUP BY clause. However, not every query needs every clause. At a minimum, a query needs a SELECT item list and a FROM clause. (Microsoft SQL Server and PostgreSQL both support certain types of queries that do not need a FROM clause. Refer to the examples below for more information.)

The SELECT item list

The SELECT item list basically includes all items of information a user wants to retrieve from the server. Different types of elements can appear in the select item list. It’s possible to retrieve literal strings, aggregate functions, and mathematical calculations. In Microsoft SQL ...

Get SQL in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.