Order of Execution of Clauses in Sql Statement.

We frequently use the SQL statements daily.But we don't the order in which the SQL statements Execute. There are different Clauses in SQL statement such as SELECT,FROM,WHERE,ORDER BY. This resource will describe what is the order of execution of these clauses. Find Order of Execution of Clauses in Sql Statement.

Learn Order of Execution of Clauses in Sql Statement.

SQL is a standard query language for accessing and manipulating databases.

SQL can execute the queries on database .
It also can retrieve data or insert data or update data from the database.

SQL can also be used to CREATE a new database. And also to create tables in database or create Stored Procedures or views.

General SQL Statement is as shown below:

SELECT table1.age ,COUNT(*) FROM table1 JOIN table2 ON table1.id=table2.id WHERE table1.id > 5 GROUP BY table1.age ORDER BY table1.id

Note: we will use this statement throughout in resource.

SQL Statements contains different Clauses.

Clauses used in SQL statements are as show Below:


Here we will Discuss how the above Query executes.

1)FROM Clause:

From the above clauses "From Clause" executes first.As in the From clause we know on which table we want to fire query.Once the table is known we can do our futher operations.And also it our first chance to filter the record set size as we can get filterd records by using ON condtion after From.

FROM table1 JOIN table2 ON table1.id=table2.id

This way, by the time we get to the WHERE clause, we will have already excluded rows where table1 and table2 id are not same.

2) Where Clause :
Where clause executes second .it is a second chance to filter the records.
Check the below statement in this condition we filter the result set by selecting only those records where id is greater than 5

WHERE table1.id > 5

if we create a column in SELECT query and try to use it in where condition we get an error as "Unknown column in Where condition " . as that column is not having reference in where condition because WHERE condition execute first and then SELECT will execute .

Select "abc" as col1 FROM table1 where col1="xyz"

3) Group By Clause :

This clause is executed after WHERE clause . This clause will Group the result set according to similar column value.

In the above query result will be grouped according to similar age.

Group By table1.age

4) Order by Clause:

Order By Clause :

Order by Clause execute on the result set which is return by SELECT clause and arranges the result set in particular order.

ORDER BY table1.id

Hope this resource will help you.


Author: Alwyn Duraisingh03 Jan 2012 Member Level: Gold   Points : 0

Kushal Zamkade,

Precious resource, which can help many viewers to understand the process while executing the Query.

Keep up the Good Work.

Author: Kushal Zamkade03 Jan 2012 Member Level: Gold   Points : 0

Thanks sir.

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: