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.
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.
Precious resource, which can help many viewers to understand the process while executing the Query.
Keep up the Good Work.