The logical execution order of an SQL query is defined by the following sequences:
- The
FROMclause is first evaluated to get the output relation - Then
WHEREpredicate is applied to the records in the output relation. IfWHEREis not present, the predicate isTrue - Records satisfies the
WHEREclause then placed into groups according to theGROUPBYclause. IfGROUPBYclause is not present, all records will be placed into one group - Then groups will filtered acoording to
HAVINGclause. If it is not present, the predicate isTrue - Then the
SELECTclause 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 byclause if it is present. Otherwise, the results will be displayed randomly - Finally, if
LIMITis present, display the number of records requested in theLIMITclause