Arikah Map

Select (SQL)

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

It is used to retrieve zero or more rows from one or more tables in a database. In most applications, SELECT is the most commonly used Data Manipulation Language (DML) command. In specifying a SELECT query, the user specifies a description of the desired result set, but they do not specify what physical operations must be executed to produce that result set. Translating the query into an optimal "query plan" is left to the database system, more specifically to the query optimiser.

Commonly available keywords related to SELECT include:


Contents

Examples

Table "T"QueryResult
C1 C2
1 a
2 b
SELECT * FROM T;
C1 C2
1 a
2 b
C1 C2
1 a
2 b
SELECT C1 FROM T;
C1
1
2
C1 C2
1 a
2 b
SELECT * FROM T WHERE C1 = 1;
C1 C2
1 a
C1 C2
1 a
2 b
SELECT * FROM T ORDER BY C1 DESC;
C1 C2
2 b
1 a

Given a table T, the query SELECT * FROM T; will result in all the elements of all the rows of the table being shown.

With the same table, the query SELECT C1 FROM T; will result in the elements from the column C1 of all the rows of the table being shown — in Relational algebra terms, a projection will be performed.

With the same table, the query SELECT * FROM T WHERE C1 = 1; will result in all the elements of all the rows where the value of column C1 is '1' being shown — in Relational algebra terms, a selection will be performed, because of the WHERE keyword.

The last query SELECT * FROM T ORDER BY C1 DESC; will output the same rows as the first query, however the results will be in reverse sort order (Z-A) because of the ORDER BY keyword using C1 as a sorting point. This query doesn't have a WHERE keyword, so anything and everything will be returned. Multiple ORDER BY items can be specified (separated by comma [eg. ORDER BY C1 ASC, C2 DESC]) to further refine sorting.

Limiting result rows

In ISO SQL:2003, result sets may be limited by using

ROW_NUMBER() window function

Several window functions exist. ROW_NUMBER() OVER may be used for a simple limit on the returned rows. E.g., to return no more than ten rows:

SELECT * FROM (  SELECT    ROW_NUMBER() OVER (ORDER BY key ASC) AS rownumber,    columns  FROM tablename) AS fooWHERE rownumber <= 10

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

RANK() window function

The RANK() OVER window function acts like ROW_NUMBER, but may return more than n rows in case of tie conditions. E.g., to return the top-10 youngest persons:

SELECT * FROM (  SELECT    RANK() OVER (ORDER BY age ASC) AS ranking,    person_id,    person_name,    age  FROM person) AS fooWHERE ranking <= 10

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

Non-standard syntax

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:

VendorLimit Syntax
DB2 (Supports the standard, since DB2 Version 6)
Firebird SELECT FIRST 10 * FROM T
Informix SELECT FIRST 10 * FROM T
Interbase SELECT * FROM T ROWS 10
Microsoft (Supports the standard, since SQL Server 2005)
Also SELECT TOP 10 [PERCENT] * FROM T ORDER BY col
MySQL SELECT * FROM T LIMIT 10
SQLite SELECT * FROM T LIMIT 10
PostgreSQL SELECT * FROM T LIMIT 10
Oracle (Supports the standard, since Oracle8i)
Also SELECT * from T WHERE ROWNUM <= 10

See also

Topics in database management systems (DBMS)

view    edit</span> )

Concepts
Database | Database model | Relational database | Relational model |Relational algebra | Primary key - Foreign key - Surrogate key - Superkey
Database normalization | Referential integrity | Relational DBMS | Distributed DBMS | ACID

Objects
Trigger | View | Table | Cursor | Log | Transaction |Index | Stored procedure | Partition

Topics in SQL
Select | Insert | Update | Merge |Delete | Join | Union |Create |Drop
Comparison of syntax

Implementations of database management systems

Types of implementations
Relational | Flat file | Deductive | Dimensional | Hierarchical | Object oriented | Temporal

Products
Caché | db4o | dBASE | Firebird | Helix database | DB2 | Informix | Ingres | InterBase | Microsoft SQL Server | MySQL | OpenLink Virtuoso | Oracle | PostgreSQL | SQLite | Sybase IQ | Sybase | Teradata | Visual FoxPro | Comparison - relational | Comparison - object-relational

Components
Query language | Query optimizer | Query plan | ODBC |JDBC
Lists
List of object-oriented database management systems
List of relational database management systems

Categories


SQL statements

Find

Find

Find