The Execution Order of a SQL Query

The Fearsome side of SQL which we all tend to forget

Sowmiya
3 min readOct 4, 2023

SQL is simple but deeply complex. Myself, despite being at the entry stage of the intermediate level, still find it frustrating while debugging the queries.

But the real anxiety strikes when penning down the SQL query during the interview. More trepidation runs over the vein while elucidating the SQL query to the interviewer, bearing in mind the time frame.

photo on whiplash

Looking back, the board examination pressure or the undergrad (Anna University) semester exam preparation hustle was all-in-all just helping to cope with the panic attacks to face in the career stage.

And I can’t fathom how many times I have just mumbled or rambled the SQL query functions or the execution just to get over with it.

But why this happens? Yeah, we have read about thousands of influencers or learning instructors on LinkedIn saying, ‘Consistency is the key baby’.Umm, duh, but why doesn't anybody focus more on the order of the execution step? Because we are expected to know the flow on the spot, forging to forget the objective of either the performance tuning or cost-time effectiveness.

Let’s take a moment, to understand the execution flow of a SQL Query (from the perspective of a first grader)

Here is the query that is deconstructed:

SELECT
venue,SUM(ticket_price) AS totalPrice,
FROM
concert
WHERE ticket_pruchased =TRUE
GROUP BY venue
HAVING SUM(ticket_price) > 2500
ORDER BY SUM(ticket_price) DESC
LIMIT 50;

First and foremost, the SQL Query executes FROM and WHERE. From the table concert, the rows where ticket_pruchased is TRUE are only filtered.

Secondly, GROUP BY takes up to aggregate by the venue.

Thirdly, we aggregate the venue that has purchased tickets for more than 2500$. HAVING only applies to the aggregated result.

Then we ORDER BY, so we see the tickets purchased by the venue first.

And we LIMIT it to the top 50 cities.

Alas, at the very end, we apply SELECT and aliases. Also, note that we cannot put totalPrice in ORDER BY because ORDER BY happens before AS is executed and the alias is applied.

Whilst my explanation may sound absurd, this is the basic logic applied and the SQL Query execution runs as mentioned above.

Bottomline anybody can be a master SQL Developer with consistency as mentioned by millions of LinkedIn instructors/influencers. But what we fail to address is the anxiety that pulls us down during the interview. And how can this be fixed? Honestly, I don’t know and I’m still learning.

In a world full of recessions and lay-offs era, it’s okay to fail multiple times or not get it right. But, never give up, and never be scared of looking at the people around you.

But one thing I learned is, to believe in myself and focus more on myself in my own way rather than worrying about the people whom I think are stronger than me in technical skills.

I don’t know how in the end, this turned out to be a therapy blog.

Thank you for reading. I’m still in the learning phase.

.

.

.

SQL Learning articles:- (My favorite one)

https://datalemur.com/sql-tutorial

--

--