Database Management

Introduction to SQL Joins

Introduction to SQL Joins

Purpose of SQL Joins:

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 SQL Joins 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
3Raj11-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

 

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

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

 

Full Outer Join:

Full Outer Join Full outer join returns all the rows from the left table and from the right table. The resultant table contains all the records from both the tables and fill the un-matched fields with NULL 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
1RameshNULL
5RohanNULL
6VijayNULL
NULLNULL11-09-2021

Cross Join:

Cross Join Cross Join also called Cartesian Join. It is used to return all the records where each row from the first table is combined with each row of the second table. select cs.id, cs.name, ord.date from customers cs cross join orders ord

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

Self Join:

Self Join means a table is joined with itself. A self-join can be used to find the record that matches another record in the same table. It uses the same syntax as a join but the only difference is that the same table is used instead of a different table select cs.id, cs.name from customers cs1, customers cs2 where cs1.state= cs2.state

IDName
3Raj
5Rohan

If you have any queries then feel free to ask in the comment section below.