0
0
SQLquery~10 mins

Subquery in FROM clause (derived table) in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Subquery in FROM clause (derived table)
Start Query
Identify FROM clause
Detect Subquery
Execute Subquery
Create Derived Table
Use Derived Table in Outer Query
Execute Outer Query
Return Final Result
The database first runs the subquery inside the FROM clause, treats its result as a temporary table, then runs the outer query using this temporary table.
Execution Sample
SQL
SELECT dept, avg_salary
FROM (SELECT department AS dept, AVG(salary) AS avg_salary
      FROM employees
      GROUP BY department) AS dept_avg
WHERE avg_salary > 50000;
This query calculates average salaries per department using a subquery in FROM, then selects departments with average salary over 50000.
Execution Table
StepActionSubquery ResultOuter Query ActionOutput Rows
1Start query executionN/AN/AN/A
2Execute subquery: SELECT department AS dept, AVG(salary) AS avg_salary FROM employees GROUP BY department[{dept: 'HR', avg_salary: 48000}, {dept: 'IT', avg_salary: 60000}, {dept: 'Sales', avg_salary: 52000}]Create derived table dept_avgN/A
3Apply outer query WHERE avg_salary > 50000 on derived tableN/AFilter rows where avg_salary > 50000[{dept: 'IT', avg_salary: 60000}, {dept: 'Sales', avg_salary: 52000}]
4Return final resultN/AOutput filtered rows[{dept: 'IT', avg_salary: 60000}, {dept: 'Sales', avg_salary: 52000}]
💡 Outer query finishes after filtering derived table rows by avg_salary > 50000
Variable Tracker
VariableStartAfter Step 2After Step 3Final
subquery_resultN/A[{dept: 'HR', avg_salary: 48000}, {dept: 'IT', avg_salary: 60000}, {dept: 'Sales', avg_salary: 52000}][{dept: 'HR', avg_salary: 48000}, {dept: 'IT', avg_salary: 60000}, {dept: 'Sales', avg_salary: 52000}]Same as After Step 2
derived_tableN/ACreated from subquery_resultSameSame
filtered_rowsN/AN/A[{dept: 'IT', avg_salary: 60000}, {dept: 'Sales', avg_salary: 52000}]Same
Key Moments - 3 Insights
Why does the subquery run before the outer query?
Because the subquery in the FROM clause creates a temporary table (derived table) that the outer query needs to use. This is shown in execution_table step 2 where the subquery runs first.
Is the derived table stored permanently in the database?
No, the derived table exists only during the query execution as a temporary result from the subquery, as seen in variable_tracker where 'derived_table' is created after step 2 and used immediately.
Can the outer query filter on columns from the subquery?
Yes, the outer query can filter on any columns produced by the subquery, as shown in execution_table step 3 where the WHERE clause filters on avg_salary.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the subquery_result after step 2?
A[{dept: 'HR', avg_salary: 48000}, {dept: 'IT', avg_salary: 60000}, {dept: 'Sales', avg_salary: 52000}]
B[{dept: 'HR', avg_salary: 60000}, {dept: 'IT', avg_salary: 48000}]
CEmpty set
DN/A
💡 Hint
Check the 'Subquery Result' column in execution_table row with Step 2
At which step does the outer query apply the filter avg_salary > 50000?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Outer Query Action' column in execution_table to find when filtering happens
If the subquery returned no rows, what would the final output be?
AAll rows from employees table
BEmpty result set
CError because no rows
DOnly departments with salary > 50000
💡 Hint
Derived table is empty means outer query filters nothing, see variable_tracker for filtered_rows
Concept Snapshot
Subquery in FROM clause creates a temporary table (derived table).
Database runs subquery first, then outer query uses its result.
Derived table can be filtered or joined in outer query.
Useful for complex aggregations or intermediate results.
Syntax: FROM (subquery) AS alias
Always alias the subquery result.
Full Transcript
This visual execution shows how a subquery inside the FROM clause works. First, the database runs the subquery to get a temporary table called a derived table. Then, the outer query uses this derived table to filter or select data. For example, the subquery calculates average salaries per department. The outer query then selects only departments with average salary above 50000. The execution table traces each step: running the subquery, creating the derived table, filtering rows, and returning the final result. Variables track the subquery result, derived table, and filtered rows. Key moments clarify why the subquery runs first, that the derived table is temporary, and that the outer query can filter on subquery columns. The quiz tests understanding of these steps and outcomes. This helps beginners see how subqueries in FROM clauses work step-by-step.