What Does the Query Processor See Before You Click Execute?

2025-10-14 · 3 min

When you type a SELECT and press Execute, something remarkable begins — but not in the order you might expect.

SQL Server is not a naive interpreter of your code. It is a query-logic compiler that sees the world differently. Before the query can execute, the T-SQL text is parsed, rewritten, normalized, and optimized. The result is not simply “running the code,” but a query plan: a precise set of instructions for the relational engine to retrieve data as efficiently as possible.

Here is the first paradox: SQL Server does not logically start with SELECT. It starts with FROM, identifying data sources and joins, then follows the logical processing phases:

  1. FROM + JOIN — determine where the data comes from and how it is joined.
  2. WHERE — filter rows.
  3. GROUP BY and aggregate functions — produce summaries.
  4. HAVING — filter groups.
  5. SELECT — choose output columns and expressions.
  6. ORDER BY — sort the result.

It is no coincidence that performance often depends on where filtering happens rather than how elegantly the query is written.

“The illusion that code runs as written is one of the oldest tricks in computing.”

That illusion also applies to SQL. The syntax looks sequential, but the query processor reasons in relational algebra. This is why rewriting a predicate or join can dramatically change an execution plan. Understanding this distinction is the first step toward writing queries that are efficient, not merely correct.


How to check this in practice

Try it yourself. Run the following query in SSMS or Azure Data Studio:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
USE AdventureWorks2022;
SET SHOWPLAN_XML ON;

SELECT p.Name, SUM(sod.LineTotal) AS Total
FROM Sales.SalesOrderDetail sod
JOIN Production.Product p ON sod.ProductID = p.ProductID
WHERE p.Color = 'Red'
GROUP BY p.Name
HAVING SUM(sod.LineTotal) > 10000
ORDER BY Total DESC;

SQL Server will not execute the query; instead, it generates an XML execution plan. Open it and notice that the physical data access involves a SCAN or SEEK, such as a Clustered Index Scan or Index Seek. Other operators may include Nested Loops, Hash Match, and Filter. The execution tree is not a literal line-by-line rendering of your SQL text.

Disable the showplan option with:

1
SET SHOWPLAN_XML OFF;

Want a graphical view? Enable Ctrl+M before running a query. The SSMS plan shows SELECT at the root of the operator tree, while the data access operators appear at the leaves.


See? It is not magic — it is relational algebra in action. Each SELECT becomes a composition of operators arranged by the Query Processor. Understanding that difference is when you stop merely writing queries and start designing them.


Tags: SQLServer , TSQL , DBA , SQLQuiz , DataPlatform

Marcin Pytlik
LinkedIn · GitHub

Marcin Pytlik

LinkedIn · GitHub