SELECT statement order

Updated at:

For a statement written in SELECT syntax, the logical execution order of the SELECT statement differs from its written order. This topic describes the execution order of SELECT clauses and provides examples.

Execution sequences

The SELECT syntax includes the following main clauses: SELECT, FROM, WHERE, GROUP BY, HAVING, WINDOW, QUALIFY, ORDER BY, DISTRIBUTE BY, SORT BY, and LIMIT.

ORDER BY and GROUP BY cannot be used together with DISTRIBUTE BY or SORT BY. The logical execution sequences of common SELECT statements are as follows:

  • Scenario 1: Grouping and global sorting.

    FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT
  • Scenario 2: Data distribution and sorting.

    FROM → WHERE → SELECT → DISTRIBUTE BY → SORT BY

To avoid confusion between the written order and execution order of clauses, MaxCompute supports writing queries in execution order. The syntax is as follows:

FROM <table_reference>
[WHERE <where_condition>]
[GROUP BY <col_list>]
[HAVING <having_condition>]
[WINDOW <window_name> AS (<window_definition>)]
[QUALIFY <expression>]
SELECT [ALL | DISTINCT] <select_expr>, <select_expr>, ...
[ORDER BY <order_condition>]
[DISTRIBUTE BY <distribute_condition> [SORT BY <sort_condition>]]
[LIMIT <number>]

Sample data

The examples in this topic use the following sample data. Run the following statements to create and populate the sale_detail table:

-- Create a partitioned table named sale_detail.
CREATE TABLE IF NOT EXISTS sale_detail (
    shop_name     STRING,
    customer_id   STRING,
    total_price   DOUBLE
)
PARTITIONED BY (
    sale_date STRING,
    region    STRING
);

-- Add partitions to the sale_detail table.
ALTER TABLE sale_detail ADD
    PARTITION (sale_date='2013', region='china')
    PARTITION (sale_date='2014', region='shanghai');

-- Append data to the specified partitions of the sale_detail table.
INSERT INTO sale_detail PARTITION (sale_date='2013', region='china')
VALUES
    ('s1', 'c1', 100.1),
    ('s2', 'c2', 100.2),
    ('s3', 'c3', 100.3);

INSERT INTO sale_detail PARTITION (sale_date='2014', region='shanghai')
VALUES
    ('null', 'c5', NULL),
    ('s6', 'c6', 100.4),
    ('s7', 'c7', 100.5);

Run the following statements to query the data in the partitioned sale_detail table:

SET odps.sql.allow.fullscan=true;
SELECT * FROM sale_detail; 

-- Result:
+-----------+-------------+-------------+-----------+--------+
| shop_name | customer_id | total_price | sale_date | region |
+-----------+-------------+-------------+-----------+--------+
| s1        | c1          | 100.1       | 2013      | china  |
| s2        | c2          | 100.2       | 2013      | china  |
| s3        | c3          | 100.3       | 2013      | china  |
| null      | c5          | NULL        | 2014      | shanghai |
| s6        | c6          | 100.4       | 2014      | shanghai |
| s7        | c7          | 100.5       | 2014      | shanghai |
+-----------+-------------+-------------+-----------+--------+

Examples

  • Example 1: The following statements use grouping and global sorting:

    Note

    Before running the following queries on the partitioned table, add SET odps.sql.allow.fullscan=true; to enable a full table scan, or specify partitions in the queries.

    -- Standard SELECT syntax.
    SET odps.sql.allow.fullscan=true;
    
    SELECT region, MAX(total_price)
    FROM sale_detail
    WHERE total_price > 100
    GROUP BY region
    HAVING SUM(total_price) > 300.5
    ORDER BY region
    LIMIT 5;
    
    -- Execution-order form, equivalent to the query above.
    FROM sale_detail
    WHERE total_price > 100
    GROUP BY region
    HAVING SUM(total_price) > 300.5
    SELECT region, MAX(total_price)
    ORDER BY region
    LIMIT 5;

    Result:

    +--------+------------+
    | region | _c1        |
    +--------+------------+
    | china  | 100.3      |
    +--------+------------+

    How the clauses execute:

    1. Read rows from sale_detail (FROM sale_detail) that satisfy WHERE total_price > 100.

    2. Group the filtered rows by region (GROUP BY region).

    3. Keep only groups whose total_price sum exceeds 300.5 (HAVING SUM(total_price) > 300.5).

    4. From the retained groups, select the region and maximum total_price for each group (SELECT region, MAX(total_price)).

    5. Sort the query results by region (ORDER BY region).

    6. Return only the first five sorted rows (LIMIT 5).

  • Example 2: The following statements use data distribution and sorting:

    -- Standard SELECT syntax.
    SET odps.sql.allow.fullscan=true;
    
    SELECT
        shop_name,
        total_price,
        region
    FROM sale_detail
    WHERE total_price > 100.2
    DISTRIBUTE BY region
    SORT BY total_price;
    
    -- Execution-order form, equivalent to the query above.
    FROM sale_detail
    WHERE total_price > 100.2
    SELECT
        shop_name,
        total_price,
        region
    DISTRIBUTE BY region
    SORT BY total_price;

    Result:

    +-----------+-------------+--------+
    | shop_name | total_price | region |
    +-----------+-------------+--------+
    | s3        | 100.3       | china  |
    | s6        | 100.4       | shanghai |
    | s7        | 100.5       | shanghai |
    +-----------+-------------+--------+

    How the clauses execute:

    1. Read rows from sale_detail (FROM sale_detail) that satisfy WHERE total_price > 100.2.

    2. Select the shop_name, total_price, and region columns from the filtered rows (SELECT shop_name, total_price, region).

    3. Hash-partition the selected rows by region (DISTRIBUTE BY region).

    4. Within each partition, sort by total_price in ascending order (SORT BY total_price).