---
title: "What is the order of execution process of subquery in SQL?"  
description: "What is the order of execution process of subquery in SQL?"  
author: "Sandra Emily"  
published: 2024-01-18  
updated: 2025-01-07  
canonical: https://www.mindstick.com/forum/160566/what-is-the-order-of-execution-process-of-subquery-in-sql  
category: "mssql server"  
tags: ["mssql server", "sql server", "sql"]  
reading_time: 2 minutes  

---

# What is the order of execution process of subquery in SQL?

What is the [order of execution](https://www.mindstick.com/forum/159236/explain-the-concept-of-multiple-catch-blocks-in-java-and-their-order-of-execution) [process](https://yourviews.mindstick.com/story/1525/7-important-factors-that-may-affect-the-learning-process) of [subquery](https://www.mindstick.com/forum/1279/using-alias-in-subquery) in [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database)?

## Replies

### Reply by Khushi Singh

**[Order](https://www.mindstick.com/articles/12276/how-timely-order-deliveries-can-improve-customer-experience) of [Execution](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net) Process for Subqueries in SQL**

## Non-Correlated Subqueries:

The subquery runs before producing a result set to be utilized by the outer query.\
Example: Subquery located in the `WHERE` or `HAVING` clauses.

```plaintext
SELECT employee_id
FROM employees
WHERE department_id = (SELECT department_id FROM departments WHERE department_name = 'IT');
```

**Correlated Subqueries**:

The outer query executes first, and the subquery runs for each row of the outer query.

Example:

```plaintext
SELECT e1.employee_id
FROM employees e1
WHERE salary > (SELECT AVG(salary) FROM employees e2 WHERE e1.department_id = e2.department_id);
```

**Subqueries in** `FROM` **Clause (Inline Views)**:

The subquery executes as a temporary table, then the outer query processes its result.

```plaintext
SELECT department_id, MAX(avg_salary)
FROM (SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id) sub;
```

**Subqueries in** `SELECT` **Clause**:

The outer query executes, and the subquery runs for each row.

```plaintext
SELECT employee_id, (SELECT department_name FROM departments WHERE department_id = employees.department_id) AS department_name FROM employees;
```

## Summary:

\
**Non-Correlated**: Subquery runs first.

**Correlated**: Outer query begins first and then comes the subquery and it works row wise.

**FROM Clause**: Subquery is treated as a temporary table that is formally known as derived table.

**SELECT Clause**: Subquery occurs for each record of the outer query on the database.

\


---

Original Source: https://www.mindstick.com/forum/160566/what-is-the-order-of-execution-process-of-subquery-in-sql

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
