![]() |
VOOZH | about |
SQL Subqueries are valuable features that enable us to embed one query within another, adding more importance to retrieving and manipulating data in complicated manner. It is used to set up temporary tables or intermediate results to be subsequently used by the outer query.
FROM clause can be used to specify a subquery expression in SQL. The relation produced by the subquery is then used as a new relation on which the outer query is applied for further operations. Subqueries in the FROM clause is used when we need to create a temporary dataset that does not exist in any physical table. This can be extremely helpful when working with complex datasets.
The correlation variables from the tables referenced in the outer query's FROM clause cannot be used directly within the subqueries in the FROM clause. Given below are two syntax of using Subquery in the FROM clause used in different scenarios.
FROM Clause:SELECT column1, column2
FROM (SELECT column_x AS C1, column_y FROM table WHERE condition) AS subquery_table
WHERE outer_condition;
FROM Clause with JOIN:SELECT column1, column2
FROM (SELECT column_x AS C1, column_y FROM table WHERE PREDICATE_X) AS table2
JOIN table1 ON table2.some_column = table1.some_column
WHERE PREDICATE;
subquery_table) to be referenced in the outer query.Lets discuss some real world examples to understand how Subqueries in the FROM Clause work in practice. Here we have two tables Instructor table that contains information about instructors, including their salary and department. Other one is Department table that contains information about different departments, including the department's budget.
| InstructorID | Name | Department | Salary |
|---|---|---|---|
| 44547 | Smith | Computer Science | 95000 |
| 44541 | Bill | Electrical | 55000 |
| 47778 | Sam | Humanities | 44000 |
| 48147 | Erik | Mechanical | 80000 |
| 411547 | Melisa | Information Technology | 65000 |
| 48898 | Jena | Civil | 50000 |
| Department Name | Budget |
|---|---|
| Computer Science | 100000 |
| Electrical | 80000 |
| Humanities | 50000 |
| Mechanical | 40000 |
| Information Technology | 90000 |
| Civil | 60000 |
In this example we will use a subquery in the FROM clause to calculate the average budget of all departments and then do the comparison with the salaries of instructors.
Query:
SELECT I.InstructorID, I.Name, I.Department, I.Salary
FROM (SELECT AVG(Budget) AS averageBudget FROM Department) AS BUDGET,
Instructor AS I
WHERE I.Salary > BUDGET.averageBudget;
Output
| InstructorID | Name | Department | Salary |
|---|---|---|---|
| 44547 | Smith | Computer Science | 95000 |
| 48147 | Erik | Mechanical | 80000 |
Explanation:
There are various advantages of using Subqueries in the FROM clause as given below:
Subqueries in the FROM clause enables us to define temporary result sets that may be utilized by outer queries. They make complex SQL operations easier, enhance query readability, and provide flexibility in data filtering and aggregation. Knowing how to utilize subqueries within the FROM clause is a basic skill for every SQL practitioner, particularly when dealing with complex databases or when you have to carry out complex data analysis.