SQL Joins

Introduction

SQL Joins are used to combine records from two or more tables based on common fields between them.

The five types of Joins are:

1. SQL INNER JOIN Statement

  • SQL INNER JOIN statement selects all possible rows from both tables, if there is a match between the columns in both tables.
  • The data should not be displayed, if there is no match between columns in both columns.
inner join

Syntax:
SELECT column_name
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

Example : Query using INNER JOIN Statement.

Consider the following table titled 'Employee'.

Emp_IDEmp_NameEmp_Phone
1Marten9022222222
2Paul9033333333
3Albert9044444444
4Steve9055555555

Consider the following table titled  'Salary'.

Emp_IDTransaction_IDAmount
110110000
210220000
310325000
410430000
510530000
610650000

The column 'Emp_ID' is common to both tables. So the query should be written as:

SELECT Employee.Emp_Name, Salary.Transaction_ID, Salary.Amount
FROM Employee
INNER JOIN Salary
ON Salary.Emp_ID = Employee.EMP_ID;

The result is shown in the following table.

Emp_NameTransaction_IDAmount
Marten10110000
Paul10220000
Albert10325000
Steve10530000

2. LEFT JOIN

  • SQL LEFT JOIN or LEFT OUTER JOIN keyword returns all rows from the left table (table1) with the matching rows in the right table (table2).
  • If there is no match found, the result displayed as NULL  if no match found on right column.
left join

Syntax:
SELECT column_name
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

Example : Query using LEFT JOIN statement.

Consider the following table titled 'Employee'.

Emp_IDEmp_NameEmp_Phone
1Marten9022222222
2Paul9033333333
3Albert9044444444
4Steve9055555555

Consider the following table titled 'Salary'.

Emp_IDTransaction_IDAmount
110110000
210220000
310325000
410430000
510530000
610650000

To perform LEFT JOIN on table (Salary), query should be written as:

SELECT Employee.Emp_Name, Employee.Emp_Phone, Salary.Amount
FROM Employee
LEFT JOIN Salary
ON Salary.Emp_ID = Employee.Emp_ID;

Emp_NameEmp_PhoneAmount
Marten902222222210000
Paul903333333320000
Albert904444444425000
Steve905555555530000

3. RIGHT JOIN

  • The SQL RIGHT JOIN or RIGHT OUTER JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1).
  • The result is null in the left table, if there is no match.
right join

Syntax:
SELECT Column_name
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

Example : Query using RIGHT JOIN Statement.

Consider the following table entitled 'Employee'.

Emp_IDEmp_NameEmp_Phone
1Marten9022222222
2Paul9033333333
3Albert9044444444
4Steve9055555555
5Smith9066666666
6Jhon9011111111

Consider the following table entitled as 'Salary'.

Emp_IDTransaction_IDAmount
110110000
210220000
310325000
410430000
510530000

To perform RIGHT JOIN ON table (Employee), query should be written as:

SELECT  Employee.Emp_Name, Salary.Amount
FROM Salary
RIGHT JOIN Employee
ON Salary.Emp_ID = Employee.Emp_ID;

The result is shown in the following table.

Emp_NameAmount
Martin10000
Paul20000
Albert25000
Steve30000
Smith30000
Jhon

4. SQL FULL JOIN

The FULL JOIN or FULL OUTER JOIN statement returns all rows from the left table (table1) and the right table (table2).

full outer join

Syntax:
SELECT column_name
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name;

Example : Query using FULL JOIN Statement.

Consider the following table titled 'Employee'.

Emp_IDEmp_NameEmp_Phone
1Marten9022222222
2Paul9033333333
3Albert9044444444
4Steve9055555555
5Smith9066666666
6Jhon9011111111

Consider the following table entitled 'Salary'.

Emp_IDTransaction_IDAmount
110110000
210220000
310325000
410430000

To perform FULL JOIN, query should be written as:

SELECT Employee.Emp_ID, Employee.Emp_Name, Salary.Transaction_ID, Salary.Amount
FROM Employee
FULL JOIN Salary
ON Salary.Emp_ID = Employee.Emp_ID;

The result is shown in the following table.

Emp_IDEmp_NameTransaction_IDAmount
1Marten10110000
2Paul10220000
3Albert10325000
4Steve10430000
5 Smith
6Jhon

5. CROSS JOIN

  • The SQL CROSS JOIN returns the cartesian product of rows from joined table.
  • A cartesian join is a join of all rows of table1 to all rows of table2.
Syntax:
SELECT * FROM table1 CROSS JOIN table2;

Example : Query using CROSS JOIN Statement.

Write a query to perform cross join on table1 and table2

Table1 : Students

Stud_IDStud_NameSubjectDept_ID
1RaviC01
2SapnaCPP02
3PrajaktaJAVA03

Table2 : Department

Dept_IDDept_Name
01Comp_Science
02IT
03MCA

SELECT Std_Id, Stud_Name, Subject, Dept_Name
FROM students CROSS JOIN Department;

Stud_IDStud_NameSubjectDept_Name
1RaviCComp_Science
2SapnaCPPComp_Science
3PrajaktaJAVAComp_Science
1RaviCIT
2SapnaCPPIT
3PrajaktaJAVAIT
1RaviCMCA
2SapnaCPPMCA
3PrajaktaJAVAMCA