Understanding SQL Execution Order: From FROM to LIMIT Explained
This article walks through the exact sequence a database follows when executing a SQL query—starting with FROM and JOIN, then WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY, and finally LIMIT—highlighting key differences from the textual order of the statement.
SQL Execution Order Explained
This article describes the actual order in which a database processes a SQL query, which differs from the textual order of the statement.
Execution steps
1. FROM and JOIN are evaluated first to determine table relationships and produce an initial row set.
2. WHERE applies simple filters to that row set.
3. GROUP BY groups rows according to the specified columns.
4. HAVING filters each group; it can use aggregate functions, unlike WHERE.
5. SELECT computes the final columns, adding any aggregate results; DISTINCT removes duplicate rows.
6. ORDER BY sorts the result set.
7. LIMIT truncates the sorted set to the requested number of rows.
FROM, JOIN and WHERE
These clauses define which tables are involved and how they are related.
from table1 join table2 on table1.id=table2.id from table1,table2 where table1.id=table2.idWithout a join condition the query produces a Cartesian product.
GROUP BY
Groups rows based on the specified columns but does not filter them.
HAVING vs WHERE
HAVING can contain aggregate functions and filters groups after aggregation; WHERE can only use simple conditions before grouping.
Example: having salary < avg(salary) removes rows whose salary is below the group’s average.
SELECT and DISTINCT
After grouping, SELECT retrieves the desired columns; DISTINCT removes duplicate rows.
select employee.id, distinct name, salary, avg(salary)ORDER BY and LIMIT
ORDER BY arranges the final rows; LIMIT stops processing after the required number of rows are retrieved, which is why it must be applied last.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
