Databases 6 min read

Unveiling the Real SQL Execution Order: From FROM to LIMIT Explained

This article demystifies the actual execution sequence of SQL queries, detailing how FROM and JOIN establish table relationships, WHERE filters rows, GROUP BY groups data, HAVING applies aggregate conditions, SELECT retrieves fields, ORDER BY sorts results, and LIMIT restricts the final output.

21CTO
21CTO
21CTO
Unveiling the Real SQL Execution Order: From FROM to LIMIT Explained

This is a standard SQL query illustration.

Actual SQL execution order:

First, FROM and JOIN determine table relationships and produce an initial dataset.

Then WHERE applies basic filtering.

Next GROUP BY groups the data.

Each group then evaluates HAVING , which can include ordinary or aggregate function filters.

After that SELECT retrieves the required fields; if aggregate functions are used, a new column is added.

DISTINCT removes duplicate rows.

Finally, the result set is merged and sorted according to ORDER BY .

Data Association Process

FROM & JOIN & WHERE

These clauses define the tables involved and the join conditions. from table1 join table2 on table1.id=table2.id Multiple tables can be listed with a WHERE condition: from table1, table2 where table1.id=table2.id Without a join condition, a Cartesian product occurs.

GROUP BY

GROUP BY partitions data according to specified criteria without filtering.

For example, grouping by the parity of an ID.

HAVING & WHERE

HAVING can contain both ordinary conditions and aggregate functions, while WHERE is limited to non‑aggregate conditions. Often HAVING can replace WHERE for smoother query appearance.

Using WHERE before GROUP BY : filter rows first, then group.

Using GROUP BY before HAVING : group first, then filter groups; the result is essentially the same.

Example: 100/2=50, splitting 100 into ten‑unit pieces: (10+10+10+10+10…)/2=5+5+5+…+5=50. As long as the filter condition remains unchanged, grouping does not affect the outcome; thus WHERE‑then‑GROUP BY and GROUP BY‑then‑HAVING yield identical results.

HAVING differs by supporting aggregate functions, allowing conditions such as having salary<avg(salary) to filter groups based on their average salary.

SELECT

After grouping, SELECT retrieves fields; aggregate functions add a new column. Duplicate column names must be qualified to avoid ambiguity, and DISTINCT can be used to remove duplicate rows.

select employee.id, distinct name, salary, avg(salary)

Data from each group after HAVING is merged.

ORDER BY

ORDER BY sorts the final result set, e.g., by ID. If a LIMIT clause is present, the query stops once the required number of rows is retrieved.

LIMIT

LIMIT is applied after sorting; placing it earlier would return the first N rows regardless of order, leading to incorrect results. For example, to get the three lowest salaries, LIMIT must follow ORDER BY.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performanceSQLdatabaseSQL OptimizationQuery Execution
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.