SQL Joins using venn diagrams
-
Upload
ifour-institute-sustainable-learning -
Category
Software
-
view
26 -
download
0
Transcript of SQL Joins using venn diagrams
![Page 1: SQL Joins using venn diagrams](https://reader036.fdocuments.in/reader036/viewer/2022070600/58e6787f1a28ab2a298b5ea9/html5/thumbnails/1.jpg)
iFour Consultancy
SQL JOINS using Venn diagrams
![Page 2: SQL Joins using venn diagrams](https://reader036.fdocuments.in/reader036/viewer/2022070600/58e6787f1a28ab2a298b5ea9/html5/thumbnails/2.jpg)
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
![Page 3: SQL Joins using venn diagrams](https://reader036.fdocuments.in/reader036/viewer/2022070600/58e6787f1a28ab2a298b5ea9/html5/thumbnails/3.jpg)
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
![Page 4: SQL Joins using venn diagrams](https://reader036.fdocuments.in/reader036/viewer/2022070600/58e6787f1a28ab2a298b5ea9/html5/thumbnails/4.jpg)
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
![Page 5: SQL Joins using venn diagrams](https://reader036.fdocuments.in/reader036/viewer/2022070600/58e6787f1a28ab2a298b5ea9/html5/thumbnails/5.jpg)
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
![Page 6: SQL Joins using venn diagrams](https://reader036.fdocuments.in/reader036/viewer/2022070600/58e6787f1a28ab2a298b5ea9/html5/thumbnails/6.jpg)
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
![Page 7: SQL Joins using venn diagrams](https://reader036.fdocuments.in/reader036/viewer/2022070600/58e6787f1a28ab2a298b5ea9/html5/thumbnails/7.jpg)
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
![Page 8: SQL Joins using venn diagrams](https://reader036.fdocuments.in/reader036/viewer/2022070600/58e6787f1a28ab2a298b5ea9/html5/thumbnails/8.jpg)
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
![Page 9: SQL Joins using venn diagrams](https://reader036.fdocuments.in/reader036/viewer/2022070600/58e6787f1a28ab2a298b5ea9/html5/thumbnails/9.jpg)
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
![Page 10: SQL Joins using venn diagrams](https://reader036.fdocuments.in/reader036/viewer/2022070600/58e6787f1a28ab2a298b5ea9/html5/thumbnails/10.jpg)
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
![Page 11: SQL Joins using venn diagrams](https://reader036.fdocuments.in/reader036/viewer/2022070600/58e6787f1a28ab2a298b5ea9/html5/thumbnails/11.jpg)
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
![Page 12: SQL Joins using venn diagrams](https://reader036.fdocuments.in/reader036/viewer/2022070600/58e6787f1a28ab2a298b5ea9/html5/thumbnails/12.jpg)
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
![Page 13: SQL Joins using venn diagrams](https://reader036.fdocuments.in/reader036/viewer/2022070600/58e6787f1a28ab2a298b5ea9/html5/thumbnails/13.jpg)
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
![Page 14: SQL Joins using venn diagrams](https://reader036.fdocuments.in/reader036/viewer/2022070600/58e6787f1a28ab2a298b5ea9/html5/thumbnails/14.jpg)
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
![Page 15: SQL Joins using venn diagrams](https://reader036.fdocuments.in/reader036/viewer/2022070600/58e6787f1a28ab2a298b5ea9/html5/thumbnails/15.jpg)
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
![Page 16: SQL Joins using venn diagrams](https://reader036.fdocuments.in/reader036/viewer/2022070600/58e6787f1a28ab2a298b5ea9/html5/thumbnails/16.jpg)
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
![Page 17: SQL Joins using venn diagrams](https://reader036.fdocuments.in/reader036/viewer/2022070600/58e6787f1a28ab2a298b5ea9/html5/thumbnails/17.jpg)
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
![Page 18: SQL Joins using venn diagrams](https://reader036.fdocuments.in/reader036/viewer/2022070600/58e6787f1a28ab2a298b5ea9/html5/thumbnails/18.jpg)
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
![Page 19: SQL Joins using venn diagrams](https://reader036.fdocuments.in/reader036/viewer/2022070600/58e6787f1a28ab2a298b5ea9/html5/thumbnails/19.jpg)
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
![Page 20: SQL Joins using venn diagrams](https://reader036.fdocuments.in/reader036/viewer/2022070600/58e6787f1a28ab2a298b5ea9/html5/thumbnails/20.jpg)
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