MySQL

Chapter 7 - DQL (Data Query Language)

SQL Execution Order

In MySQL, the SQL execution order defines the sequence in which the various clauses of a query are processed by the database engine. Understanding this execution order is essential for writing efficient queries and ensuring accurate results. The logical order of execution may differ from the written order of clauses in an SQL query. Below is the logical execution order of an SQL query along with explanations and examples.

SQL Execution Order

  1. FROM:

    • The FROM clause is executed first. It specifies the tables from which the data will be retrieved.
    • Example:
    SELECT first_name, last_name FROM employees;
    • This query selects the first_name and last_name from the employees table.
  2. JOIN:

    • If the query includes a JOIN, it is processed next. The database joins the specified tables based on the given condition.
    • Example:
    SELECT e.first_name, e.last_name, d.department_name FROM employees e JOIN departments d ON e.department_id = d.department_id;
    • This query retrieves employee names and their department names by joining the employees and departments tables.
  3. WHERE:

    • The WHERE clause filters rows from the tables before any groupings are applied.
    • Example:
    SELECT first_name, last_name FROM employees WHERE salary > 50000;
    • This query retrieves employees who earn more than 50,000.
  4. GROUP BY:

    • The GROUP BY clause groups rows that share the same values in specified columns, typically used with aggregate functions like COUNT(), SUM(), etc.
    • Example:
    SELECT department_id, COUNT(*) AS employee_count FROM employees GROUP BY department_id;
    • This query groups employees by department and returns the count of employees in each department.
  5. HAVING:

    • The HAVING clause is used to filter groups created by the GROUP BY clause. It works similarly to the WHERE clause but applies to aggregated data.
    • Example:
    SELECT department_id, COUNT(*) AS employee_count FROM employees GROUP BY department_id HAVING employee_count > 5;
    • This query returns departments with more than five employees.
  6. SELECT:

    • The SELECT clause specifies which columns or expressions to retrieve. At this stage, any computed columns or aliases are evaluated.
    • Example:
    SELECT first_name, last_name, salary * 12 AS annual_salary FROM employees;
    • This query calculates the annual salary for each employee.
  7. DISTINCT:

    • The DISTINCT keyword is applied to remove duplicate rows from the result set.
    • Example:
    SELECT DISTINCT department_id FROM employees;
    • This query retrieves unique department IDs from the employees table.
  8. ORDER BY:

    • The ORDER BY clause sorts the final result set in either ascending (ASC) or descending (DESC) order.
    • Example:
    SELECT first_name, last_name, salary FROM employees ORDER BY salary DESC;
    • This query retrieves employees and sorts them by salary in descending order.
  9. LIMIT:

    • The LIMIT clause is processed last. It restricts the number of rows returned by the query.
    • Example:
    SELECT first_name, last_name, salary FROM employees ORDER BY salary DESC LIMIT 5;
    • This query retrieves the top 5 highest-paid employees.

Full Example with Execution Order:

SELECT department_id, COUNT(*) AS employee_count FROM employees JOIN departments ON employees.department_id = departments.department_id WHERE salary > 30000 GROUP BY department_id HAVING employee_count > 10 ORDER BY employee_count DESC LIMIT 3;

Execution Order:

  1. FROM: Fetch the employees table.
  2. JOIN: Join employees with departments on department_id.
  3. WHERE: Filter rows where salary is greater than 30,000.
  4. GROUP BY: Group by department_id.
  5. HAVING: Filter groups having more than 10 employees.
  6. SELECT: Select department_id and COUNT(*).
  7. ORDER BY: Sort by employee_count in descending order.
  8. LIMIT: Limit the results to 3 rows.

Understanding the logical order of execution helps write efficient and accurate queries, making it easier to work with complex SQL statements.

Tansy SQL Course | SQL Execution Order | Chapter 7 | Lesson 38 - Video Thumbnail

SQL Query Order of Execution

Below is an illustration depicting the internal processing sequence of different clauses in a SQL statement by the SQL engine. It's important to note that in this example, the DISTINCT and LIMIT clauses are not utilized.

SQL Execution Order

i

As illustrated in this image, the SQL engine initially executes the FROM clause and any INNER JOINs, followed by the other steps as depicted.

Comments(0 comments)

Comments Not Found