0% found this document useful (0 votes)
89 views

SQL Order of Execution

Sql order of execution

Uploaded by

Prasad Nani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

SQL Order of Execution

Sql order of execution

Uploaded by

Prasad Nani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DEEP DIVE

INTO
SQL ORDER
OF EXECUTION
SQL ORDER OF EXECUTION

SQL Order of Execution refers to the sequence in which the


components of a SQL query are processed by the database
management system (DBMS) to retrieve the desired results.

Understanding the SQL order of execution is essential for


accurate results, query optimization, efficient resource
utilization, and reducing query complexity.
STAGES OF SQL ORDER OF EXECUTION

Stage 1 :
FROM CLAUSE : Identifies tables and accesses data.
Stage 2:
WHERE CLAUSE:Applies specified conditions to filterdata.
Stage 3:
GROUP BY CLAUSE: Groups data and applies aggregation
functions.
Stage 4:
HAVING CLAUSE: Filters aggregated data based on conditions.
STAGES OF SQL ORDER OF EXECUTION

Stage 5:
SELECT CLAUSE: Defines columns for the result set.
Stage 6:
ORDER BY CLAUSE: Sorts the result set based on
specified columns.
Stage 7:
LIMIT/OFFSET CLAUSE: Restricts the result set to a
specified number of rows with optional offset.
EXAMPLE QUERY
SQL order of execution for this query:

QUERY: 1. Retrieve data from the products table.


2. Apply the filter condition in the WHERE
SELECT product_category, clause to the data.
3. Group the filtered data by the
AVG(price) AS avg_price
product_category column and calculate the
FROM products average price for each group.
WHERE stock_quantity > 0 4. Filter the grouped data using the HAVING
GROUP BY product_category clause condition.
HAVING AVG(price) > 50 5. Select the product_category column and the
ORDER BY avg_price DESC calculated average price for the final result
LIMIT 5; set.
6. Sort the result set based on the calculated
average price in descending order.
7. Limit the result set to a maximum of 5 rows.

You might also like