Understanding SQL Joins – All You Need To Know About SQL Joins

1.1K Views
3 / 4 Blog from Introduction to SQL

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assement report and video recording

Structured Query Language aka SQL is the core of relational databases with the help of which we can handle data. It provides us with various features such as Triggers, Injection, Hosting and, Joins is just one of the most important concept to master in SQL. In this article on SQL Joins, I will discuss the various types of Joins used in SQL.

The following topics will be covered in this article

What are Joins?

JOINS in SQL are commands which are used to combine rows from two or more tables, based on a related column between those tables.  There are predominantly used when a user is trying to extract data from tables which have one-to-many or many-to-many relationships between them.

Now, that you know what joins mean, let us next learn the different types of joins.

Different Types of Joins

There are mainly four types of joins that you need to understand. They are:

You can refer to the below image.

Types Of Joins In SQL - SQL Joins - EdurekaLet us look into each one of them. For your better understanding of this concept, I will be considering the following three tables to show you how to perform the Join operations on such tables.

Employee Table:

EmpIDEmpFnameEmpLnameAgeEmailIDPhoneNoAddress
1VardhanKumar22vardy@abc.com9876543210Delhi
2HimaniSharma32himani@abc.com9977554422Mumbai
3AayushiShreshth24aayushi@abc.com9977555121Kolkata
4HemanthSharma25hemanth@abc.com9876545666Bengaluru
5SwateeKapoor26swatee@abc.com9544567777Hyderabad

Project Table:

ProjectIDEmpIDClientIDProjectNameProjectStartDate
11113Project12019-04-21
22221Project22019-02-12
33335Project32019-01-10
44432Project42019-04-16
55554Project52019-05-23
66691Project62019-01-12
77772Project72019-07-25
88883Project82019-08-20

Client Table:

ClientIDClientFnameClientLnameAgeClientEmailIDPhoneNoAddressEmpID 
1SusanSmith30susan@adn.com9765411231Kolkata3
2MoisAli27mois@jsq.com9876543561Kolkata3
3SomaPaul22soma@wja.com9966332211Delhi1
4ZainabDaginawala40zainab@qkq.com9955884422Hyderabad5
5BhaskarReddy32bhaskar@xyz.com9636963269Mumbai2

INNER JOIN

This type of join returns those records which have matching values in both tables. So, if you perform an INNER join operation between the Employee table and the Projects table, all the tuples which have matching values in both the tables will be given as output.

Syntax:

SELECT Table1.Column1,Table1.Column2,Table2.Column1,....
FROM Table1
INNER JOIN Table2
ON Table1.MatchingColumnName = Table2.MatchingColumnName;
NOTE: You can either use the keyword INNER JOIN or JOIN to perform this operation.

Example:


SELECT Employee.EmpID, Employee.EmpFname, Employee.EmpLname, Projects.ProjectID, Projects.ProjectName
FROM Employee
INNER JOIN Projects ON Employee.EmpID=Projects.EmpID;

Output:

EmpIDEmpFnameEmpLnameProjectIDProjectName
1VardhanKumar111Project1
2HimaniSharma222Project2
3AayushiShreshth333Project3
3AayushiShreshth444Project4
5SwateeKapoor555Project5

FULL JOIN

Full Join or the Full Outer Join returns all those records which either have a match in the left(Table1) or the right(Table2) table.

Syntax:

SELECT Table1.Column1,Table1.Column2,Table2.Column1,....
FROM Table1
FULL JOIN Table2
ON Table1.MatchingColumnName = Table2.MatchingColumnName;

Example:


SELECT Employee.EmpFname, Employee.EmpLname, Projects.ProjectID
FROM Employee
FULL JOIN Projects
ON Employee.EmpID = Projects.EmpID;

Output:

EmpFnameEmpLnameProjectID
VardhanKumar111
 HimaniSharma222
AayushiShreshth333
AayushiShreshth444
HemanthSharmaNULL
SwateeKapoor555
NULLNULL666
NULLNULL777
NULLNULL888

LEFT JOIN

The LEFT JOIN or the LEFT OUTER JOIN  returns all the records from the left table and also those records which satisfy a condition from the right table. Also, for the records having no matching values in the right table, the output or the result-set will contain the NULL values.

Syntax:

SELECT Table1.Column1,Table1.Column2,Table2.Column1,....
FROM Table1
LEFT JOIN Table2
ON Table1.MatchingColumnName = Table2.MatchingColumnName;

Example:


SELECT Employee.EmpFname, Employee.EmpLname, Projects.ProjectID, Projects.ProjectName
FROM Employee
LEFT JOIN
ON Employee.EmpID = Projects.EmpID ;

Output:

EmpFnameEmpLnameProjectIDProjectName
VardhanKumar111Project1
HimaniSharma222Project2
AayushiShreshth333Project3
AayushiShreshth444Project4
SwateeKapoor555Project5
HemanthSharmaNULLNULL

RIGHT JOIN

The RIGHT JOIN or the RIGHT OUTER JOIN  returns all the records from the right table and also those records which satisfy a condition from the left table. Also, for the records having no matching values in the left table, the output or the result-set will contain the NULL values.

Syntax:

SELECT Table1.Column1,Table1.Column2,Table2.Column1,....
FROM Table1
RIGHT JOIN Table2
ON Table1.MatchingColumnName = Table2.MatchingColumnName;

Example:


SELECT Employee.EmpFname, Employee.EmpLname, Projects.ProjectID, Projects.ProjectName
FROM Employee
RIGHT JOIN
ON Employee.EmpID = Projects.EmpID;

Output:

EmpFnameEmpLnameProjectIDProjectName
VardhanKumar111Project1
HimaniSharma222Project2
AayushiShreshth333Project3
AayushiShreshth444Project4
SwateeKapoor555Project5
NULLNULL666Project6
NULLNULL777Project7
 NULL NULL888Project8

Now, let us move forward with our next section in this article i.e. the top questions asked about SQL Joins in your interviews.

Most Common Questions asked about Joins

Question 1: What is a Natural Join and in which situations is a natural join used?

Solution:

A Natural Join is also a Join operation that is used to give you an output based on the columns in both the tables between which, this join operation must be implemented. To understand the situations n which natural join is used, you need to understand the difference between Natural Join and Inner Join.

The main difference the Natural Join and the Inner Join relies on the number of columns returned. Refer below for example.

Tables Example - SQL Joins - Edureka

Now, if you apply INNER JOIN on these 2 tables, you will see an output as below:

Tables Inner Join Example - SQL Joins - Edureka

If you apply NATURAL JOIN, on the above two tables, the output will be as below:

Tables Natural Join Example - SQL Joins - EdurekaFrom the above example, you can clearly see that the number of columns returned from the Inner Join is more than that of the number of columns returned from Natural Join. So, if you wish to get an output, with less number of columns, then you can use Natural Join

Question 2: How to map many-to-many relationships using joins?

Solution:

To map many to many relationships using joins, you need to use two JOIN statements.

For example, if we have three tables(Employees, Projects and Technologies), and let us assume that each employee is working on a single project. So, one project cannot be assigned to more than one employee. So, this is basically, a one-to-many relationship.

Now, similarly, if you consider that, a project can be based on multiple technologies, and any technology can be used in multiple projects, then this kind of relationship is a many-to-many relationship.

To use joins for such relationships, you need to structure your database with 2 foreign keys. So, to do that, you have to create the following 3 tables:

  • Projects
  • Technologies
  • projects_to_technologies

The project_to_technologies table holds the combinations of project-technology in every row. This table maps the items on the projects table to the items on the technologies table so that multiple projects can be assigned to one or more technologies.

Once the tables are created, use the following two JOIN statements to link all the above tables together:

  • projects_to_technologies to projects
  • proejcts_to-technologies to technologies

Question 3: What is a Hash Join?

Solution:

Hash joins are also a type of joins which are used to join large tables or in an instance where the user wants most of the joined table rows.

The Hash Join algorithm is a two-step algorithm. Refer below for the steps:

  • Build phase: Create an in-memory hash index on the left side input
  • Probe phase: Go through the right side input, each row at a time to find the matches using the index created in the above step.

Question 4: What is Self & Cross Join?

Solution:

Self Join

SELF JOIN in other words is a join of a table to itself. This implies that each row in a table is joined with itself.

Cross Join

The CROSS JOIN is a type of join in which a join clause is applied to each row of a table to every row of the other table. Also, when the WHERE condition is used, this type of JOIN behaves as an INNER JOIN, and when the WHERE condition is not present, it behaves like a CARTESIAN product.

Question 5: How to perform JOIN operation on three tables?

Solution:

To perform a JOIN operation on 3 tables, you need to use 2 JOIN statements. You can refer to the second question for an understanding of how to join 3 tables with an example.

NOTE: To apply a JOIN operation between ‘n‘ tables, you have to use ‘n-1‘ JOIN statements.

Now that you know SQL Joins, I’m sure you’re curious to learn more about SQL. Here’s a list of articles that you can refer to:

  1. SQL Commands
  2. SQL Data Types
  3. Spark SQL
  4. SQL Interview Questions
  5. What is MYSQL?

    By this, I come to the end of this article on SQL Joins. I hope you enjoyed reading this article on SQL Joins. We have seen the different commands that will help you write queries and play around with your databases. If you wish to learn more about MySQL and get to know this open source relational database, then check out our MySQL DBA Certification Training which comes with instructor-led live training and real-life project experience. This training will help you understand MySQL in depth and help you achieve mastery over the subject.

    Got a question for us? Please mention it in the comments section of ”SQL Joins” and I will get back to you.

    Comments
    0 Comments

    Browse Categories

    Subscribe to our Newsletter, and get personalized recommendations.