View
29
Download
0
Embed Size (px)
iFour Consultancy
SQL JOINS using Venn diagrams
What is Join? A relational operation that causes two or more tables with a common domain to be
combined into a single table or view SQL joins creates a set of row in a temporary table
SQL Joins
http://www.ifourtechnolab.com/ Software Outsourcing Company India
EQUI JOINS EQUI JOINS is a simple SQL join. Uses equals to(=) sign as the comparison operator for the condition.
NON EQUI JOINS NON EQUI JOINS uses comparison operator other than the equal sign. The operator used like >, <, >=, <= with the condition.
Types of SQL Joins
http://www.ifourtechnolab.com/ Software Outsourcing Company India
Types of SQL EQUI JOIN
INNER JOIN It returns on matched row from the participating table. Match happened only at the key record of the participating table.
OUTER JOIN Returns all rows from one table and matching row from the secondary table. And the comparison columns should be equal in both the table.
http://www.ifourtechnolab.com/ Software Outsourcing Company India
Lists of SQL JOINS
INNER JOIN
LEFT JOIN or LEFT OUTER JOIN
RIGHT JOIN or RIGHT OUTER JOIN
FULL OUTER JOIN
http://www.ifourtechnolab.com/ Software Outsourcing Company India
INNER JOIN
The INNER JOIN selects all rows from both participating tables as long as there is a match between the columns.
An SQL INNER JOIN is same as JOIN clause, combining rows from two or more tables.
http://www.ifourtechnolab.com/ Software Outsourcing Company India
Example : INNER JOIN
ID Address1 Delhi
2 Mumbai
3 Chennai
ID Name1 Divyesh
2 Astaj
3 Nishith
4 Aakash
Table : Employee Table : Employee_City
http://www.ifourtechnolab.com/ Software Outsourcing Company India
Example : INNER JOIN (Cont.)
SELECT * FROM Employee INNER JOIN Employee_City on Employee.ID = Employee_city.ID;
OUTPUT
QUERY
ID Name ID Address
1 Divyesh 1 Delhi
2 Astaj 2 Mumbai
3 Nishith 3 ChennaiEmployee Employee_City
http://www.ifourtechnolab.com/ Software Outsourcing Company India
LEFT JOIN or LEFT OUTER JOIN
The LEFT OUTER JOIN, joins two tables and fetches rows based on a condition, which are matching in both the tables.
The unmatched rows will also be available from the table before the JOIN clause.
http://www.ifourtechnolab.com/ Software Outsourcing Company India
Example : LEFT OUTER JOIN or LEFT JOIN
ID Address1 Delhi
2 Mumbai
3 Chennai
5 Ahmedabad
ID Name1 Divyesh
2 Astaj
3 Nishith
4 Aakash
Table : Employee Table : Employee_City
http://www.ifourtechnolab.com/ Software Outsourcing Company India
SELECT * FROM Employee LEFT JOIN Employee_City on Employee.ID = Employee_City.ID;
Example : LEFT OUTER JOIN or LEFT JOIN(Cont.)
OUTPUT
QUERY
ID Name ID Address
1 Divyesh 1 Delhi
2 Astaj 2 Mumbai
3 Nishith 3 Chennai
4 Aakash NULL NULL
Employee Employee_City
http://www.ifourtechnolab.com/ Software Outsourcing Company India
SELECT * FROM Employee LEFT JOIN Employee_City on Employee.ID = Employee_City.ID Where Employee_City.ID is NULL;
Example : LEFT OUTER JOIN or LEFT JOIN(Cont.)
OUTPUT
QUERY
Employee Employee_City
ID Name ID Address
4 Aakash NULL NULL
http://www.ifourtechnolab.com/ Software Outsourcing Company India
RIGHT JOIN or RIGHT OUTER JOIN
The RIGHT OUTER JOIN, joins two tables and fetches rows based on a condition, which are matching in both the tables.
The unmatched rows will also be available from the table after the JOIN clause.
http://www.ifourtechnolab.com/ Software Outsourcing Company India
Example : RIGHT OUTER JOIN or RIGHT JOIN
ID Address1 Delhi
2 Mumbai
3 Chennai
5 Ahmedabad
ID Name1 Divyesh
2 Astaj
3 Nishith
4 Aakash
Table : Employee Table : Employee_City
http://www.ifourtechnolab.com/ Software Outsourcing Company India
SELECT * FROM Employee RIGHT JOIN Employee_City on Employee.ID = Employee_City.ID;
Example : RIGHT OUTER JOIN or RIGHT JOIN(Cont.)
OUTPUT
QUERY
ID Name ID Address1 Divyesh 1 Delhi
2 Astaj 2 Mumbai
3 Nishith 3 Chennai
null null 5 Ahmedabad
Employee Employee_City
http://www.ifourtechnolab.com/ Software Outsourcing Company India
SELECT * FROM Employee RIGHT JOIN Employee_City OnEmployee.ID= Employee_City.IDWhere Employee.ID is NULL;
Example : RIGHT OUTER JOIN or RIGHT JOIN(Cont.)
OUTPUT
QUERY
Employee Employee_City
ID Name ID Address
NULL NULL 5 Ahmedabad
http://www.ifourtechnolab.com/ Software Outsourcing Company India
FULL OUTER JOIN
Combines the result of both left outer and right outer join
Returns all match and unmatched rows.
Includes tables on both side of join clause
http://www.ifourtechnolab.com/ Software Outsourcing Company India
Example : FULL OUTER JOIN
ID Address1 Delhi
2 Mumbai
3 Chennai
5 Ahmedabad
ID Name1 Divyesh
2 Astaj
3 Nishith
4 Aakash
Table : Employee Table : Employee_City
http://www.ifourtechnolab.com/ Software Outsourcing Company India
SELECT * FROM Employee FULL OUTER JOIN Employee_City on Employee.ID = Employee_City.ID;
Example : FULL OUTER JOIN(Cont.)
OUTPUT
QUERY
ID Name ID Address
1 Divyesh 1 Delhi
2 Astaj 2 Mumbai
3 Nishith 3 Chennai
4 Aakash null null
null null 5 Ahmedabad
Employee Employee_City
http://www.ifourtechnolab.com/ Software Outsourcing Company India
select * from Employee FULL OUTER JOIN Employee_Cityon Employee.ID = Employee_City.IDWHERE Employee.ID is NULL orEmployee_City.ID is NULL
Example : FULL OUTER JOIN(Cont.)
OUTPUT
QUERY
Employee Employee_City
ID Name ID Address
4 Aakash null null
null null 5 Ahmedabad
http://www.ifourtechnolab.com/ Software Outsourcing Company India