Ever felt confused by SQL's execution flow? π€
Then you better stay with me!
Today let's exemplify SQL's execution order with a simple queryππ»
1οΈβ£ π¦π§ππ₯π§ππ‘π ππ₯π’π π’π¨π₯ π₯ππͺ π§ππππ
We use a dummy table with the salary of employees depending on their field and experience,
π― Our main goal?
Understand the field that earns the most.
2οΈβ£ π¦π€π π€π¨ππ₯π¬ π¦π§π₯π¨ππ§π¨π₯π (to use)
We define a query to obtain our goal data.
3οΈβ£ π¦π€π ππ¦ π ππππππ₯ππ§ππ©π πππ‘ππ¨πππ
SQL expects statements to be written in a specific orderβ...
but their evaluation sequence differs.
π¦π§ππ£ 1 - ππ₯π’π
First SQL determines the table where to take the data from.
In our case our dummy table salaries with all the info it contains.
π¦π§ππ£ 2 - πͺπππ₯π
Here data is filtered.
This is where we specify conditions to narrow down our results.
Think of it as our detective's magnifying glass! π
π¦π§ππ£ 3 - ππ₯π’π¨π£ ππ¬
Grouping time!
This step clusters similar data together based on a specified column.
Perfect for summarizing data like employee fields! π
π¦π§ππ£ 4 - πππ©ππ‘π
It's like WHERE's big sibling.
HAVING filters groups, especially useful after GROUP BY.
Only the groups meeting our criteria will pass this gate.
π¦π§ππ£ 5 - π¦πππππ§
The star of the show!
Here we pick the columns we want to see in our final output.
Like an artist choosing colors for their painting π¨
π¦π§ππ£ 6 - π’π₯πππ₯ ππ¬
Now we arrange our results.
This is where we decide the sorting order, like arranging books on a shelf. π
π¦π§ππ£ 7 - πππ ππ§
The final touch!
Here we can limit the number of rows in our result set.
It's like picking the top 10 hits of the year. π
4οΈβ£ πππ‘ππ π₯ππ¦π¨ππ§
And voilΓ ....!
we have our neatly arranged and insightful data, ready for action!
Next week we will see how a JOIN command would affect this flow!
Still no clear how SQL's execution flow works?
Check my previous thread to further understand it! ππ»
And that's all for now
If you liked this thread, I am sharing Data Science and AI content.
So don't forget to follow me to get more content like this! (@rfeers)
RT the tweet below to help me share the word! :D
Share this Scrolly Tale with your friends.
A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.
