×

SQL JOIN

As the name says, joins mean to merge something or to combine something. But in the case of SQL, joins mean to merge or combine two different tables.

The Join clause uses in two tables, which are the parent and child tables. Join clause takes records from one or more tables and combines the records.

There are different types of joins used in SQL are as follows:

1. Inner Join

2. Left Outer Join

3. Right Outer Join

4. Full Outer Join

SQL Joins takes records from two different tables but displays results in a single table.

Let's understand each type of join one by one with the example.

1. Inner Join

Inner Join in SQL is a widely used join. It takes all the records from both tables until and unless the conditions match. It means this join will return only those common rows in both tables.

The syntax of the inner join is as follows:

SELECT Table_Name_1.Column_Name_1, Table_Name_1.Column_Name_2, Table_Name_2.Column_Name_1, Table_Name_2.Column_Name_2 FROM Table_Name_1 INNER JOIN Table_Name_2 ON Table_Name_1.Column_Name = Table_Name_2.Column_Name;

 Let’s understand inner join more by looking at an example:

We have two tables:

Table Name 1:  Employee_Details table with certain columns

E_id                E_NameE_salaryE_City 
1001                    Pranoti Shende60000    Pune  
1002                  Vaibhav Sharma58000Mumbai
1003                        Nikhil Vani50000Mumbai
1004              Prachi Sharma60000    Hyderabad
1005Harshada Koli48500Nashik
1006Sonal Maheshwari50000Bangalore
1007Bhavesh Jain65000Pune
1008Kapil Verma50000Nashik
1009Rajesh Goud55000Hyderabad
1010Deepam Jauhari60000Bangalore

Table Name 2: Comp

Table with certain columns (E_Id Common in both tables)

 Comp_Id         MakeE_Id
101Dell1001
102Dell1004
103 Lenovo1006
104Lenovo1008
105 HP1010
106HP1005
107Asus1003

We want to display employee Id, names, and make from the comp table.

To produce this as output, execute this query as follow:

SELECT ED.E_Id, ED.E_Name, Make FROM Employee_Details ED INNER JOIN Comp C ON ED.E_Id = C.E_Id; 

The output of the above query is as follows:

E_id                E_NameMake
1001                    Pranoti ShendeDell
1004              Prachi SharmaDell
1006Sonal MaheshwariLenovo
1008Kapil VermaLenovo
1010Deepam JauhariHP
1005Harshada KoliHP
1003                        Nikhil VaniAsus
SQL JOIN

Suppose, while using inner joins, there can be a situation where you want to filter rows based on some criteria. In such a case, we can use the where clause.

Let's take an example and understand the example of how where clause works in joins:

SELECT ED.E_Id, ED.E_Name, ED.E_City, Make FROM Employee_Details ED INNER JOIN Comp C ON ED.E_Id = C.E_Id WHERE E_City = ‘Bangalore’; 

We display the employee's information whose employee resides in Bangalore City in the above query.

The output of the above query is as follows:

E_id                E_NameE_CityMake
1006Sonal MaheshwariBangaloreLenovo
1010Deepam JauhariBangaloreHP
SQL JOIN

For example, you want to display employee id, name, city and salary, comp id and make where group by comp Id; then we will execute the below query as follow:

SELECT ED.E_Id, ED.E_Name, ED.E_Salary, ED.E_City, C.Comp_Id, Make FROM Employee_Details ED INNER JOIN Comp C ON ED.E_Id = C.E_Id GROUP BY Comp_Id;

The output of the above query is as follows:

E_id                E_NameE_salaryE_City  Comp_Id         Make
1001                    Pranoti Shende60000    Pune  101Dell
1004              Prachi Sharma60000Hyderabad102Dell
1006Sonal Maheshwari50000Bangalore103 Lenovo
1008Kapil Verma50000    Nashik104Lenovo
1010Deepam Jauhari60000Bangalore105 HP
1005Harshada Koli48500Nashik106HP
1003                        Nikhil Vani50000Mumbai107Asus
SQL JOIN

2. Left Outer Join

The left outer join returns all the records from (left table) table one, whether the record in table 2 matches or not, according to the join condition. The record, which matches their result set is the same as the inner join result, and all uncommon records from another table will result in null.

Let’s understand Left Outer join more by looking at an example:

We have two tables:

Table Name 1:  Employee_Details table with certain columns

E_id                E_NameE_salaryE_City 
1001                    Pranoti Shende60000    Pune  
1002                  Vaibhav Sharma58000Mumbai
1003                        Nikhil Vani50000Mumbai
1004              Prachi Sharma60000    Hyderabad
1005Harshada Koli48500Nashik
1006Sonal Maheshwari50000Bangalore
1007Bhavesh Jain65000Pune
1008Kapil Verma50000Nashik
1009Rajesh Goud55000Hyderabad
1010Deepam Jauhari60000Bangalore

Table Name 2: Comp

Table with certain columns (E_Id Common in both tables)

 Comp_Id         MakeE_Id
101Dell1001
102Dell1004
103 Lenovo1006
104Lenovo1008
105 HP1010
106HP1005
107Asus1003

We want to display the employee’s name, id from the employee's table and make a name, and comp id from the comp table

To produce this as output, execute this query as follow:

SELECT ED.E_Id, ED.E_Name, C.Comp_Id, Make FROM Employee_Details ED LEFT JOIN Comp C ON ED.E_Id = C.E_Id;

The output of the above query is as follows:

E_id                  E_NameComp_IdMake
1001                    Pranoti Shende101Dell
1002                  Vaibhav SharmaNULLNULL
1003                        Nikhil Vani107Asus
1004              Prachi Sharma102Dell
1005Harshada Koli106HP
1006Sonal Maheshwari103Lenovo
1007Bhavesh JainNULLNULL
1008Kapil Verma104Lenovo
1009Rajesh GoudNULLNULL
1010Deepam Jauhari105HP
SQL JOIN

Suppose, while using left outer joins, there can be a situation where you want to filter rows based on some criteria. In such cases, we can use the where clause.

Let's take an example and understand the example of how where clause works in joins:

SELECT ED.E_Id, ED.E_Name, ED.E_Salary, C.Comp_Id, Make FROM Employee_Details ED LEFT JOIN Comp C ON ED.E_Id = C.E_Id WHERE E_Salary BETWEEN 50000 AND 60000;

In the above query, we display the employee's information whose employee salary is between 50000 and 60000.

The output of the above query is as follows:

E_id                  E_NameE_salaryComp_IdMake
1001                    Pranoti Shende60000    101Dell
1002                  Vaibhav Sharma58000NULLNULL
1003                        Nikhil Vani50000107Asus
1004              Prachi Sharma60000    102Dell
1006Sonal Maheshwari50000103Lenovo
1008Kapil Verma50000104Lenovo
1009Rajesh Goud55000NULLNULL
1010Deepam Jauhari60000105HP
SQL JOIN

3. Right Outer Join

In Right Outer, join for both tables will return all the records from table 2, whether the record in table 1 matches or not to the join condition. The record which matches their result set is the same as the inner join result, and all nonmatching records from another table will result in null.

Let’s understand Right Outer join more by looking at an example:

We have two tables:

Table Name 1:  Employee_Details table with certain columns

E_id                E_NameE_salaryE_City 
1001                    Pranoti Shende60000    Pune  
1002                  Vaibhav Sharma58000Mumbai
1003                        Nikhil Vani50000Mumbai
1004              Prachi Sharma60000    Hyderabad
1005Harshada Koli48500Nashik
1006Sonal Maheshwari50000Bangalore
1007Bhavesh Jain65000Pune
1008Kapil Verma50000Nashik
1009Rajesh Goud55000Hyderabad
1010Deepam Jauhari60000Bangalore

Table Name 2: Comp

Table with certain columns (E_Id Common in both tables)

 Comp_Id         MakeE_Id
101Dell1001
102Dell1004
103 Lenovo1006
104Lenovo1008
105 HP1010
106HP1005
107Asus1003

We want to display the employee’s name, id from the employee's table and make a name, and comp id from the comp table

To produce this as output, execute this query as follow:

SELECT C.Comp_Id, Make, ED.E_Id, ED.E_Name FROM Comp C RIGHT OUTER JOIN Employee_Details ED ON ED.E_Id = C.E_Id;

The output of the above query is as follows:

Comp_IdMakeE_id                E_Name
101Dell1001                    Pranoti Shende
NULLNULL1002                  Vaibhav Sharma
107Asus1003                        Nikhil Vani
102Dell1004              Prachi Sharma
106HP1005Harshada Koli
103Lenovo1006Sonal Maheshwari
NULLNULL1007Bhavesh Jain
104Lenovo1008Kapil Verma
NULLNULL1009Rajesh Goud
105HP1010Deepam Jauhari
SQL JOIN

Suppose, while using right outer joins, there can be a situation where you want to filter rows based on some criteria. In such cases, we can use the where clause.

Let's take an example and understand the example of how where clause works in joins:

SELECT C.Comp_Id, Make, ED.E_Id, ED.E_Name, ED.E_Salary FROM Comp C RIGHT OUTER JOIN Employee_Details ED ON ED.E_Id = C.E_Id WHERE E_Salary BETWEEN 50000 AND 60000;

In the above query, we display the employee's information whose employee salary is between 50000 and 60000.

The output of the above query is as follows:

Comp_IdMakeE_id                  E_NameE_salary
101Dell1001                    Pranoti Shende60000    
NULLNULL1002                  Vaibhav Sharma58000
107Asus1003                        Nikhil Vani50000
102Dell1004              Prachi Sharma60000    
103Lenovo1006Sonal Maheshwari50000
104Lenovo1008Kapil Verma50000
NULLNULL1009Rajesh Goud55000
105HP1010Deepam Jauhari60000
SQL JOIN

4. Full Outer Join

Full Outer Join merges the result of both outer joins, Left Outer Joins and the Right Outer Join. Full Outer Joins returns the output, which matches rows and unmatched rows between the two tables. Full Outer Join is the same as Cross Join.

Let’s understand Full Outer join more by looking at an example:

We have two tables:

Table Name 1:  Employee_Details table with certain columns

E_id                E_NameE_salaryE_City 
1001                    Pranoti Shende60000    Pune  
1002                  Vaibhav Sharma58000Mumbai
1003                        Nikhil Vani50000Mumbai
1004              Prachi Sharma60000    Hyderabad
1005Harshada Koli48500Nashik
1006Sonal Maheshwari50000Bangalore
1007Bhavesh Jain65000Pune
1008Kapil Verma50000Nashik
1009Rajesh Goud55000Hyderabad
1010Deepam Jauhari60000Bangalore

Table Name 2: Comp

Table with certain columns (E_Id Common in both tables)

 Comp_Id         MakeE_Id
101Dell1001
102Dell1004
103 Lenovo1006
104Lenovo1008
105 HP1010
106HP1005
107Asus1003

We want to display all the details from the employee_details and comp tables where salary is between 55000 and 60000. We will use the below query:

SELECT * FROM Employee_Details FULL JOIN Comp WHERE E_Salary BETWEEN 55000 AND 60000;

The output of the above query is as follows:

E_id                E_NameE_salaryE_City  Comp_Id        MakeE_Id
1001                    Pranoti Shende60000    Pune  101Dell1001
1001                    Pranoti Shende60000    Pune  102Dell1004
1001                    Pranoti Shende60000    Pune  103Lenovo1006
1001                    Pranoti Shende60000    Pune  104Lenovo1008
1001                    Pranoti Shende60000    Pune  105HP1010
1001                    Pranoti Shende60000    Pune  106HP1005
1001                    Pranoti Shende60000    Pune  107Asus1003
1002Vaibhav Sharma58000Mumbai101Dell1001
1002Vaibhav Sharma58000Mumbai102Dell1004
1002Vaibhav Sharma58000Mumbai103Lenovo1006
1002Vaibhav Sharma58000Mumbai104Lenovo1008
1002Vaibhav Sharma58000Mumbai105HP1010
1002Vaibhav Sharma58000Mumbai106HP1005
1002Vaibhav Sharma58000Mumbai107Asus1003
1004Prachi Sharma60000Hyderabad101Dell1001
1004Prachi Sharma60000Hyderabad102Dell1004
1004Prachi Sharma60000Hyderabad103Lenovo1006
1004Prachi Sharma60000Hyderabad104Lenovo1008
1004Prachi Sharma60000Hyderabad105HP1010
1004Prachi Sharma60000Hyderabad106HP1005
1004Prachi Sharma60000Hyderabad107Asus1003
1009Rajesh Goud55000Hyderabad101Dell1001
1009Rajesh Goud55000Hyderabad102Dell1004
1009Rajesh Goud55000Hyderabad103Lenovo1006
1009Rajesh Goud55000Hyderabad104Lenovo1008
1009Rajesh Goud55000Hyderabad105HP1010
1009Rajesh Goud55000Hyderabad106HP1005
1009Rajesh Goud55000Hyderabad107Asus1003
1010Deepam Jauhari60000Bangalore101Dell1001
1010Deepam Jauhari60000Bangalore102Dell1004
1010Deepam Jauhari60000Bangalore103Lenovo1006
1010Deepam Jauhari60000Bangalore104Lenovo1008
1010Deepam Jauhari60000Bangalore105HP1010
1010Deepam Jauhari60000Bangalore106HP1005
1010Deepam Jauhari60000Bangalore107Asus1003
SQL JOIN

Related Topics

GROUP BY vs ORDER BY

The GROUP BY clause and ORDER BY clause in SQL are used to arrange data obtained by SQL queries. The important difference between the GROUP BY clause and ORDER BY...

4 minutes read.

SQL GROUP BY CLAUSE

The GROUP BY clause is used to arrange similar records into the groups in the Structured Query Language queries. The records are arranged with the help of functions which is...

4 minutes read.

Where vs Having

Difference Between Where and Having The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause...

3 minutes read.

How to create a database in SQL?

How to create a database in SQL It is very necessary to create a database in order to store the data into the database.Database name must always be unique.SQL does not...

6 minutes read.

How to Update Table in SQL?

How to Update Table in SQL Introduction UPDATE query is used to update a record in a table. UPDATE is a DML command, which operates on the data of the table and not...

4 minutes read.

Types of SQL JOIN

The SQL JOIN combines one or more than one tables based on their relationship. The SQL JOIN involves a parent table and a child table relationship. There are different types of...

10 minutes read.

SQL INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help...

5 minutes read.

SQL Commands

SQL commands are classified into four groups on the basis of their nature. DDL (Data Definition Language) DML (Data Manipulation Language) DCL (Data Control Language) DQL (Data Query Language) NOTE: This...

1 minute read.

Save Point in SQL

In SQL, the classification is done into 4 languages. They are Data Definition Language (DDL)Data Manipulation Language (DML)Transaction Control Language (TCL)Data Control Language (DCL) Save Point falls under the Transaction Control Language....

4 minutes read.

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands. What is Drop Command? Drop is a Data Definition Language command in...

6 minutes read.

How to delete a row in SQL

Introduction To delete or remove the unused/unwanted rows from a table, DELETE command is used in SQL.DELETE command removes the entire record from a table.The DELETE statement can delete one or...

6 minutes read.

SQL SubQuery

The Sub-query in the SQL is the inner query placed or positioned inside another query, which is also known is the outer query. The inner query is embedded in the...

6 minutes read.

SQL Handling Duplicate

Removing Duplicates using DISTINCT Keyword: By using the DISTINCT keyword in SQL we can remove duplicate characters from tables or databases. A table contains more duplicate values and duplicate values can cause...

4 minutes read.

TCL Commands in SQL

In Structured Query Language, TCL is an abbreviation for Transaction Control Language. A single unit of work in a database is formed after the consecutive execution of commands is known...

6 minutes read.

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the...

5 minutes read.

SQL CASE

This page contains all the information about SQL CASE. The CASE is an If-Else type of logical query used in the statement. The CASE in Structured Query Language is similar...

5 minutes read.

SQL IN vs SQL EXISTS

SQL IN vs SQL EXISTS This article discusses in detail about the IN and the EXISTS operators in SQL. It is a common question between developers that what is the difference...

3 minutes read.

Introduction to SQL

SQL Introduction SQL is Standard Query Language. This language is used to communicate or interact with database. In other words, SQL is used to access and manage data or information...

2 minutes read.

SQL LOGICAL Operator

In this tutorial, we will understand the operator who falls under the logical operator in SQL with the help of examples. The SQL Logical Operator displays the query result in one...

5 minutes read.

How to Add Comments in SQL?

How to Add Comments in SQL Comments are text notes that are incorporated into the program to make the code easier to understand. In SQL, commenting is used to explain various sections...

2 minutes read.