The execution order in SQL is the following: FROM –> WHERE –> GROUP BY –> HAVING –> SELECT –> ORDER BY –> LIMIT
In my query I am using a CASE WHEN statement and an aggregate function in the SELECT statement and then GROUPING it by the CASE WHEN column. But in the GROUP BY clause I dont’ repeat the CASE WHEN construction but just use the column alias "shipping_cost".
SELECT
CASE
WHEN ship_country = 'USA' OR ship_country = 'Canada' THEN 0.0
ELSE 10.0
END AS shipping_cost,
COUNT(*) AS order_count
FROM orders
GROUP BY shipping_cost;
According to what is mentioned above, since the GROUP BY statement is executed before the SELECT statement, how can the query run successfully and not return an error? I am working in PostgresSQL.
Note: I realize this question may have been asked many times but to be honest I haven’t been able to find a satisfying answer to it yet
2
Answers
Use
EXPLAIN ANALYZE VERBOSE
to see what exactly PostgreSQL does with your query.The execution order you described is generally, more or less how it works, but ultimately the last word belongs to the planner. In your case, the planner sees it only makes sense to do the
case
before aggregation and it does exactly that: demoThe order of execution of a
SELECT
statement is the logical order. An SQL implementation is free to do it some other way as long as the result is the same.In this case, PostgreSQL extends the SQL standard in a well documented way:
This is further explained in the section on SQL standard compatibility:
If you don’t like the feature, don’t use it and stay standard compliant.