|

SQL Statement Query Clause Evaluation Order – Which Part of SQL Statements Execute First and in What Order?

Which Part of SQL Statements Execute First and in What Order.

It is very important to understand which parts of the SQL Statement executes first and which part comes next until full completion.

The diagram below gives an overview of the execution order.

sql-query-clause-evaluation-order

The steps in which the SQL statement is executed are as below:

Step 1: FROM clause (cross product and join operators)

Step 2: WHERE clause (row conditions)

Step 3: GROUP BY clause (sort on grouping columns, compute aggregates)

Step 4: HAVING clause (group conditions)

Step 5: ORDER BY clause

Step 6: eliminate columns not in SELECT

This is Conceptual not actual as DBMS use many shortcuts

A bit of advice is that.

Row operations in the FROM and WHERE CLAUSES occur first and Errors in formulation usually occur in row operations. Hence it is advisable to always use small tables to understand relationship of rows operations (FROM , WHERE) and from there move on to the group operations (which are GROUP , HAVING operations ).

For large problems, execute row operations separately to ensure that results are being returned are what you expect before grouping the data.

Grouping only occurs one time.

This  advice and tip is part of Data Warehousing for Business Intelligence by  University of Colorado System on Coursera

Want more information like this?

Similar Posts

  • |

    Pyodbc Sqlalchemy Python 2 and 3 SQL Server Native Client 11.0

    This is a sample code to use Python to connect to SQL Server Native Client.

    Want more information like this?

  • |

    Creating Oracle PL/SQL Tables for Intercollegiate Athletic Database – Data Warehousing

    Creating Oracle  tables for Intercollegiate Athletic Database. Part of Assignment one of Data Warehousing for Business Intelligence on Coursera

    After creating the tables lets insert values

      In case you want to change a constraint on a field, you can use a syntax examples as the following. ALTER TABLE FACILITY ADD CONSTRAINT UNQ_FAC UNIQUE (FACNAME)…

  • | |

    Using Subqueries To Join 2 More Tables Which Do Not Have Unique Primary and Foreign Keys

    Using Subqueries to join 2 more tables which do not have distinct values in a column We want to find the number of Flight bookings by Country, number of Car bookings by Country and the number of Hotel bookings by Country all appearing in one table with same country on one row. We want to…

  • | | | | |

    Java Python R SQL Excel Compared Similarities For Data Science and Data Analytics – The Basics

    If you have ever worked with Java, Python, R, SQL, Excel and other Languages on a varied Data Science or Data Analytics projects, you will realise that all these languages have similar syntaxes, or at least, can achieve the same objective with very similar codes.   Below is a comparison and similarities of these various tools…

  • |

    Splitting Values in one Column to Multiple Columns using CASE Expression Statement SQL Server

    Sometimes, you would want to split values in a column into separate rows  in SQL server.  This is a simple guide. Lets check the fields in the table we will be working with.

    Let’s check the result:   We can see that the itemType field has different values wheres as the price and quantity…

Leave a Reply

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