Database Systems
Committing your transactions…
Initializing Sorting Algorithms...
Sorting
Trees
Graphs
Preparing interactive visualizations...
Database Systems
Committing your transactions…
SQL is written SELECT-first but evaluated FROM-first. Follow the rows through every clause in logical order and the rules about aliases and aggregates stop being arbitrary.
Click Play to begin.
| Clause | Aggregates | Aliases |
|---|---|---|
| WHERE | no | no |
| GROUP BY | no | no* |
| HAVING | yes | no* |
| SELECT | yes | defines them |
| ORDER BY | yes | yes |
* MySQL and PostgreSQL accept SELECT aliases in GROUP BY (and MySQL in HAVING) as an extension; standard SQL does not. The optimizer may reorder the physical work — pushing filters down, for instance — but never the logical result.
Question 1 of 3
Why does WHERE COUNT(*) > 2 fail, while HAVING COUNT(*) > 2 works?