The order in which a query is written is not the order in which it is evaluated by SQL Server.
The query is evaluated by SQL server in following order:
The query is evaluated by SQL server in following order:
5: SELECT {select list}
1: FROM {table source}
2: WHERE {search condition}
3: GROUP BY {group by list}
4: HAVING {search condition}
6: ORDER BY {order by list}