DEV Community

Asim khan
Asim khan

Posted on

Join the Party: A Comprehensive Guide to SQL Joins with Examples

Introduction

In the world of database management, joining tables is a crucial operation that allows you to combine data from multiple tables into a single result set. SQL joins are used to retrieve data from two or more tables based on a related column between them. In this blog, we'll delve into the different types of SQL joins, including inner join, left join, right join, and outer join, along with examples to help you understand the concepts better.

Image description

What is a SQL Join?

A SQL join is a clause used to combine rows from two or more tables based on a related column between them. The join condition is specified in the ON clause, which defines the relationship between the tables. The result of a join operation is a new table that contains columns from both tables.

Image description
Types of SQL Joins

1. Inner Join
An inner join returns only the rows that have a match in both tables. It combines rows from two or more tables where the join condition is met.

Example:

Suppose we have two tables, Customers and Orders, and we want to retrieve the customer names and their corresponding order details.

Customers table:

CustomerID  CustomerName
1           John Smith
2           Jane Doe
3           Bob Brown
Enter fullscreen mode Exit fullscreen mode

Orders table:

OrderID CustomerID  OrderDate
1   1           2022-01-01
2   1           2022-01-15
3   2           2022-02-01
Enter fullscreen mode Exit fullscreen mode

Inner Join:

SELECT Customers.CustomerName, Orders.OrderDate
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID;
Enter fullscreen mode Exit fullscreen mode

Result:

CustomerName    OrderDate
John Smith  2022-01-01
John Smith  2022-01-15
Jane Doe    2022-02-01
Enter fullscreen mode Exit fullscreen mode

Image description
2. Left Join (or Left Outer Join)

A left join returns all the rows from the left table and the matching rows from the right table. If there are no matches, the result will contain null values for the right table columns.

Example:

Using the same Customers and Orders tables, let's retrieve all customers, even if they don't have any orders.

Left Join:

SELECT Customers.CustomerName, Orders.OrderDate
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID = Orders.CustomerID;
Enter fullscreen mode Exit fullscreen mode
Result:

CustomerName    OrderDate
John Smith  2022-01-01
John Smith  2022-01-15
Jane Doe    2022-02-01
Bob Brown   null
Enter fullscreen mode Exit fullscreen mode

3. Right Join (or Right Outer Join)

A right join is similar to a left join, but it returns all the rows from the right table and the matching rows from the left table. If there are no matches, the result will contain null values for the left table columns.

Example:

Using the same Customers and Orders tables, let's retrieve all orders, even if there is no matching customer.

Right Join:

SELECT Customers.CustomerName, Orders.OrderDate
FROM Customers
RIGHT JOIN Orders
ON Customers.CustomerID = Orders.CustomerID;
Enter fullscreen mode Exit fullscreen mode
Result:

CustomerName    OrderDate
John Smith  2022-01-01
John Smith  2022-01-15
Jane Doe    2022-02-01
null            2022-03-01
Enter fullscreen mode Exit fullscreen mode

4. Left Join with Null
A left join with null returns all the rows from the left table where the join condition is not met, i.e., where the right table columns are null.

Example:

Using the same Customers and Orders tables, let's retrieve all customers who don't have any orders.

Left Join with Null:

SELECT Customers.CustomerName
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
WHERE Orders.CustomerID IS NULL;
Enter fullscreen mode Exit fullscreen mode

Result:

CustomerName
Bob Brown
Enter fullscreen mode Exit fullscreen mode

5. Right Join with Null

A right join with null returns all the rows from the right table where the join condition is not met, i.e., where the left table columns are null.

Example:

Using the same Customers and Orders tables, let's retrieve all orders that don't have a matching customer.

Right Join with Null:

SELECT Orders.OrderDate
FROM Customers
RIGHT JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
WHERE Customers.CustomerID IS NULL;
Enter fullscreen mode Exit fullscreen mode

Result:

OrderDate
2022-03-01
Enter fullscreen mode Exit fullscreen mode

6. Full Outer Join

A full outer join returns all rows from both tables, with null values in the columns where there are no matches.

Example:

Using the same Customers and Orders tables, let's retrieve all customers and orders, even if there are no matches.

Full Outer Join:


SELECT Customers.CustomerName, Orders.OrderDate
FROM Customers
FULL OUTER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID;
Enter fullscreen mode Exit fullscreen mode

Result:

CustomerName    OrderDate
John Smith  2022-01-01
John Smith  2022-01-15
Jane Doe    2022-02-01
Bob Brown   null
null            2022-03-01
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this blog, we've explored the different types of SQL joins, including inner join, left join, right join, and outer join, along with examples to help you understand the concepts better. By mastering these join operations, you'll be able to retrieve data from multiple tables and perform complex queries with ease. Remember to practice these examples and experiment with different join operations to become proficient in SQL joins.

if you still have any confusion

Image description

still then

Image description

Top comments (0)