The IoT Academy Blog

What is SQL Joins and Its Types – Explained With Diagram

  • Written By The IoT Academy 

  • Published on March 14th, 2024

In computer science and software engineering, knowing about data structures and algorithms is like understanding the basic elements of technology. Among these basics are searching and sorting very important methods. Whether you’re new to coding or have been doing it for a while, learning these methods is crucial. This guide will teach you all about them, including which methods to use and when.

What is SQL?

SQL, or Structured Query Language, is a common language. Which we use for managing databases. Also, it lets you do things like ask questions, add new information, and set up how to organize a database. With SQL, you can use simple commands like SELECT, INSERT, UPDATE, DELETE, and SQL Joins to work with data. It’s a handy tool for anyone dealing with data, whether you’re a programmer, data analyst, or someone who manages databases.

What are SQL Joins?

SQL joins help combine information from different tables in a database by matching rows with similar values in specified columns. Therefore, This creates a single result with data from multiple sources. As well as they are crucial for getting data from relational databases. Most of the people use it a lot in managing databases.

SQL Joins Types

Joins in SQL are like combining pieces from different puzzles. As well as, by looking at how they fit together based on a shared piece. Also, there are several types of SQL joins, each serving a different purpose:

types-of-sql-joins

  1. INNER JOIN

An inner join in SQL only shows rows from both tables. That has matching values, leaving out any rows that don’t match. Also, it is useful for getting data when you need a connection between tables.

 

inner-join

SELECT *

FROM tableA

INNER JOIN tableB

ON tableA.name = tableB.name


INNER-JOIN-Table

The resulting table will be as follows:

inner-join-table-result
  1. LEFT JOIN (or LEFT OUTER JOIN)

Left SQL joins are also called SQL left outer joins. It displays all rows from the left table along with any matching rows from the right table. Also, If there are no matches in the right table, it still shows the rows from the left table. As well as fills in NULL values for the right table’s columns. This join guarantees that all left table rows are included, even if they don’t match anything in the right table.

left-join

SELECT *

FROM TableA

LEFT OUTER JOIN TableB

ON tableA.name = tableB.name;

left-join-table

The resulting table will be as follows:

left-join-table-result
  1. RIGHT JOIN (or RIGHT OUTER JOIN)

A right join is also called a right outer join. As well as it shows all rows from the right table and any matching rows from the left table. If there are no matches in the left table. Therefore, it displays rows from the right table and fills in NULL values for the left table’s columns. This join guarantees that all right table rows are included, even if they don’t match anything in the left table.

right-join

SELECT *

FROM tableA

RIGHT OUTER JOIN tableB

ON tableA.name = tableB.name

right-join-table

This will show the result as:

right-join-table-result
  1. CROSS JOIN

CROSS JOIN in SQL combines every row of one table with every row of another table, creating all possible pairs of rows without needing any matching condition. This can make the result set very large if the tables have many rows.

cross-join

SELECT *

FROM tableA

CROSS JOIN tableB;

cross-join-table-result

This will show the result as:

cross-join-table-result

Joins Based on Operators

  1. FULL JOIN (or FULL OUTER JOIN)

A full join SQL, also called as SQL outer join, combines the results of both left and right joins. It includes all rows from both tables and uses NULL values for unmatched rows. As well as this join ensures all rows from both tables are in the result. Even if they don’t match in the other table.

full-join

SELECT *

FROM TableA

FULL OUTER JOIN TableB

ON TableA.name = TableB.name;

full-outer-join-table

The resulting table will be as follows:

full-join-table-result
  1. NATURAL JOIN

A NATURAL JOIN in SQL works to match columns with the same names in both tables being joined. Without needing to specify the columns explicitly. However, it can cause problems if there are columns with the same name but different meanings. Therefore, it simplifies joining tables but needs careful use to avoid mistakes.

SELECT *

FROM tableA

NATURAL JOIN tableB

SQL Joins Example

Here’s an example of an SQL join using the Employees and Departments tables:

Employees Table:

EmployeeID

Name

DepartmentID

1

John

101

2

Jane

102

3

Bob

103

4

Alice

101

 

Departments Table:

DepartmentID

Name

101

IT

102

Marketing

103

HR

 

Suppose we want to retrieve the names of employees along with their respective departments. We can achieve this using an INNER JOIN:

SELECT Employees.Name, Departments.Name AS Department

FROM Employees

INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;

 

This query will return:

Name

Department

John

IT

Jane

Marketing

Bob

IT

Alice

HR

 

In this example, the INNER JOIN merges data from both tables. Using the DepartmentID column, create a single result. As well as by showing employee names and their departments.

Learners Also Read: DML and DDL in SQL – Explain SQL Commands in Detail

Conclusion

In conclusion, SQL joins help put together information from different tables in databases. Learning about the different types of joins. Also, how to use them can help you find and study data effectively for your work. Understanding SQL joins is important for anyone who wants to work with data or build databases.

Frequently Asked Questions
Q. How to join 5 tables in SQL?

Ans. To join five tables in SQL, first, connect two tables with a JOIN based on their related columns. Then, keep adding more tables with additional JOINs, making sure each new join links to the previously joined tables. This lets you combine all five tables and get the data you want with just one query.

Q. Can I join 4 tables?

Ans. Yes, you can join four tables in SQL. First, connect two tables with a JOIN, then add a third table with another JOIN. Finally, include the fourth table with an extra JOIN. Just make sure each join condition connects to the previously joined tables for a smooth connection.

About The Author:

The IoT Academy as a reputed ed-tech training institute is imparting online / Offline training in emerging technologies such as Data Science, Machine Learning, IoT, Deep Learning, and more. We believe in making revolutionary attempt in changing the course of making online education accessible and dynamic.

logo

Digital Marketing Course

₹ 9,999/-Included 18% GST

Buy Course
  • Overview of Digital Marketing
  • SEO Basic Concepts
  • SMM and PPC Basics
  • Content and Email Marketing
  • Website Design
  • Free Certification

₹ 29,999/-Included 18% GST

Buy Course
  • Fundamentals of Digital Marketing
  • Core SEO, SMM, and SMO
  • Google Ads and Meta Ads
  • ORM & Content Marketing
  • 3 Month Internship
  • Free Certification
Trusted By
client icon trust pilot
1whatsapp