×

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

SQL Cloning Tables

Cloning a Table: To create a copy of the table. To perform the operations, without affecting the actual table. Steps for creating a Cloning Table: Step 1: Empty Table Creation The syntax for Creating...

3 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 Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

2 minutes read.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

SQL NOT Operator

SQL NOT is a Boolean operator used with the WHERE clause. NOT operator shows the records if the expression is false. When we use the NOT operator, we fetch only...

7 minutes read.

SQL INSERT Table

In this tutorial, we will help you to understand and learn how to add records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

DELETE VS DROP in SQL

This page contains all the information about the Delete command and Drop command concept and the difference between the DELETE and DROP commands in SQL. What is the DELETE command in...

3 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 UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

3 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 SELECT LIKE Operator

The SQL SELECT LIKE Operator tutorial helps us understand how to use the LIKE operator in the SELECT query with examples. The SQL SELECT LIKE Operator retrieves the records from the...

4 minutes read.

SQL Aliases

SQL Aliases: These are used to give a temporary name for a column or table. These are used to make tables or columns more readable. These are used to rename the table...

2 minutes read.

SQL SELECT MIN

The SQL Min() function is an aggregate function in SQL. SQL Min() returns the minimum value of a given condition. The expression may be numerical, or it may be an expression. The syntax...

5 minutes read.

Check Constraint in SQL

The Check Constraint in SQL is the rule or set of rules used to limit the data range that can be entered in a table column. Check constraint is used...

8 minutes read.

Grant Command in SQL

What is DCL (Data Control Language)? Data Control Language (DCL) is a computer programming language with syntax intended to manage access to data kept in databases. It is a part of...

3 minutes read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

SQL SELECT Statement

The SQL SELECT statement is used to retrieve the data from the tables. We can also retrieve the selected records from the table using the query condition. The SQL SELECT...

5 minutes read.

SQL Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

4 minutes read.

How to remove duplicates in SQL

Introduction There are some specific rules that needs to be followed while creating the database objects. To improve the performance of a database, a primary key, clustered and non-clustered indexes, and...

9 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 minutes read.