SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

SQL | Joining three or more tables

Joining three or more tables in SQL is a continuation of the same principles used when joining two tables. You essentially chain the joins, specifying the join conditions for each pair of tables.

Consider the following three tables:

  1. users: user_id, username
  2. orders: order_id, user_id, product_id
  3. products: product_id, product_name

Example: To find out which user ordered which product, you need to join all three tables.

SELECT users.username, products.product_name
FROM users
JOIN orders ON users.user_id = orders.user_id
JOIN products ON orders.product_id = products.product_id;

Here's a breakdown:

  1. First, users is joined with orders based on user_id.
  2. The intermediate result is then joined with products based on product_id.

You can also combine different types of joins:

SELECT users.username, products.product_name
FROM users
LEFT JOIN orders ON users.user_id = orders.user_id
INNER JOIN products ON orders.product_id = products.product_id;

In this example:

  1. We're using a LEFT JOIN between users and orders. This means that even if a user hasn't made any orders, they'll still appear in the intermediate result.
  2. We then use an INNER JOIN with products. This means only those rows with a valid product will appear in the final result.

Notes:

  • Always ensure you're joining on indexed columns, especially in large tables, to maintain query performance.
  • Keep track of the type of each join as you chain them together, as the order and type can affect your results.
  • If dealing with tables from different databases or schemas, make sure to specify the full table name (e.g., database_name.table_name).
  • Always test your multi-table joins to ensure they're returning the expected results.
  1. Joining Multiple Tables in SQL Query:

    SELECT employees.employee_id, employees.employee_name, departments.department_name, projects.project_name
    FROM employees
    INNER JOIN departments ON employees.department_id = departments.department_id
    INNER JOIN employee_projects ON employees.employee_id = employee_projects.employee_id
    INNER JOIN projects ON employee_projects.project_id = projects.project_id;
    
  2. How to Perform a Three-Table Join in SQL:

    • Use multiple INNER JOIN clauses to connect the necessary tables.
    SELECT *
    FROM table1
    INNER JOIN table2 ON table1.column_id = table2.column_id
    INNER JOIN table3 ON table2.column_id = table3.column_id;
    
  3. Joining Four or More Tables in a Single SQL Query:

    SELECT *
    FROM table1
    INNER JOIN table2 ON table1.column_id = table2.column_id
    INNER JOIN table3 ON table2.column_id = table3.column_id
    INNER JOIN table4 ON table3.column_id = table4.column_id;
    
  4. SQL INNER JOIN with Three or More Tables:

    • Continue to use INNER JOIN for each additional table needed.
    SELECT *
    FROM table1
    INNER JOIN table2 ON table1.column_id = table2.column_id
    INNER JOIN table3 ON table2.column_id = table3.column_id
    INNER JOIN table4 ON table3.column_id = table4.column_id;
    
  5. Complex Multi-Table Joins in SQL Explained:

    • Complex joins involve multiple tables with intricate relationships.
    SELECT *
    FROM employees
    INNER JOIN departments ON employees.department_id = departments.department_id
    LEFT OUTER JOIN employee_projects ON employees.employee_id = employee_projects.employee_id
    INNER JOIN projects ON employee_projects.project_id = projects.project_id
    WHERE projects.status = 'Active';
    
  6. Outer Join with Three or More Tables in SQL:

    • Use OUTER JOIN for tables where you want to retain unmatched rows.
    SELECT *
    FROM table1
    LEFT OUTER JOIN table2 ON table1.column_id = table2.column_id
    INNER JOIN table3 ON table2.column_id = table3.column_id;
    
  7. Joining Tables Using Subqueries in SQL:

    • Subqueries can be used for more complex conditions.
    SELECT *
    FROM table1
    INNER JOIN (
        SELECT column_id, column_name
        FROM table2
        WHERE condition
    ) AS subquery ON table1.column_id = subquery.column_id;