Databases 7 min read

What Really Happens First? Unveiling the True Execution Order of SQL Queries

This article explains the actual logical execution order of SQL statements, clarifies common misconceptions about WHERE, GROUP BY, and SELECT, shows how column aliases affect processing, discusses optimizer reordering, and compares the order with LINQ and pandas query patterns.

Liangxu Linux
Liangxu Linux
Liangxu Linux
What Really Happens First? Unveiling the True Execution Order of SQL Queries

SQL Query Execution Order

The logical processing steps of a SQL query differ from the textual order; SELECT is not executed first but is the fifth step after FROM, WHERE, GROUP BY, HAVING, etc. The accompanying diagram illustrates the sequence.

Questions Answered by the Diagram

Can WHERE appear after GROUP BY? (No, WHERE is evaluated before GROUP BY.)

Can the result of a window function be filtered? (No, window functions are evaluated in the SELECT phase, which comes after WHERE and GROUP BY.)

Can ORDER BY use columns from GROUP BY? (Yes, ORDER BY is applied last and may reference any prior expression.)

When is LIMIT applied? (At the very end of processing.)

Database engines may deviate from this logical order for performance, applying optimizations that do not change the final result.

Mixed Factor: Column Aliases

Some SQL dialects allow using column aliases in GROUP BY. The query below appears to reference the alias, but the engine can rewrite it to use the original expression.

SELECT CONCAT(first_name, ' ', last_name) AS full_name, count(*)
FROM table
GROUP BY full_name

The engine may transform it to:

SELECT CONCAT(first_name, ' ', last_name) AS full_name, count(*)
FROM table
GROUP BY CONCAT(first_name, ' ', last_name)

Thus GROUP BY still executes before SELECT, and the engine performs a validation pass before generating the execution plan.

Engine May Reorder for Optimization

In practice, the optimizer can reorder operations such as pushing predicates before a join to reduce work.

SELECT * FROM owners
LEFT JOIN cats ON owners.id = cats.owner
WHERE cats.name = 'mr darcy'

If the goal is only to find cats named “mr darcy”, filtering before the join is more efficient and does not alter the result.

LINQ Queries Start with FROM

In C# and VB.NET, LINQ query syntax follows the logical order FROM → WHERE → SELECT. Example:

var teenAgerStudent = from s in studentList
    where s.Age > 12 && s.Age < 20
    select s;

Pandas Query Pattern (Similar Logical Flow)

Although pandas does not enforce a strict order, writing code in the logical sequence improves readability and often matches engine optimizations.

df = thing1.join(thing2)               # JOIN
df = df[df.created_at > 1000]          # WHERE
df = df.groupby('something', num_yes=('yes','sum'))  # GROUP BY
df = df[df.num_yes > 2]                # HAVING
df = df[['num_yes','something1','something']]       # SELECT
df.sort_values('sometthing', ascending=True)[:30] # ORDER BY & LIMIT
df[:30]                                 # LIMIT

Writing queries in this order clarifies intent and often aligns with how database engines internally reorder operations for performance.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

optimizationSQLQuery ExecutionpandasWindow FunctionsLINQ
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.