Database Management

SQL Joins

SQL Joins

Purpose:

Join is the most important concept in SQL. Join is used for getting data from two or more tables however it is observed that people do not understand the right concept of the join and they end up using the wrong join method which might result in the wrong dataset.

Use of Join:

A SQL Join statement is used to combine data from two or more tables based on a common field between them. When we have a large web application and data are huge in multiple tables then we need to combine the data of two or more tables. In such a case, we need to use join queries.

Types of Join:

  • Left Join
  • Right Join
  • Full Outer Join
  • Inner Join
  • Cross Join
  • Self Join

Here, we are using below two tables for Join examples.

 

Examples Tables:

Customers:

IDNameAgeState
1Ramesh20MP
2Mahesh23UP
3Raj23GJ
4Jay24AP
5Rohan25GJ
6VijayNULLNULL

Orders:

IDDateCustomer ID
110-02-20213
211-02-20213
311-04-20212
411-07-20214
511-09-2021NULL

 

Left Join:

Left Join

This join returns all the values which are located in the left table and matched values that are located in the right table.

select cs.id, cs.name, ord.date from customers cs left join orders ord on cs.id = ord.customer_id

IDNameDate
3Raj10-02-2021
3Raj10-02-2021
2Mahesh11-04-2021
4Jay11-07-2021
5RohanNULL
6VijayNULL
1RameshNULL

Description:

The above query returns all records from the customer’s tables and matched records from the orders table.

select cs.id, cs.name, ord.date from orders ord left join customers cs on cs.id = ord.customer_id

IDNameDate
3Raj10-02-2021
3Raj11-02-2021
2Mahesh11-04-2021
4Jay11-07-2021
NULLNULL11-09-2021

Description:

The above query returns all records from orders tables and matched records from the customers table.

 

Right Join:

Right Join

This join returns all the values which are located in the right table and unmatched values which are located in the left table

select cs.id, cs.name, ord.date from customers cs right join orders ord on cs.id = ord.customer_id

IDNameDate
3Raj10-02-2021
3Raj11-02-2021
2Mahesh11-04-2021
4Jay11-07-2021
NULLNULL11-09-2021

Description:

The above query returns all records from orders tables and unmatched records from customers table.

select cs.id, cs.name, ord.date from orders ord right join customers cs on cs.id = ord.customer_id

IDNameDate
3Raj10-02-2021
3Raj11-02-2021
2Mahesh11-04-2021
4Jay11-07-2021
5RohanNULL
6VijayNULL
1RameshNULL

Description:

The above query returns all records from customers table and unmatched records from orders table.

 

Inner Join:

Inner Join

It is used to return only those rows from the tables that matched the specified condition. it returns the common rows from two tables.

select cs.id, cs.name, ord.date from customers cs inner join orders ord on cs.id = ord.customer_id

IDNameDate
3Raj10-02-2021
3Raj11-02-2021
2Mahesh11-04-2021
4Jay11-07-2021

Description:

The above query return matched records from customers and orders table.

 

Full Outer Join:

Full Outer Join

This query return all records from left and right table.

select cs.id, cs.name, ord.date from customers cs full outer join orders ord on cs.id = ord.customer_id

IDNameDate
3Raj10-02-2021
3Raj11-02-2021
2Mahesh11-04-2021
4Jay11-07-2021
NULLNULL11-09-2021
5RohanNULL
6VijayNULL
1RameshNULL

Description:

The above query return all data from customer table and orders table.

select cs.id, cs.name, ord.date from orders ord right join customers cs on cs.id = ord.customer_id

 

Cross Join:

Cross Join

It is used to combine all possibilities of the two or more tables and return the result that contains every row from all contributing tables. It is also known as cartesian join, which means that provides the cartesian product of all associated tables.

select name, date, customer_id from customers cross join orders

NameDateCustomerID
Ramesh10-02-20213
Mahesh10-02-20213
Raj10-02-20213
Jay10-02-20213
Rohan10-02-20213
Vijay10-02-20213
Ramesh11-02-20213
Mahesh11-02-20213
Raj11-02-20213
Jay11-02-20213
Rohan11-02-20213
Vijay11-02-20213
Ramesh11-04-20212
Mahesh11-04-20212
Raj11-04-20212
Jay11-04-20212
Rohan11-04-20212
Vijay11-04-20212
Ramesh11-07-20214
Mahesh11-07-20214
Raj11-07-20214
Jay11-07-20214
Rohan11-07-20214
Vijay11-07-20214
Ramesh11-09-2021NULL
Mahesh11-09-2021NULL
Raj11-09-2021NULL
Jay11-09-2021NULL
Rohan11-09-2021NULL
Vijay11-09-2021NULL

Description:

Above query returns all records from the customers table that contains every row from orders tables.

 

Self Join:

In this join, a table joins with itself

select o1.id, o1.date, o1.customer_id from orders o1, orders o2 where o1.customer_id = o2.customer_id and o1.date != o2.date;

IDDateCustomer ID
110-02-20213
211-02-20213

References:

http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins