SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
Subqueries in the FROM
clause, often referred to as derived tables or table expressions, allow you to treat the results of a subquery as if they were a regular table. This can be extremely useful when performing complex data manipulations or when you need to break down your SQL query into logical, intermediate steps.
SELECT ... FROM ( -- Your subquery here SELECT ... FROM ... WHERE ... ) AS alias WHERE ...
The alias
is crucial when using a subquery in the FROM
clause; you must always give the subquery an alias name, and then you can reference the columns of the subquery using that alias.
Basic example:
Consider a scenario where you have a table employees
and you want to get the average salary of employees grouped by their department. Then, from that result, you want to filter only those departments where the average salary is above a certain value.
SELECT dept, avg_salary FROM ( SELECT department AS dept, AVG(salary) AS avg_salary FROM employees GROUP BY department ) AS derived_table WHERE avg_salary > 50000;
Joining with another table:
Suppose you have a table of orders
and a table of products
. You want to find the total quantity of each product ordered and then join this with the products
table to get the product name.
SELECT p.product_name, d.total_quantity FROM products p JOIN ( SELECT product_id, SUM(quantity) AS total_quantity FROM orders GROUP BY product_id ) AS d ON p.id = d.product_id;
Using multiple derived tables:
Let's say you have two tables: sales_2022
and sales_2023
. You want to get the total sales for each year and then compare them.
SELECT s2022.region, s2022.total_sales AS sales_2022, s2023.total_sales AS sales_2023 FROM ( SELECT region, SUM(sales) AS total_sales FROM sales_2022 GROUP BY region ) AS s2022 JOIN ( SELECT region, SUM(sales) AS total_sales FROM sales_2023 GROUP BY region ) AS s2023 ON s2022.region = s2023.region;
FROM
clause:However, always ensure to test the performance implications of using derived tables, especially in large datasets or complex operations, as there might be more efficient ways to achieve the same results.
Using subqueries in FROM clause in SQL:
SELECT * FROM (SELECT column1 FROM example_table WHERE condition) AS subquery_result;
SQL subquery in FROM clause example:
SELECT * FROM (SELECT column1 FROM example_table WHERE column2 = 'Value') AS subquery_result;
How to join tables using subqueries in FROM:
SELECT * FROM example_table JOIN (SELECT columnA, columnB FROM another_table WHERE condition) AS subquery_result ON example_table.column1 = subquery_result.columnA;
Subquery as a derived table in SQL FROM clause:
SELECT * FROM (SELECT column1, column2 FROM example_table WHERE condition) AS derived_table;
Selecting data from subquery in FROM in SQL:
SELECT column1, (SELECT AVG(column2) FROM another_table WHERE condition) AS average_value FROM example_table;
Correlated subqueries in the FROM clause:
SELECT * FROM example_table e JOIN (SELECT AVG(column2) AS avg_value FROM another_table a WHERE a.id = e.id) AS subquery_result ON e.column1 > subquery_result.avg_value;
Benefits and drawbacks of subqueries in FROM:
-- Benefit: Modularity SELECT * FROM (SELECT column1 FROM example_table WHERE condition) AS subquery_result; -- Drawback: Performance SELECT * FROM example_table WHERE column1 IN (SELECT column2 FROM another_table WHERE condition);
Joining multiple tables with subqueries in FROM:
SELECT * FROM example_table e JOIN (SELECT columnA, columnB FROM another_table WHERE condition) AS subquery_result1 ON e.column1 = subquery_result1.columnA JOIN (SELECT columnX, columnY FROM yet_another_table WHERE condition) AS subquery_result2 ON e.column2 = subquery_result2.columnX;
Advanced examples of subqueries in FROM clause:
SELECT * FROM (SELECT column1, column2 FROM example_table WHERE condition1) AS subquery_result1 JOIN (SELECT columnA, columnB FROM another_table WHERE condition2) AS subquery_result2 ON subquery_result1.column1 = subquery_result2.columnA;