The query specification represents the result of a complex database query. The most common SELECT statements are just query specifications.
<query specification> ::= SELECT ( DISTINCT | ALL )? <select list> <from clause> ( <where clause> )? ( <group by clause> )? ( <having clause> )?.
The result of a <query specification> is constructed as follows:
The <from clause> is evaluated.
If a <where clause> is specified, the result is filtered by applying the <search condition> of the <where clause> . The new result has the same degree as the previous result.
If a <group by clause> is specified, the specified grouping is performed on the result. The new result has the same degree as the previous result.
If a <having clause> is specified, the result is filtered by applying the <search condition> of the <having clause> . The new result has the same degree as the previous result.
The <value expression> s in the <select list> is calculated. The new result is a table that has the degree and the <display name> s of the <select list> .
If DISTINCT is specified, all duplicate rows from the result are filtered. Else the result remains unchanged. (Specifying ALL has no effect.)
DISTINCT may be specified only once in a <query specification> .