Reader small image

You're reading from  Learn SQL Database Programming

Product typeBook
Published inMay 2020
Reading LevelIntermediate
PublisherPackt
ISBN-139781838984762
Edition1st Edition
Languages
Right arrow
Author (1)
Josephine Bush
Josephine Bush
author image
Josephine Bush

Josephine Bush has over 10 years experience as a Database Administrator. Her experience is extensive and broad-based, including in financial, business, and energy data systems using MySQL, SQL Server, Oracle, and PostgreSQL. She is a Microsoft Certified Solutions Expert: Data Management and Analytics. She holds a BS in Information Technology, an MBA in IT Management, and an MS in Data Analytics.
Read more about Josephine Bush

Right arrow

Understanding SQL query order of execution

It's important to understand that there is a specific order you write SQL query clauses in, as well as a specific order that they are actually executed in by MySQL engine.

You write SQL query clauses in this order:

  • SELECT
  • FROM/JOIN
  • WHERE
  • GROUP BY
  • HAVING
  • ORDER BY
  • LIMIT

The order that the clauses are run in by the MySQL engine is as follows:

  • FROM/JOIN
  • WHERE
  • GROUP BY
  • HAVING
  • SELECT
  • ORDER BY
  • LIMIT

This is why you can only use column aliases from the SELECT clause in the ORDER BY clause. Understanding the order that MySQL actually runs your clauses in can help you to better understand how your query works or doesn't work.

There is a setting in MySQL that overrides this default behavior for the GROUP BY and HAVING clauses, which are enabled by default, so MySQL will allow you to use column aliases in them. The other RDBMSes...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Learn SQL Database Programming
Published in: May 2020Publisher: PacktISBN-13: 9781838984762

Author (1)

author image
Josephine Bush

Josephine Bush has over 10 years experience as a Database Administrator. Her experience is extensive and broad-based, including in financial, business, and energy data systems using MySQL, SQL Server, Oracle, and PostgreSQL. She is a Microsoft Certified Solutions Expert: Data Management and Analytics. She holds a BS in Information Technology, an MBA in IT Management, and an MS in Data Analytics.
Read more about Josephine Bush