Types of Joins in SQL Explained
Q: What are the different types of joins in SQL, and how do they differ from each other?
- SQL Developer
- Junior level question
Explore all the latest SQL Developer interview questions and answers
ExploreMost Recent & up-to date
100% Actual interview focused
Create SQL Developer interview for FREE!
In SQL, there are several types of joins that are used to combine rows from two or more tables based on a related column between them. The main types of joins are:
1. INNER JOIN: This join returns only the rows that have matching values in both tables. For example, if we have a `Customers` table and an `Orders` table, an INNER JOIN on `CustomerID` would return only the customers who have placed orders.
```sql
SELECT Customers.CustomerID, Customers.CustomerName
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
```
2. LEFT JOIN (or LEFT OUTER JOIN): This join returns all rows from the left table and the matched rows from the right table. If there is no match, NULL values will be returned for columns from the right table. For instance, to find all customers and their orders (including customers with no orders):
```sql
SELECT Customers.CustomerID, Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
```
3. RIGHT JOIN (or RIGHT OUTER JOIN): This is the opposite of the LEFT JOIN. It returns all rows from the right table and matched rows from the left table. For example, if we want to see all orders and the corresponding customers (including orders with no associated customers):
```sql
SELECT Customers.CustomerID, Customers.CustomerName, Orders.OrderID
FROM Customers
RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
```
4. FULL JOIN (or FULL OUTER JOIN): This join combines the results of both LEFT and RIGHT joins. It returns all rows when there is a match in either left or right table records. For example, to get a complete list of customers and orders, including those without matches:
```sql
SELECT Customers.CustomerID, Customers.CustomerName, Orders.OrderID
FROM Customers
FULL JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
```
5. CROSS JOIN: This join returns the Cartesian product of two tables, meaning it joins every row of the first table with every row of the second table. For example, using a `Products` table and a `Categories` table, it would show all combinations of products and categories:
```sql
SELECT Products.ProductName, Categories.CategoryName
FROM Products
CROSS JOIN Categories;
```
6. SELF JOIN: This is a join where a table is joined with itself. This is useful for comparing rows within the same table. For example, to find pairs of employees that work in the same department:
```sql
SELECT a.EmployeeID, a.EmployeeName, b.EmployeeName AS ColleagueName
FROM Employees a
JOIN Employees b ON a.DepartmentID = b.DepartmentID AND a.EmployeeID <> b.EmployeeID;
```
Each type of join serves specific purposes, depending on the relationship between the data in the involved tables and the desired result set.
1. INNER JOIN: This join returns only the rows that have matching values in both tables. For example, if we have a `Customers` table and an `Orders` table, an INNER JOIN on `CustomerID` would return only the customers who have placed orders.
```sql
SELECT Customers.CustomerID, Customers.CustomerName
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
```
2. LEFT JOIN (or LEFT OUTER JOIN): This join returns all rows from the left table and the matched rows from the right table. If there is no match, NULL values will be returned for columns from the right table. For instance, to find all customers and their orders (including customers with no orders):
```sql
SELECT Customers.CustomerID, Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
```
3. RIGHT JOIN (or RIGHT OUTER JOIN): This is the opposite of the LEFT JOIN. It returns all rows from the right table and matched rows from the left table. For example, if we want to see all orders and the corresponding customers (including orders with no associated customers):
```sql
SELECT Customers.CustomerID, Customers.CustomerName, Orders.OrderID
FROM Customers
RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
```
4. FULL JOIN (or FULL OUTER JOIN): This join combines the results of both LEFT and RIGHT joins. It returns all rows when there is a match in either left or right table records. For example, to get a complete list of customers and orders, including those without matches:
```sql
SELECT Customers.CustomerID, Customers.CustomerName, Orders.OrderID
FROM Customers
FULL JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
```
5. CROSS JOIN: This join returns the Cartesian product of two tables, meaning it joins every row of the first table with every row of the second table. For example, using a `Products` table and a `Categories` table, it would show all combinations of products and categories:
```sql
SELECT Products.ProductName, Categories.CategoryName
FROM Products
CROSS JOIN Categories;
```
6. SELF JOIN: This is a join where a table is joined with itself. This is useful for comparing rows within the same table. For example, to find pairs of employees that work in the same department:
```sql
SELECT a.EmployeeID, a.EmployeeName, b.EmployeeName AS ColleagueName
FROM Employees a
JOIN Employees b ON a.DepartmentID = b.DepartmentID AND a.EmployeeID <> b.EmployeeID;
```
Each type of join serves specific purposes, depending on the relationship between the data in the involved tables and the desired result set.


