SQL Order of Execution: WHERE, GROUP BY, HAVING, LIMIT
SQL runs FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY, then LIMIT. Why WHERE cannot see an aggregate and why LIMIT applies after grouping.
In this chapter
WHERE cannot see a bucket. LIMIT runs last.
SQL order of execution: common questions
What is the order of execution of a SQL query?
FROM and JOIN, then WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY, and finally LIMIT or OFFSET.
Why can I not use a SELECT alias in WHERE?
WHERE runs before SELECT, so the alias does not exist yet. Repeat the expression or wrap the query in a subquery or CTE.
More on GROUP BY and HAVING
- SQL GROUP BY Explained Visually, with Examples
- SQL GROUP BY with SUM, COUNT and AVG: Examples
- SQL GROUP BY with WHERE: Filter Rows Before Grouping
- SQL HAVING Clause Explained with Examples
- HAVING with COUNT and SUM: Filter Groups in SQL
- WHERE vs HAVING in SQL: Difference with Examples
- Invalid Use of Group Function: SQL HAVING Errors Fixed
- Column Not in GROUP BY Clause Error (ONLY_FULL_GROUP_BY)
- COUNT(*) vs COUNT(column) in SQL: How NULLs Are Counted
- SQL GROUP BY and HAVING Cheat Sheet
Keep learning
Overview
chapters · group by and having WHERE cannot see a bucket. LIMIT runs last. The clauses run from the rows toward the result. A name you invented in SELECT is not a column yet when WHERE runs. LIMIT then trims whatever rows are left.
1. The order they run
Written order is SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT. Run order is not that list: GROUP BY happens before SELECT, and HAVING after the buckets exist. This is the same strip you saw in the other chapters.
2. Where can you use a name you invented?
COUNT(*) AS n gives the count a name, but only at step 5. Clauses before it cannot see that name in standard SQL. Databases differ in how much they bend this rule, so test each clause and each database. ! Real life check The portable rule is short: use aliases in ORDER BY only, and repeat the full expression everywhere else (HAVING COUNT(*) > 10). A query that leans on a MySQL extension is the first to break the day you move to PostgreSQL or SQL Server.
3. LIMIT cuts the result, after the buckets
One bucket already No GROUP BY means the whole table is one bucket. COUNT(*) is one row, so LIMIT 1 changes nothing. Several buckets, then keep one GROUP BY makes several rows. ORDER BY puts the biggest first, and LIMIT 1 drops the rest. Use the lab above: switch LIMIT 1 on and off and watch the last step.
More courses
- Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
- Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
- React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
- System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
- NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
- Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
- Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
- AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
- Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
- TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
- CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
- Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
- Docker — What an image is, why layers cache, and how containers find each other on a network.
- Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
- Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
- AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
- Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.