SELECT query writing order and execution order

 
 

In SQL, the order in which the SQL statements in a SELECT query are written does not necessarily determine the order in which they are executed. The following is a description of the usual SELECT query statement sequence and execution sequence:

  1. Book order

    • ​SELECT​: Specify the columns to be returned.
    • ​FROM​: Specify the table to be queried.
    • ​WHERE​: Conditions for filtering rows.
    • ​GROUP BY​: Group rows.
    • ​HAVING​: Perform conditional filtering on the grouped results.
    • ​ORDER BY​: The resultant order.
  2. Execution order: During the actual execution process, the processing order of SQL queries sometimes does not follow the order of writing, but follows the following logical order:

    • ​FROM​: Retrieve data from the specified table.
    • ​WHERE​: Filter rows using specified criteria.
    • ​GROUP BY​: Group according to the specified column.
    • ​HAVING​: Perform conditional filtering on the grouped results.
    • ​SELECT​: Select the columns to be returned.
    • ​ORDER BY​: Sort the result set.

These sequences can help understand the logical processing order of SQL queries, but do not mean that the DBMS will execute them step by step in this order. In fact, the optimizer may rewrite or restructure the query to improve performance.

Guess you like

Origin blog.csdn.net/hay23455/article/details/134736912