Order of SQL Statements

Whenever we write any SQL query , we always think about the result set which it will produce. However it is equally important to understand how the result set is built whenever any SQL Query is executed. For this, we should know the order of execution of any SQL Query. The standard format of any SQL Query is like :

SELECT column_name FROM table_name WHERE column='condition' ORDER BY columns GROUP BY column_names HAVING some_condition;
ORDER OF SQL STATEMENTS
ORDER OF SQL STATEMENTS

Here we can see that there are several SQL clauses used in the query. However knowledge of order in which these SQL Clauses are executed is important. So, first WHERE clause is executed , then GROUP BY followed by HAVING. Once the data is ready then ORDER BY sorts the data and then SELECT clause is executed to retrieve only those columns which are specified.

So the order is:

WHERE-->GROUP BY-->HAVING-->ORDER BY-->SELECT

One thought on “Order of SQL Statements

  1. What about the From clause?? Specifically FROM and JOIN ON with either OR/AND or CASE filter versus having WHERE as an outside filter clause.

Leave a Reply

Your email address will not be published. Required fields are marked *