The logical execution order of an SQL query is defined by the following sequences:
- The
FROM
clause is first evaluated to get the output relation - Then
WHERE
predicate is applied to the records in the output relation. IfWHERE
is not present, the predicate isTrue
- Records satisfies the
WHERE
clause then placed into groups according to theGROUPBY
clause. IfGROUPBY
clause is not present, all records will be placed into one group - Then groups will filtered acoording to
HAVING
clause. If it is not present, the predicate isTrue
- Then the
SELECT
clause uses the remaining groups to generate results and, if they exist, apply aggregate functions on the results - Generated results will be sorted according to
ORDER by
clause if it is present. Otherwise, the results will be displayed randomly - Finally, if
LIMIT
is present, display the number of records requested in theLIMIT
clause