Error: Column does not exist.
I bet you’ve seen this error a few times after running your SQL query. That’s okay. I have, too.
When you’re learning SQL, it’s natural for us to think that the way we write our query is the same way the database processes it. I mean, we typically write from left to right, top to bottom, and we read in the same order. So why do we get a ‘column does not exist‘ error?
It’s simple: SQL is not like a human.
SQL runs in an order that has nothing to do with how you read the query top to bottom. So when you try to use an alias “too early,” SQL genuinely has no idea what you’re talking about. According to the system, the alias doesn’t exist yet.
In order to fix the issue, you need to refer to the actual column name in the WHERE clause. Of course, if you have a really long column name and want to use the alias, you can split your query into two using CTEs (or subqueries, if you must, but I highly recommend against them).
The Written Order vs the Execution Order
Let’s start with how most people think SQL executes.
You start with a simple query that looks something like this:
SELECT department,
COUNT(*) AS employee_count
FROM employees
WHERE salary > 80000
GROUP BY department
HAVING COUNT(*) > 5
ORDER BY employee_count DESC;When you read this, your brain naturally processes it line by line:
- Select these columns
- From this table
- Filter the rows
- Group them
- Filter the groups
- Sort the result
Seems logical, right?
But SQL doesn’t execute the query in that same order. Here is the simplified order of what gets read in the back-end:
FROM
WHERE
GROUP BY
HAVING
SELECT
ORDER BYNotice that the SELECT statement doesn’t even get read till the end. That explains why you can’t alias something in SELECT and expect it to work in WHERE or FROM. This part accounts for a lot of the errors that occur when referencing aliases too soon.
Now let’s walk through the example query using the order SQL actually follows.
Step by Step Execution Explained

FROM
SQL starts by reading all rows from the employees table. At this point, nothing has been filtered, grouped, or counted. It is just raw data.
This is an important first step, because everything else depends on what passes through the next steps.

WHERE
Next, SQL filters rows where salary is greater than 80000.
This happens before any aggregation. SQL is still working at the row level. No counts exist yet. No groups exist yet.
If a row does not pass the WHERE condition, it is gone permanently.
Tired of googling the same SQL problems every week?
Learn and practice SQL here:
GROUP BY
Now SQL groups the remaining rows by department.
This is the first time aggregation actually begins. Counts, sums, averages, and other aggregates are calculated here.
Anything that was filtered out earlier never makes it into these groups.
HAVING
After grouping, SQL applies the HAVING clause.
This is where groups are filtered. Departments with five or fewer employees are removed.
This step exists specifically because WHERE cannot filter aggregated results. At the time WHERE runs, employee counts (i.e. the grouping to get the counts) do not exist yet.
SELECT
Only now does SQL decide what columns to return.
This is where aliases like employee_count are created. Before this step, that name does not exist anywhere.
This is why you cannot reference a SELECT alias in WHERE or GROUP BY. SQL has not created it yet.
ORDER BY
Finally, SQL sorts the results.
ORDER BY runs last, which is why it can reference SELECT aliases. At this point, the final shape of the data already exists.
The Importance of Understanding Execution Order
Understanding the differences between written SQL and SQL execution is important for many reasons:
- You cannot use a SELECT alias in WHERE because SELECT has not run yet.
- The HAVING function exists because WHERE cannot see aggregated values yet.
- ORDER BY can use aliases because it runs after SELECT.
It also explains why adding what feels like a harmless filter can completely change your numbers.
You might think you are filtering final results when you are actually filtering raw rows before aggregation. The query still runs. The logic still looks fine. The output just changes.
That is usually how mistakes sneak into dashboards.
A Common Mistake to Watch Out For
This shows up all the time in reporting and dashboard builds.
Someone builds a query that looks and seems to work perfectly fine. But then someone else decides to add a filter to the query, and now the totals no longer match last month.
Nothing looks obviously wrong. The SQL runs without errors. The numbers just don’t make sense.
In many cases, the filter was added to WHERE when it really belonged after aggregation (i.e., in the HAVING function).
Understanding execution order makes it easier to spot these issues before they go live.
Tired of googling the same SQL problems every week?
Learn and practice SQL here:
Don’t Focus on Memorization
A lot of beginners try to learn SQL by memorizing restrictions:
You cannot do this here.
You must use HAVING there.
Aliases sometimes work and sometimes do not.
Memorizing every nuance doesn’t work, and it will just make learning that much harder. Instead, practice writing SQL and test things out. Try referring to an alias at different stages, and see what happens. Try filtering in two different ways and see how the numbers change. Just start using SQL in small ways, and you’ll understand execution order more easily than if you tried to memorize it.
Once you do, SQL becomes very predictable. You’ll know what to write and why. And if you do get an error, you’ll be able to pinpoint it right away.
If something does not work, it is usually because you are asking SQL to use information that does not exist yet in the execution process.
Recommended Posts:
How This Helps You Write Better SQL
When your results look wrong, don’t freak out. And do not immediately rewrite the query.
Pause and ask yourself a simpler question:
At what stage does this logic belong?
Am I filtering rows, groups, or final results?
Once you start thinking in execution phases instead of written order, debugging becomes much faster and far less frustrating.
One Last Reminder
Don’t beat yourself up if you make a mistake. That’s how you learn. I still make mistakes, and I’ve been using SQL for years. The thing to remember is that every little mistake you make, you grow a little bit more.
It sounds corny, I know.
But it’s true.
You won’t be a pro when you first start learning SQL. But take it step by step and try out new functions or concepts as you come across things. Get your hands dirty!
Once you do, SQL will start to feel easier, and you might actually start liking it.
Frequently Asked Questions
What is the SQL order of execution?
The SQL order of execution is the sequence SQL follows to process a query. It is not the same as the written order. SQL typically processes FROM first, then WHERE, GROUP BY, HAVING, SELECT, and finally ORDER BY.
Why does SQL not execute in the order it is written?
SQL is a declarative language. You describe what result you want, not how to compute it. The database engine decides the execution steps based on logical processing rules, not the order of the clauses in your query.
Why can’t I use a SELECT alias in the WHERE clause?
Because WHERE runs before SELECT. At the time SQL evaluates the WHERE clause, the SELECT aliases do not exist yet. That is why aliases only work in ORDER BY, which runs after SELECT.
Why does HAVING exist if WHERE already filters data?
WHERE filters individual rows before aggregation. HAVING filters grouped results after aggregation. Since aggregates do not exist during the WHERE phase, HAVING is required for conditions on counts, sums, and averages.
What is the difference between SQL execution order and written order?
The written order is how the query is structured for readability. The execution order is how SQL logically processes the query. Understanding the execution order explains many SQL rules that feel inconsistent when you are first learning.
Is SQL execution order the same across databases?
Yes, the logical execution order is consistent across major SQL databases like SQL Server, PostgreSQL, MySQL, and Oracle. Query optimizers may change physical execution plans, but the logical order remains the same.
More about different databases/dialects here:
Enjoyed this post?
I write about data, career transitions, and making analytics easier to understand.
Subscribe to my newsletter to get the latest posts.
If you want more content, I post regularly on LinkedIn, so connect and say hi!








Such a nice and interesting article here! (I can clearly relate as I’ve been doing SQL since 2010 :D)
Thank you! Order of execution isn’t always explained clearly enough. 🙂