The SELECT statement can be constructed with various clauses as per the requirement. It can be as simple as just a set of columns from a table or as complex as a set columns from multiple tables with aggregate functions. Understanding how SQL Server executes added statements will be worthwhile for constructing the SELECT quickly and correctly as per the requirement. Here is the logical processing order in SELECT statement;
Note that all clauses have not been used with above code. Here is the order of all possible phases;
As you see, it is not the order we have written the query, hence keeping this order in mind is absolutely helpful.
Good that you posted this, since I'v seen a lot of newbies who think otherwise.
ReplyDeleteI always make sure that the order of SELECT execution is taught at the beginning of my SQL classes.
I make them write SQLs by starting with SELECT * FROM and then once the rest of the query is done, only then start adding in the required columns to the SELECT list
Once they get this into their heads at the beginning, then T-SQL more like a piece of cake.