|
|
The SELECT Expression
A
select-expressiondefines a set of data (rows and columns) extracted from one or more tables or views.The
select-expressionsyntax is:where
select-expression-bodyis:where the
select-specificationsyntax is:The different clauses in the specifications above are described in detail in the following sections.
The SELECT Clause
The
SELECTclause defines which values are to be selected. Values are specified by column references or expressions; where columns are addressed, the value selected is the content of the column.SELECT *
This form of the
SELECTclause specifies all columns in the Cartesian product of the tables specified in theFROMclause. The single asterisk may not be combined with any other value specification.Example
SELECT * FROM countries ...Note: Use of SELECT * is discouraged in programs (except in EXISTS predicates) since the asterisk is expanded to a column list when the statement is compiled, and any subsequent alterations in the table or view definitions may cause the program to function incorrectly.
SELECT table.*
If a named table or view (
table-nameorcorrelation-name) is followed by an asterisk in theSELECTclause, all columns are selected from that table or view.This formulation may be used in a list of select specifications.
If a
correlation-nameis used, it must be defined in the associatedFROMclause, see The FROM Clause and Table-reference.Note: Use of SELECT table.* is discouraged in programs (except in EXISTS predicates) since the asterisk is expanded to a column list when the statement is compiled, and any subsequent alterations in the table or view definitions may cause the program to function incorrectly.
SELECT expression
Values to be selected may be specified as expressions (using column-references, set functions and literals, see Expressions).
Column names used in expressions must refer to columns in the tables addressed in the
FROMclause or be an outer reference.A column name must be qualified if more than one column in the set of table references addressed in the
FROMclause has the same name.SELECT ... AS Column-label
A
column-labelmay be added after each separate expression in theSELECTclause.column-labelis an SQL identifier which becomes the name of the column in the result set.If no name is given the original column name is used, unless the new column was created by an expression, in which case the new column has no name.
For example,
SELECT COLUMN_NAMEwould result in a column calledCOLUMN_NAMEin the result set, butSELECT COLUMN_NAME + 1would result in a column in the result set with no name.The Keywords ALL and DISTINCT
If
ALLis specified or if no keyword is given, duplicate rows are not eliminated from the result of theselect-specification.If
DISTINCTis specified, duplicate rows are eliminated.NULLis considered to be equal toNULLin this context.The FROM Clause and Table-reference
The
FROMclause defines an intermediate result set for the select-specification, and may define correlation names for the table references used in the result set.
Mimer SQL Engine
Mimer SQL Engine does not support the( select-expression )part of thetable-referencesyntax. (I.e. select in from list is not supported.)General Syntax
All source tables and views referenced in the
SELECTclause and at the top level in theWHEREclause (but not in any subselect used in theWHEREclause) must be named in theFROMclause.Intermediate Result Sets
If a single table or view is named in the
FROMclause, the intermediate result set is identical to the table or view.If the
FROMclause names more than one table or view, the intermediate result set may be regarded as the complete Cartesian product of the named tables or views.Note: The intermediate result set is a conceptual entity, introduced to aid in understanding of the selection process. The complete result set does not have any direct physical existence, so that the machine resources available do not need to correspond to the (sometimes very large) Cartesian product tables implied by multiple table references in a FROM clause.
Correlation Names
Correlation names introduced in the
FROMclause redefine the form of the table name which may be used to qualify column names, see Qualified Object Names.Correlation names may be used for several purposes:
- to shorten table names, which saves typing and makes statements easier to follow and less error-prone.
- to relate a table to a logical copy of itself.
- to rename a column when a column with the same name exists in another of the query's tables.
A table or view name is exposed in the
FROMclause if it does not have a correlation name. The same table or view name cannot be exposed more than once in the sameFROMclause.The same correlation name may not be introduced more than once in the same
FROMclause, and it cannot be the same as an exposed table or view name.The WHERE Clause
The
WHEREclause selects a subset of the rows in the intermediate result set on the basis of values in the columns. If noWHEREclause is specified, all rows of the intermediate result set are selected.All column references in the
search-conditionmust uniquely identify a column in the intermediate result set defined by theFROMclause or be an outer reference.Column references must be qualified if more than one column in the intermediate result set has the same name, or if the column is an outer reference.
The GROUP BY Clause
The
GROUP BYclause determines grouping of the result table for the application of set functions specified in theSELECTclause.The
GROUP BYclause has the following syntax:If a
GROUP BYclause is specified, each column reference in theSELECTlist must either identify a grouping column or be the argument of a set function.The rows of the intermediate result set are (conceptually) arranged in groups, where all values in the grouping column(s) are identical within each group.
Each group is reduced to a single row in the final result of the select-specification.
If a
GROUP BYclause is not specified, theSELECTlist must either be a list that does not include any set functions or a list of set functions and optional literal expressions.The COLLATE Clause
Collations determine the sort order of character data. If the
COLLATEclause is specified, the resulting data will be grouped according to the collation specified. For more information, see the Mimer SQL User's Manual, Collations.If no
COLLATEclause is specified, the column's implicit collation will be used.The HAVING Clause
The
HAVINGclause restricts selection of groups in the same way that aWHEREclause restricts selection of rows.The
HAVINGclause has the following syntax:The search condition in the
HAVINGclause defines restrictions on the values in the elements of theSELECTlist. Column references in the search condition of theHAVINGclause must identify a grouping column, or be used in set functions, or be outer references.Most commonly,
HAVINGis used together withGROUP BY, in which case the search conditions relate either values in grouping columns or results of set functions to expressions.If the
HAVINGclause is used without aGROUP BYclause, all rows in the result table are treated as a single group. In this case, theHAVINGclause must refer to a set function (since there are no grouping columns).The UNION Operator
If several
SELECTstatements are connected byUNIONorUNION DISTINCT, the result is derived by first merging all result tables specified by the separateSELECTstatements, and then eliminating duplicate rows from the merged set. All columns in the result table are significant for the purpose of eliminating duplicates.The
UNION ALLoperator on the other hand retains all duplicates. The operator can be viewed as a way to concatenate several queries.The rules described below apply to both
UNIONandUNION ALL.All separate result tables from
SELECTstatements connected byUNIONmust have the same number of columns and the data types of columns to be merged must be compatible.The columns in the result table are named in accordance with the columns in the first
SELECTstatement of theUNIONconstruction.Separate
SELECTstatements may be enclosed in parentheses if desired. This does not affect the result of aUNIONoperation.The names in the first select specification are used in
UNIONconstructions.See Result Data Types for a description of how the data type of the
UNIONresult is determined.The INTERSECT Operator
If several
SELECTstatements are connected byINTERSECTorINTERSECT DISTINCT, the result is derived by taking the results of two queries and return only rows that appear in both result sets, and then eliminating duplicate rows from the merged set. All columns in the result table are significant for the purpose of eliminating duplicates.The
INTERSECT ALLoperator on the other hand retains all duplicates.The rules described below apply to both
INTERSECTandINTERSECT ALL.All separate result tables from
SELECTstatements connected byINTERSECTmust have the same number of columns and the data types of columns to be merged must be compatible.The columns in the result table are named in accordance with the columns in the first
SELECTstatement of theINTERSECTconstruction.Separate
SELECTstatements may be enclosed in parentheses if desired. This does not affect the result of aINTERSECToperation.The names in the first select specification are used in
INTERSECTconstructions.See Result Data Types for a description of how the data type of the
INTERSECTresult is determined.The EXCEPT Operator
If several
SELECTstatements are connected byEXCEPTorEXCEPT DISTINCT, the result is derived by taking the distinct rows of the first query and return the rows that do not appear in second result query. All columns in the result table are significant for the purpose of eliminating duplicates.The
EXCEPT ALLoperator on the other hand retains all duplicates.The rules described below apply to both
EXCEPTandEXCEPT ALL.All separate result tables from
SELECTstatements connected byEXCEPTmust have the same number of columns and the data types of columns to be merged must be compatible.The columns in the result table are named in accordance with the columns in the first
SELECTstatement of theEXCEPTconstruction.Separate
SELECTstatements may be enclosed in parentheses if desired. This does not affect the result of aEXCEPToperation.The names in the first select specification are used in
EXCEPTconstructions.See Result Data Types for a description of how the data type of the
EXCEPTresult is determined.The ORDER BY Clause
The result table may be ordered according to an
order-by-clause.The
ORDER BYclause has the following syntax:Every expression in the
order-by-clausemust contain a reference to a column in a table specified in theFROMclause.Column labels, created with
SELECT AS, may not be part of a complexORDER BYexpression, (i.e. if column label is used, the expression must contain nothing but the column label).The
ORDER BYexpressions must not include set functions (i.e.MAX,MIN,AVG,SUMandCOUNT), subqueries orNEXT VALUE FORsequence.If
DISTINCT,GROUP BYorUNION,EXCEPTorINTERSECTis specified, only columns from the result set may be specified asORDER BYexpressions.The default collation for sorting data is the collation defined for the column being sorted. If you include a
COLLATEclause, you can override the default collation by explicitly specifying a different collation. For more information, see the Mimer SQL User's Manual, Collations.Ascending/Descending
For each column in the
order-by-clause, the sort order may be specified asASC(ascending) - the default, orDESC(descending). If more than one column is specified, the result table is ordered first by values in the first specified column, then by values in the second, and so on.
Mimer SQL Engine
Only top-levelorder-by-clauseis supported. (I.e.ORDER BYis not supported in subqueries.)The RESULT OFFSET Clause
The
result-offset-clauseis used to limit the result set by removing a specified number of rows from its beginning.The
result-offset-clauseclause has the following syntax:If a statement contains both an
order-by-clauseand aresult-offset-clause, the result set is first sorted according to theORDER BYclause, and then the number of rows specified in theresult-offset-clauseare removed.
Mimer SQL Engine
Only top-levelresult-offset-clauseis supported. (I.e.OFFSETis not supported in subqueries.)Only an integer value or a host-variable can be used to specify the OFFSET value.The FETCH FIRST Clause
The
fetch-first-clauseis used to limit the result set by specifying the number of rows to be returned.The
fetch-first-clausehas the following syntax:If a statement contains both an
order-by-clauseand afetch-first-clause, the result set is first sorted according to theorder-by-clauseand then limited to the number of rows specified in thefetch-first-clause.If both a
result-offset-clauseand afetch-first-clauseare specified, theresult-offset-clauseis applied first, then thefetch-first-clause.Restrictions
SELECTaccess is required on all tables and views specified in aFROMclause.Notes
If the
SELECTstatement is used without theORDER BYclause, the sort order is undefined. This means that the sort order may change if new indexes are created, indexes are dropped, new statistics are gathered or if a new version of the SQL optimizer is installed.Standard Compliance
This section summarizes standard compliance for
select-specifications.
|
Mimer Information Technology AB Voice: +46 18 780 92 00 Fax: +46 18 780 92 40 info@mimer.se |
|
|