The Order of SQL Query Execution

The Order of SQL Query Execution

FROM and JOINs

  • The query starts by locating the data source(s) specified in the FROM clause.
  • If JOIN operations are included, tables are merged based on the conditions provided, resulting in a preliminary dataset.

WHERE

  • The WHERE clause is then applied to filter this preliminary dataset, keeping only the rows that meet the conditions specified.

GROUP BY

  • If a GROUP BY clause is present, it groups the filtered rows by the specified columns and aggregates data within each group.

HAVING

  • After grouping, the HAVING clause filters the groups based on the conditions provided, similar to the WHERE clause but for groups.

SELECT

  • The SELECT clause is used to specify the columns and expressions to retrieve from the dataset, and to define any column aliases.

DISTINCT

  • If DISTINCT is specified, duplicate rows are removed from the final result set.

ORDER BY

  • The ORDER BY clause sorts the result set according to the specified columns and order (either ascending or descending).

LIMIT/OFFSET

  • Finally, the LIMIT clause restricts the number of rows returned, while the OFFSET clause skips a specified number of rows before returning the rest

Reference:

[1]. Mayank Ahuja (May 22, 2024) The actual Order of Execution [link ]


要查看或添加评论,请登录

社区洞察

其他会员也浏览了