SQL Tutorial

SELECT Statement Basics

The SQL SELECT statement queries data from tables in the database. The statement begins with the SELECT keyword. The basic SELECT statement has 3 clauses: The SELECT clause specifies the table columns that are retrieved. The FROM clause specifies the tables accessed. The WHERE clause specifies which table rows are used. The WHERE clause is optional; if missing, all table rows are used.

For example,

This query accesses rows from the table - s. It then filters those rows where the city column contains Rome. Finally, the query retrieves the name column from each filtered row. Using the example s table, this query produces: A detailed description of the query actions: The remainder of this subsection examines the 3 major clauses of the SELECT statement, detailing their syntax and semantics: Extended query capabilities are covered in the next sub-section.

SELECT Clause

The SELECT clause is mandatory. It specifies a list of columns to be retrieved from the tables in the FROM clause. It has the following general format: select-list is a list of column names separated by commas. The ALL and DISTINCT specifiers are optional. DISTINCT specifies that duplicate rows are discarded. A duplicate row is when each corresponding select-list column has the same value. The default is ALL, which retains duplicate rows.

For example,

The column names in the select list can be qualified by the appropriate table name: A column in the select list can be renamed by following the column name with the new name. For example: This produces: The select list may also contain expressions. See Expressions.

A special select list consisting of a single '*' requests all columns in all tables in the FROM clause. For example,

The * delimiter will retrieve just the columns of a single table when qualified by the table name. For example: This produces the same result as the previous example.

An unqualified * cannot be combined with other elements in the select list; it must be stand alone. However, a qualified * can be combined with other elements. For example,

Note: this is an example of a query joining 2 tables. See Joining Tables.

FROM Clause

The FROM clause always follows the SELECT clause. It lists the tables accessed by the query. For example, When the From List contains multiple tables, commas separate the table names. For example, When the From List has multiple tables, they must be joined together. See Joining Tables.

Correlation Names

Like columns in the select list, tables in the from list can be renamed by following the table name with the new name. For example, The new name is known as the correlation (or range) name for the table. Self joins require correlation names.

WHERE Clause

The WHERE clause is optional. When specified, it always follows the FROM clause. The WHERE clause filters rows from the FROM clause tables. Omitting the WHERE clause specifies that all rows are used.

Following the WHERE keyword is a logical expression, also known as a predicate.

The predicate evaluates to a SQL logical value -- true, false or unknown. The most basic predicate is a comparison:

This predicate returns: Generally, a comparison expression compares the contents of a table column to a literal, as above. A comparison expression may also compare two columns to each other. Table joins use this type of comparison. See Joining Tables.

The = (equals) comparison operator compares two values for equality. Additional comparison operators are:

For example, Note: In the sp table, the qty column for one of the rows contains null. The comparison - qty >= 200, evaluates to unknown for this row. In the final result of a query, rows with a WHERE clause evaluating to unknown (or false) are eliminated (filtered out).

Both operands of a comparison should be the same data type, however automatic conversions are performed between numeric, datetime and interval types. The CAST expression provides explicit type conversions; see Expressions.

Extended Comparisons

In addition to the basic comparisons described above, SQL supports extended comparison operators -- BETWEEN, IN, LIKE and IS NULL.

Logical Operators

The logical operators are AND, OR, NOT. They take logical expressions as operands and produce a logical result (True, False, Unknown). In logical expressions, parentheses are used for grouping.

Extended Query Capabilities

In addition to the basic capabilities covered in this sub-section, the SELECT statement also support extended capabilities, described in the next sub-section: The other SQL-Data Statements (DML) are covered in -- SQL Modification Statements:
SQL-Data Statements   SQL Tutorial Main Page

Copyright © 2002-2005 FFE Software, Inc. All Rights Reserved WorldWide