SQL Language Guide : 8. SQL Statements : SELECT (Interactive) : Query Evaluation
 
Share this page                  
Query Evaluation
The logic applied to the evaluation of SELECT statements, as described here, does not precisely reflect how the DBMS Server evaluates your query to determine the most efficient way to return results. However, by applying this logic to your queries and data, the results of your queries can be anticipated.
1. Evaluate the FROM clause.
Combine all the sources specified in the FROM clause to create a Cartesian product (a table composed of all the rows and columns of the sources). If joins are specified, evaluate each join to obtain its results table, combine it with the other sources in the FROM clause. If SELECT DISTINCT is specified, discard duplicate rows.
2. Apply the WHERE clause.
Discard rows in the result table that do not fulfill the restrictions specified in the WHERE clause.
3. Apply the GROUP BY clause.
Group results according to the columns specified in the GROUP BY clause.
4. Apply the HAVING clause.
Discard rows in the result table that do not fulfill the restrictions specified in the HAVING clause.
5. Evaluate the SELECT clause.
Discard columns that are not specified in the SELECT clause. (In case of SELECT FIRST n… UNION SELECT …, the first n rows of the result from union are chosen.)
6. Perform any intersections, unions, or exceptions.
Combine result tables as specified in the UNION, INTERSECT, or EXCEPT clause.
INTERSECT takes precedence over UNION and EXCEPT. For example, in SELECT…UNION SELECT …INTERSECT SELECT… will perform the INTERSECT first, then the UNION.
In case of SELECT FIRST n… UNION SELECT …, the first n rows of the result from union are chosen.
7. Apply the ORDER BY clause.
Sort the result rows as specified.