×

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

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 compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 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.

SQL Select Distinct

The SQL DISTINCT query is used to fetch unique values from the tables using the SELECT statement in the SQL. There may be a situation that arises when you want to...

4 minutes read.

Codd’s Rules in SQL

Codd’s Rules Dr. Edgar F. Codd, in 1985, laid down 13 fundamental rules after doing large-scale research on the Relational Model of databases. According to him, every database must follow these...

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

Data definition language directly effects on the structure/schema of the database. CREATE, ALTER, DROP are the commands of DDL.CREATE: Creates new database, table, or view of table.ALTER: Modifies the database or...

3 minutes read.

SQL KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst...

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

How to drop a column in SQL?

How to drop a column in SQL Introduction To delete a column from an already created table, one needs to use the ALTER command along with the DROP COLUMN clause. Syntax: ALTER TABLE tablename...

4 minutes read.

SQL Data Types

In SQL DATA TYPES, each column holds value. Data types can be an integer type, character type, etc., and such data is stored in the database table. The data type is...

4 minutes read.

SQL ORDER BY

SQL ORDER BY The SQL ORDER BY clause is used to sort the data stored in tables in the database. The sorting can be done in an ascending way, descending way,...

5 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 use INNER JOIN in SQL

In this article, we will learn about the INNER JOIN concept and how to use it in SQL with the WHERE clause. What is INNER JOIN in SQL? Inner Join is a...

6 minutes read.

DML Commands in SQL

DML is an abbreviation of Data Manipulation Language. Data Manipulation Language commands in Structured Query Language manipulate the data in the database. DML commands are used to retrieve records, add records,...

4 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 Auto Increment

As we all know, In SQL for unique identification, we assign a column with the primary key. But in some tables, we find difficult to differentiate a column as a...

5 minutes read.

SQL Insert multiple rows

This article will deal with a new insertion method in SQL which inserts multiple rows at a time. Multiple records can be inserted at a time into a specific table with...

4 minutes read.

SQL SELECT OR Operator

This SQL tutorial explains and helps us understand how to use the OR operator in the SELECT query with examples. The OR operator in the Structured Query Language is used to...

3 minutes read.

SQL Right Join

The SQL Right Join query displays all the table records and similar records from the left table. The query display zero records if it doesn’t find any similar records. If...

4 minutes read.