Select (SQL)

The SQL SELECT statement returns a result set of records from one or more tables.

It retrieves zero or more rows from one or more base tables, temporary tables, or views in a database. In most applications,  is the most commonly used Data Manipulation Language (DML) command. As SQL is a non-procedural language,  queries specify a result set, but do not specify how to calculate it: translating the query into an executable "query plan" is left to the database system, more specifically to the query optimizer.

The SELECT statement has many optional clauses:
 * specifies which rows to retrieve.
 * groups rows sharing a property so that an aggregate function can be applied to each group.
 * selects among the groups defined by the GROUP BY clause.
 * specifies an order in which to return the rows.

Examples
{| class="wikitable" style="float: right; clear:right; margin: 1em" border="1" !Table "T" !Query !Result
 * align="center"|
 * align="center"|
 * align="center"|
 * align="center"|


 * align="center"|
 * align="center"|
 * align="center"|
 * align="center"|
 * align="center"|
 * align="center"|

Given a table T, the query  will result in all the elements of all the rows of the table being shown.
 * align="center"|
 * align="center"|
 * align="center"|
 * }
 * align="center"|
 * }
 * }

With the same table, the query  will result in the elements from the column C1 of all the rows of the table being shown. This is similar to a projection in Relational algebra, except that in the general case, the result may contain duplicate rows. This is also known as a Vertical Partition in some database terms, restricting query output to view only specified fields or columns.

With the same table, the query  will result in all the elements of all the rows where the value of column C1 is '1' being shown &mdash; in Relational algebra terms, a selection will be performed, because of the WHERE clause. This is also known as a Horizontal Partition, restricting rows output by a query according to specified conditions.

Limiting result rows
Often it is convenient to indicate a maximum number of rows that are returned. This can be used for testing or to prevent consuming excessive resources if the query returns more information than expected. The approach to do this often varies per vendor.

In ISO SQL:2003, result sets may be limited by using
 * cursors, or
 * By introducing SQL window function to the SELECT-statement

ROW_NUMBER window function
may be used for a simple limit on the returned rows. For example, to return no more than ten rows:

ROW_NUMBER can be non-deterministic: if sort_key is not unique, each time you run the query it is possible to get different row numbers assigned to any rows where sort_key is the same. When sort_key is unique, each row will always get a unique row number.

RANK window function
The  window function acts like ROW_NUMBER, but may return more than n rows in case of tie conditions. For example, to return the top 10 youngest persons:

The above code could return more than ten rows, for example, if there are two people of the same age, it could return eleven rows.

Result limits
Not all DBMSes support the mentioned window functions, and non-standard syntax has to be used. Below, variants of the simple limit query for different DBMSes are listed:

Hierarchical query
Some databases provide specialised syntax for hierarchical data.

Window function
A window function in SQL:2003 is an aggregate function applied to a partition of the result set.

For example,

sum(population) OVER( PARTITION BY city )

calculates the sum of the populations of all rows having the same city value as the current row.

Partitions are specified using the OVER clause which modifies the aggregate. Syntax:

 :: = OVER ( [ PARTITION BY, ... ]          [ ORDER BY ] )

The OVER clause can partition and order the result set. Ordering is used for order-relative functions such as row_number.