In SQL, joins are used to combine rows from two or more tables based on a related column between them. Joins allow you to retrieve data from multiple tables as though the data were stored in a single table.
An INNER JOIN returns records that have matching values in both tables. If there is no match, no data is returned from either table for that row.
SELECT columns
FROM table1
INNER JOIN table2 ON table1.common_column = table2.common_column;
SELECT Employees.FirstName, Employees.LastName, Departments.DepartmentName
FROM Employees
INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
This query returns employees and their corresponding department names. Only employees who are assigned to a department will be displayed.
A LEFT JOIN returns all records from the left table (table1), and the matched records from the right table (table2). If there is no match, the result will contain NULL for columns from the right table.
SELECT columns
FROM table1
LEFT JOIN table2 ON table1.common_column = table2.common_column;
SELECT Employees.FirstName, Employees.LastName, Departments.DepartmentName
FROM Employees
LEFT JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
This query retrieves all employees and their department names. Employees without a department will have NULL values in the DepartmentName column.
A RIGHT JOIN is the opposite of a LEFT JOIN. It returns all records from the right table (table2), and the matched records from the left table (table1). If there is no match, the result will contain NULL for columns from the left table.
SELECT columns
FROM table1
RIGHT JOIN table2 ON table1.common_column = table2.common_column;
SELECT Employees.FirstName, Employees.LastName, Departments.DepartmentName
FROM Employees
RIGHT JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
This query retrieves all departments and their corresponding employees. Departments with no employees will still appear, but with NULL for employee details.
A FULL OUTER JOIN returns all records when there is a match in either the left table (table1) or the right table (table2). Rows without a match in one of the tables will still appear, with NULL for missing columns.
SELECT columns
FROM table1
FULL OUTER JOIN table2 ON table1.common_column = table2.common_column;
SELECT Employees.FirstName, Employees.LastName, Departments.DepartmentName
FROM Employees
FULL OUTER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
This query retrieves all employees and all departments, including those without matches. If an employee doesn't belong to a department or if a department has no employees, the missing values will be shown as NULL.
Let’s imagine you have two tables:
EmployeeID | FirstName | LastName | DepartmentID |
---|---|---|---|
1 | John | Doe | 101 |
2 | Jane | Smith | 102 |
3 | Mike | Johnson | NULL |
DepartmentID | DepartmentName |
---|---|
101 | Sales |
102 | Marketing |
103 | IT |
Using different types of joins will result in various outcomes: