20
iFour Consultancy SQL JOINS using Venn diagrams

SQL Joins using venn diagrams

Embed Size (px)

Citation preview

Page 1: SQL Joins using venn diagrams

iFour Consultancy

SQL JOINS using Venn diagrams

Page 2: 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

Page 3: SQL Joins using venn diagrams

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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