Order Of Execution Of SQL Queries

When we ask a developer what was the first language he Learned/ Worked when he joined the IT profession, he would most likely say its SQL and he will surely say he has written his

When we ask a developer what was the first language he Learned/ Worked when he joined the IT profession, he would most likely say its SQL and he will surely say he has written his share of complex queries. But very few really understand how a query we write is executed. By understanding the order of execution we can improve the performance of the queries we write. This very understanding will differentiate a good Developer from a normal Developer. Lets quickly look in the order of execution of SQL quires.

Order of Execution:

OrderClauseFunction
1FromChoose and join tables to get base data.
2WhereFilters the base data.
3Group byAggregates the base data.
4HavingFilters the aggregated data.
5SelectReturns the final data.
6Order bySorts the final data.
7Limit/OFFSETLimits the returned data to a row count.

FROM:

These are the first statement to be executed to fetch a working set of data that will eventually be modified to resultant set. So if we have multiple tables linked with the on clause then they will be executed in Top to bottom approach with linking the tables with on clause.

WHERE:

The Result from the FROM clause is stored in a virtual table which is then tested with the where conditions. All the rows which satisfy the WHERE conditions are kept while the rest of the data is discarded.

GROUP BY:

Now the new result set will be grouped based on the common values in the columns which are specified in the GROUP BY clause. GROUP BY will only work when we have aggregate functions and it will return a result set with only distinct values.

HAVING:

After Grouping the result set having clause is tested and only the rows which satisfy HAVING clause will be kept while other rows are discarded.

SELECT:

Now all the SELECT logic’s like (case, aliasing, as, distinct) will be applied on the result set. Only the columns specified in the select list will be kept which other columns are discarded.

ORDER BY:

If ORDER BY is specified then rows are arranged as per the defied order.

LIMIT/OFFSET:

The rows that fall outside the range specified by the LIMIT and OFFSET are discarded and the final result is returned back.

5 thoughts on “Order Of Execution Of SQL Queries

  1. Gооd dаy! tejaxspace.com

    Did yоu knоw thаt it is pоssiblе tо sеnd prоpоsаl whоlly lаwful?
    Wе suggеsting а nеw wаy оf sеnding lеttеr thrоugh fееdbасk fоrms. Suсh fоrms аrе lосаtеd оn mаny sitеs.
    Whеn suсh аppеаl аrе sеnt, nо pеrsоnаl dаtа is usеd, аnd mеssаgеs аrе sеnt tо fоrms spесifiсаlly dеsignеd tо rесеivе mеssаgеs аnd аppеаls.
    аlsо, mеssаgеs sеnt thrоugh fееdbасk Fоrms dо nоt gеt intо spаm bесаusе suсh mеssаgеs аrе соnsidеrеd impоrtаnt.
    Wе оffеr yоu tо tеst оur sеrviсе fоr frее. Wе will sеnd up tо 50,000 mеssаgеs fоr yоu.
    Thе соst оf sеnding оnе milliоn mеssаgеs is 49 USD.

    This mеssаgе is сrеаtеd аutоmаtiсаlly. Plеаsе usе thе соntасt dеtаils bеlоw tо соntасt us.

    Contact us.
    Telegram – @FeedbackFormEU
    Skype FeedbackForm2019
    WhatsApp – +375259112693

Leave a Reply

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