MySQL-SELECT语句执行顺序

总结摘要
MySQL SELECT Execution Order

SELECT语句执行顺序

  1. WITH (CTE, COMMON TABLE EXPRESSIONS)
  2. FROM
  3. ON
  4. JOIN
  5. WHERE
  6. GROUP BY
  7. HAVING
  8. SELECT, WINDOW FUNCTION
  9. SELECT DISTINCT
  10. UNION, INTERSECT, EXCEPT
  11. ORDER BY
  12. LIMIT, OFFSET
  13. FOR UPDATE, FOR NO KEY UPDATE, FOR SHARE, FOR KEY SHARE

Window functions are permitted only in the select list and ORDER BY clause. Query result rows are determined from the FROM clause, after WHERE, GROUP BY, and HAVING processing, and windowing execution occurs before ORDER BY, LIMIT, and SELECT DISTINCT.

窗口函数仅在 select list 和 ORDER BY 子句中允许使用。查询结果行由 FROM 子句确定,在 WHERE、GROUP BY 和 HAVING 处理之后,窗口执行发生在 ORDER BY、LIMIT 和 SELECT DISTINCT 之前。

图示

https://twitter.com/abskoop/status/1709927917743112471

参考

窗口函数 VS SELECT DISTINCT

14.20.2 Window Function Concepts and Syntax

https://dev.mysql.com/doc/refman/8.0/en/window-functions-usage.html

MySQL SQL语句书写顺序和执行顺序

https://www.cnblogs.com/east7/p/14217030.html

distinct、 join on、where、group by、having、order by执行顺序

https://blog.csdn.net/xiaouncle/article/details/79960590

SELECT DISTINCT VS ORDER BY

http://www.postgres.cn/docs/12/sql-select.html ~~ ~~

END