thinQmaster Virtual testing partner Adaptive practice exams that find your weak spots and drill them. Every answer explained. From $69
Enroll

thinQmaster

Find your exam

Five questions free, before you decide.

Step 1 · choose a vendor

In one of our camps? Your practice exams come with the tuition — they are on your study page already.

Data & Analytics · Intermediate

SQL Logical Query Processing Order

The order in which SQL logically evaluates clauses: FROM/JOIN, WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY, then LIMIT/TOP.

Definition

Because SELECT is evaluated after WHERE, column aliases defined in SELECT cannot be used in WHERE but can be used in ORDER BY. Window functions are computed during the SELECT phase, which is why they cannot appear in WHERE; QUALIFY or a subquery is needed instead.

Example

WHERE TotalPrice > 100 fails if TotalPrice is only an alias defined in SELECT.

Exam tip

Explaining 'why can't I use this alias in WHERE?' or 'why can't I filter on ROW_NUMBER in WHERE?' requires knowing this order.

Related terms