Chapter 11: SQL Join

SQL JOIN

  • In SQL, JOIN means “to combine two or more tables”.based on a related column between them.
  • The SQL JOIN clause takes records from two or more tables in a database and combines it together.
  • The joining of two or more tables is based on common field between them.
  • If you want to access more than one table through a select statement.SQL join is used
  • In the process of joining, rows of both tables are combined in a single table.
  • Five types of JOIN:
    • Inner Join,
    • Left join,
    • Right join,
    • Full join, and
    • Cross join.

SQL Inner Join

The INNER JOIN keyword selects records that have matching values in both tables.

example to deploy SQL JOIN process:

1.Staff table

IDStaff_NAMEStaff_AGESTAFF_ADDRESSMonthley_Package
1Arjun23Nagpur18000
2Suhas33Devgad21000
3Mohan27Mumbai23000
4Amol21Elhabad12100

2.Payment table

Payment_IDDATEStaff_IDAMOUNT
10130/12/200913000.00
10223/02/201032700.00
10323/02/201043500.00

Following is the JOIN statement to join these two tables :

SELECT Staff_ID, Staff_NAME, Staff_AGE, AMOUNT   
 FROM STAFF s, PAYMENT p  
 WHERE s.ID =p.STAFF_ID;  

Output result like this:

STAFF_IDNAMEStaff_AGEAMOUNT
3Mohan272700
1Arjun233000
4Amol273500
1Arjun233000

SQL LEFT JOIN

The SQL left join returns all the values from the left table(table1) and it also includes matching values from right table(table2).

 If there are no matching join value it returns NULL.

SYNTAX FOR LEFT JOIN:

SELECT table1.column1, table2.column2....  
FROM table1   
LEFTJOIN table2  
ON table1.column_field = table2.column_field;  

let us take two tables:

CUSTOMER TABLE1:

IDNAMEAGESALARY
1Arjun5156000
2Saloni2127000
3Vandana2431000
4Anurag2333000
5Priya2342100

ORDER TABLE2:

O_IDDATECUSTOMER_IDAMOUNT
00121-01-201223000
00212-02-201222100
00323-03-201234000
00411-04-201245000

join these two tables with LEFT JOIN:

SQL SELECT ID, NAME, AMOUNT,DATE  
FROM CUSTOMER  
LEFT JOIN ORDER  
ON CUSTOMER.ID = ORDER.CUSTOMER_ID;  

This will produce the following Output:

IDNAMEAMOUNTDATE
1ArjunNULLNULL
2Saloni300021-01-2012
2Saloni210012-02-2012
3Vandana400023-03-2012
4Anurag500011-04-2012
5PriyaNULLNULL

SQL RIGHT JOIN

The SQL right join returns all the values from the rows of right table(Table2) and also includes the matched values from left table (table1). if there is no matching in both tables, it returns NULL.

Syntax for right join:

SELECT table1.column1, table2.column2.....  
FROM table1   
RIGHT JOIN table2  
ON table1.column_field = table2.column_field;  

let us take an example with 2 tables table1 is CUSTOMERS table and table2 is ORDERS table.

CUSTOMER TABLE1:

IDNAMEAGESALARY
1Arjun5156000
2Saloni2127000
3Vandana2431000
4Anurag2333000
5Priya2342100

ORDER TABLE2:

DATEO_IDCUSTOMER_IDAMOUNT
21-01-201200123000
12-02-201200222100
23-03-201200334000
11-04-201200445000

Here we will join these two tables with SQL RIGHT JOIN:

SELECT ID,NAME,AMOUNT,DATE  
FROM CUSTOMER  
RIGHT JOIN ORDER  
ON CUSTOMER.ID = ORDER.CUSTOMER_ID;  
IDNAMEAMOUNTDATE
2Saloni300021-01-2012
2Saloni210012-02-2012
3Vandana400023-03-2012
4Anurag500011-04-2012

SQL FULL JOIN

The result of combination of both left and right outer join and the join tables have all the records from both tables is known as SQL full join is. It puts NULL on the place of matches not found.

SQL full outer join and SQL join are same.

SQL full outer join:

The FULL OUTER JOIN keyword return all records when there is a match in left (table1) or right (table2) table records.

Note: FULL OUTER JOIN can potentially return very large result-sets!

Tip: FULL OUTER JOIN and FULL JOIN are the same.

Syntax for full outer join:

Let us take two tables to demonstrate full outer join:

table_A

NumbersCharacter_A
1m
2n
4o

table_B

NumbersCharactor_B
2p
3q
5r

Resulting table

NumbersCharacter_ANumbersCharactor_B
2n2p
1m
4o
3q
5r

Because this is a full outer join so all rows (both matching and non-matching) from both tables are included in the output. Here only one row of output displays values in all columns because there is only one match between table_A and table_B.

SQL Cross Join

In general words we can say that SQL CROSS JOIN returns the Cartesian product of the sets of rows from the joined table.When each row of first table is combined with each row from the second table, known as Cartesian join or cross join

We can specify a CROSS JOIN in two ways Using the JOIN syntax or the table in the FROM clause without using a WHERE clause.

SYNTAX of SQL Cross Join

SELECT * FROM [TABLE1] CROSS JOIN [TABLE2]  
OR  
SELECT * FROM [ TABLE1] , [TABLE2]  

Let us take an example of two tables,

Table1 – MatchScore

PlayerDepartment_idGoals
Rohan12
Amruta13
Pariniti22
Rutuja35

Table2 – Departments

Department_idDepartment_name
1IT
2HR
3Marketing

SQL Statement:

SELECT * FROM MatchScore CROSS JOIN Departments  

After executing this query , you will find the following result:

PlayerDepartment_idGoalsDepatment_idDepartment_name
Rohan121IT
Amruta131IT
Pariniti221IT
Rutuja351IT
Rohan122HR
Amruta132HR
Pariniti222HR
Rutuja352HR
Rohan123Marketing
Amruta133Marketing
Pariniti223Marketing
Rutuja353Marketing