Execution Order of SQL Statement - ByteScout
  • Home
  • /
  • Blog
  • /
  • Execution Order of SQL Statement

Execution Order of SQL Statement

Execution Order of SQL Statement

SQL is one of the most effective tools. The SQL ordering of implementation specifies the decree in which the clauses of a query are considered. Some of the most typical query troubles people fly into could be effortlessly bypassed with more precise knowledge of the SQL order of performance, occasionally dubbed the SQL order of functions. Learning SQL query order can assist users to detect why a query won’t execute, and even more often will allow them to optimize their queries to run quickly.

In the current SQL world, SQL query planners can accomplish all types of ploys to push queries to execute more effectively, but they must consistently achieve the exact definitive response as a query that is fired per the traditional SQL order of performance.

The important point to remember always is that it is a robust method to restrict or pre-aggregate tables before possibly extensive joins, which can otherwise be significantly memory-based. Many contemporary SQL planners employ reasoning and additional kinds of joins to assist in optimizing for various queries, which can be useful but shouldn’t be counted on.

Choosing the perfect SQL order of functions is also critical if coders like to execute efficient, practical queries. The availability and execution of a database are what matter to the end-users. They like to swiftly use methods and want an elevated status of implementation that results in rapid response duration.

Current databases are complicated creatures that give numerous possibilities for errors caused in the configuration that can direct to reduced performance. Ill-written SQL queries can pull down database response time.

The Problem

Consider below SQL query in AdventureWorks2014 database.

Case 1

The above query results in an error, saying “Invalid column name ‘Code'”. Here our query doesn’t recognize the alias name ‘Code’ in where condition.

Now, let’s consider another query.

Case 2

And this time it gives the below output. This time it recognizes the alias name ‘Code’ in order by clause.

The Solution

In order to know this dual behavior of SQL Server, we need to know about the execution order of SQL Queries.

The logical processing order of the select statement is as below. For more details refer to this link.

1 FROM
2 ON
3 JOIN
4 WHERE
5 GROUP BY
6 WITH CUBE or WITH ROLLUP
7 HAVING
8 SELECT
9 DISTINCT
10 ORDER BY
11 TOP

Here, it is now clear that logical processing of where clause occurs prior to select clause. So alias defined in the select clause is not accessible in where clause. That’s the reason for case-1 behavior.

And in case-2 it is not giving an error, the reason behind is also the same. Logical processing of the order by clause occurs after logical processing of select clause. So alias columns are accessible in order by clause.

   

About the Author

ByteScout Team ByteScout Team of Writers ByteScout has a team of professional writers proficient in different technical topics. We select the best writers to cover interesting and trending topics for our readers. We love developers and we hope our articles help you learn about programming and programmers.  
prev
next