SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

SQL | Sub queries in From Clause

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.

Syntax:

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.

Examples:

  1. 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;
    
  2. 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;
    
  3. 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;
    

Advantages of using subqueries in the FROM clause:

  1. Modularity: You can break down complex queries into smaller, more manageable chunks.
  2. Readability: Complex operations can be encapsulated in the subquery, making the main query simpler and more readable.
  3. Reuse: If you need to perform the same operation multiple times in different parts of a query, encapsulating that operation in a derived table can be efficient.

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.

  1. Using subqueries in FROM clause in SQL:

    • Subqueries in the FROM clause are often used to create temporary result sets that can be utilized in the main query.
    SELECT *
    FROM (SELECT column1 FROM example_table WHERE condition) AS subquery_result;
    
  2. SQL subquery in FROM clause example:

    • A simple example of using a subquery in the FROM clause to filter data.
    SELECT *
    FROM (SELECT column1 FROM example_table WHERE column2 = 'Value') AS subquery_result;
    
  3. How to join tables using subqueries in FROM:

    • Subqueries can be used to join tables by embedding them in the FROM clause.
    SELECT *
    FROM example_table
    JOIN (SELECT columnA, columnB FROM another_table WHERE condition) AS subquery_result
    ON example_table.column1 = subquery_result.columnA;
    
  4. Subquery as a derived table in SQL FROM clause:

    • Subqueries in the FROM clause are often referred to as derived tables.
    SELECT *
    FROM (SELECT column1, column2 FROM example_table WHERE condition) AS derived_table;
    
  5. Selecting data from subquery in FROM in SQL:

    • The main query can select data from the subquery in the FROM clause.
    SELECT column1, (SELECT AVG(column2) FROM another_table WHERE condition) AS average_value
    FROM example_table;
    
  6. Correlated subqueries in the FROM clause:

    • Correlated subqueries in the FROM clause reference columns from the outer query.
    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;
    
  7. Benefits and drawbacks of subqueries in FROM:

    • Benefits include modularity and the ability to reuse subqueries. Drawbacks may include performance considerations.
    -- 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);
    
  8. Joining multiple tables with subqueries in FROM:

    • Subqueries in the FROM clause can be used to join multiple tables.
    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;
    
  9. Advanced examples of subqueries in FROM clause:

    • Advanced scenarios may involve complex logic and multiple levels of subqueries.
    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;