select_clause specifies the columns from which results are to be returned by the query. See
“Select Clause” for a complete explanation.
from_clause specifies one or more tables on which the other clauses in the query operate. See
“From Clause” for a complete explanation.
where_clause is optional and restricts the results that are returned by the query. See
“Where Clause” for a complete explanation.
groupby_clause is optional and allows query results to be aggregated in terms of groups. See
“Group By Clause” for a complete explanation.
having_clause is optional and specifies conditions for groups of rows (for example, display only the departments that have salaries totaling more than $200,000). See
“Having Clause” for a complete explanation.
UNION is an optional operator that combines the results of the left and right Select statements into a single result. See
“Union Operator” for a complete explanation.
INTERSECT is an optional operator that returns a single result by keeping any distinct values from the results of the left and right Select statements. See
“Intersect Operator” for a complete explanation.
EXCEPT and
MINUS are synonymous optional operators that returns a single result by taking the results of the left Select statement and removing the results of the right Select statement. See
“Except and Minus Operators” for a complete explanation.
orderby_clause is optional and sorts the results that are returned by the query. See
“Order By Clause” for a complete explanation.
limit_clause is optional and places an upper bound on the number of rows returned in the result. See
“Limit Clause” for a complete explanation.
SELECT [{LIMIT offset number | TOP
number}] [ALL | DISTINCT] {* |
column_expression [[AS]
column_alias] [,
column_expression [[AS]
column_alias], ...]}
LIMIT offset number creates the result set for the Select statement first and then discards the first number of rows specified by
offset and returns the number of remaining rows specified by
number. To not discard any of the rows, specify
0 for
offset, for example,
LIMIT 0 number. To discard the first
offset number of rows and return all the remaining rows, specify
0 for
number, for example, LIMIT
offset 0.
TOP number is equivalent to
LIMIT 0 number.
column_expression can be simply a column name (for example,
last_name). More complex expressions may include mathematical operations or string manipulation (for example,
salary * 1.05). See
“SQL Expressions” for details.
column_expression can also include aggregate functions. See
“Aggregate Functions” for details.
column_alias can be used to give the column a descriptive name. For example, to assign the alias department to the column dep:
Column names can be prefixed with the table name or table alias. For example, SELECT emp.last_name or e.last_name, where
e is the alias for the table
emp.
The DISTINCT operator can precede the first column expression. This operator eliminates duplicate rows from the result of a query. For example:
The INTO clause copies the result set into
new_table.
INTO DISK creates the new table in cached memory.
INTO TEMP creates a temporary table.
The result of a query can be the result of one or more aggregate functions. Aggregate functions return a single value from a set of rows. An aggregate can be used with a column name (for example,
AVG(salary)) or in combination with a more complex column expression (for example,
AVG(salary * 1.07)). The column expression can be preceded by the
DISTINCT operator. The
DISTINCT operator eliminates duplicate values from an aggregate expression.
Table 10-3 lists supported aggregate functions.
Except for COUNT(*), all aggregate functions exclude NULL values. The returned value type for
COUNT is INTEGER and for
MIN,
MAX, and
AVG it is the same type as the column.
The following example uses the COUNT,
MAX, and
AVG aggregate functions:
FROM table_name [
table_alias] [,...]
table_name is the name of a table or a subquery. Multiple tables define an implicit inner join among those tables. Multiple table names must be separated by a comma. For example:
table_alias is a name used to refer to a table in the rest of the Select statement. When you specify an alias for a table, you can prefix all column names of that table with the table alias.
You can use a Join as a way to associate multiple tables within a Select statement. Joins may be either explicit or implicit. For example, the following is the example from the previous section restated as an explicit inner join:
FROM table_name {RIGHT OUTER | INNER | LEFT OUTER | CROSS} JOIN
table.key ON
search-condition
In this example, two tables are joined using LEFT OUTER JOIN.
T1, the first table named includes nonmatching rows.
If you use a CROSS JOIN, no
ON expression is allowed for the join.
WHERE expr1 rel_operator expr2
expr1 is either a column name, literal, or expression.
expr2 is either a column name, literal, expression, or subquery. Subqueries must be enclosed in parentheses. See
“Subqueries” for complete information about subqueries.
rel_operator is the relational operator that links the two expressions.
See “SQL Expressions” for details about expressions.
column_expression is either a column name or a SQL expression (see
“SQL Expressions” for details). Multiple values must be separated by a comma. If
column_expression is a column name, it must match one of the column names specified in the Select clause. Also, the Group By clause must include all non-aggregate columns specified in the Select list.
The Having clause specifies conditions for groups of rows (for example, display only the departments that have salaries totaling more than $200,000). This clause is valid only if you have already defined a Group By clause.
HAVING expr1 rel_operator expr2
expr1 and
expr2 can be column names, constant values, or expressions. These expressions do not have to match a column expression in the Select clause. See
“SQL Expressions” for details regarding SQL expressions.
rel_operator is the relational operator that links the two expressions.
The Union operator combines the results of two Select statements into a single result. The single result is all the returned rows from both Select statements. By default, duplicate rows are not returned. To return duplicate rows, use the All keyword (
UNION ALL).
When using the Union operator, the Select lists for each Select statement must have the same number of column expressions with the same data types and must be specified in the same order.
The following example is not valid because the data types of the column expressions are different (
salary FROM emp has a different data type than
last_name FROM raises). This example does have the same number of column expressions in each Select statement but the expressions are not in the same order by data type.
When using the Intersect operator, the Select lists for each Select statement must have the same number of column expressions with the same data types and must be specified in the same order.
The following example is not valid because the data types of the column expressions are different (
salary FROM emp has a different data type than
last_name FROM raises). This example does have the same number of column expressions in each Select statement but the expressions are not in the same order by data type.
The DISTINCT operator eliminates duplicate rows from the results.
When using one of these operators, the Select lists for each Select statement must have the same number of column expressions with the same data types and must be specified in the same order.
The following example is not valid because the data types of the column expressions are different (
salary FROM emp has a different data type than
last_name FROM raises). This example does have the same number of column expressions in each Select statement but the expressions are not in the same order by data type.
ORDER BY sort_expression [DESC | ASC] [,...]
sort_expression is either the name of a column, a column alias, a SQL expression, or the positioned number of the column or expression in the select list to use. See
“SQL Expressions” for details regarding SQL expressions.
For example, to sort by last_name and then by
first_name, you could use either of the following Select statements:
In the second example, last_name is the second item in the Select list, so
ORDER BY 2,3 sorts by
last_name and then by
first_name.
LIMIT number_of_rows [OFFSET
offset_number]
number_of_rows specifies a maximum number of rows in the result. A negative number indicates no upper bound.
The OFFSET operator specifies how many rows to skip at the beginning of the result set.
offset_number is the number of rows to skip.
In a compound query, the Limit clause can appear only on the final Select statement. The limit is applied to the entire query, not to the individual Select statement to which it is attached.