
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Joining three or more tables in SQL
In SQL, joining tables is an essential operation that combines data from multiple sources. While joining two tables is straightforward many real-world scenarios require joining three or more tables to retrieve comprehensive information. This article explains how to join three or more tables, complete with examples.
Understanding Joins
SQL joins are used to combine rows from two or more tables based on a related column. The Common types of joins include:
-
INNER JOIN: Returns records that have matching values in both tables.
-
LEFT JOIN (or LEFT OUTER JOIN): Returns all records from the left table and the matching records from the right table.
-
RIGHT JOIN (or RIGHT OUTER JOIN): Returns all records from the right table and the matching records from the left table.
-
FULL JOIN (or FULL OUTER JOIN): Returns records with a match in either table.
Syntax for Joining Three Tables
The general syntax for joining three tables involves chaining multiple JOIN operations:
SELECT columns FROM table1 JOIN table2 ON table1.common_column = table2.common_column JOIN table3 ON table2.common_column = table3.common_column;
Each JOIN operation specifies the relationship between two tables. We can add additional joins for each additional table.
Example: Joining Three Tables
This example demonstrates joining three tables: Employees, Departments, and Salaries. It combines employee details, department names, and salaries to provide information about each employee.
Employees :
EmployeeID | NAME | DepartmentID |
---|---|---|
1 | Alice | 101 |
2 | Bob | 102 |
3 | Charlie | 103 |
Departments :
DepartmentID | DepartmentNAME |
---|---|
101 | HR |
102 | IT |
103 | Finance |
Salaries :
EmployeeID | Salary |
---|---|
1 | 50000 |
2 | 60000 |
3 | 55000 |
Query :
Retrieve the name of each employee their department name and their salary:
SELECT e.Name, d.DepartmentName, s.Salary FROM Employees e JOIN Departments d ON e.DepartmentID = d.DepartmentID JOIN Salaries s ON e.EmployeeID = s.EmployeeID;Result :
Name | DepartmentName | Salary |
---|---|---|
Alice | HR | 50000 |
Bob | IT | 60000 |
Charlie | Finance | 55000 |
Joining More Than Three Tables in SQL
The process for joining more than three tables is similar; simply add additional JOIN clauses.
Example: Joining Four Tables
Assume there's an additional table called Projects:
Project | EmployeeID | ProjectName |
---|---|---|
1 | 1 | Website Design |
2 | 2 | App Development |
3 | 3 | Financial Audit |
SELECT e.Name, d.DepartmentName, s.Salary, p.ProjectName FROM Employees e JOIN Departments d ON e.DepartmentID = d.DepartmentID JOIN Salaries s ON e.EmployeeID = s.EmployeeID JOIN Projects p ON e.EmployeeID = p.EmployeeID;Following is the output of the above query -
Name | DepartmentName | Salary | ProjectName |
---|---|---|---|
Alice | HR | 50000 | Website Design |
Bob | IT | 60000 | App Development |
Charlie | Finance | 55000 | Financial Audit |