The steps below is the logical flow of how a SQL statement is processed

 

A)    Cross Join the tables. If the query involves more than a table, the first two tables are cross-joined and the result set is then joined with the third table and so on and so forth till all the tables are joined together

 

B)    Apply the ON condition in INNER / LEFT / RIGHT joins

 

C)    Include outer rows that were eliminated part of Step B. However, this applies only to OUTER joins

 

D)    The conditions specified in WHERE is then processed.

Scenarios 2, 4 and 5 all apply to this condition

 

E)    Group BY

 

F)    HAVING clause. Any aggregation mentioned in the HAVING requires a GROUP BY

 

G)    SELECT list. The select list is processed only after HAVING. Any co-related subquery specified in the SELECT will not eliminate any rows.