SlideShare a Scribd company logo
JOINS IN SQL
-VARSHA KUMARI
content
 Joins:
 Cartesian Product
 Natural Join
 Equi Join
 Self Join
 Inner Join
 outer Join(Left, Right, Full)
JOINS
 A JOIN combines together information from two or
more tables into one result set.
 The records from the table are fetched based on
some values that are common to each.
 It can also specifies some condition while
retrieving the data.
 Join = cross product + some condition
Cartesian product
 Before understanding JOINS in SQL we
should know about Cartesian Product.
 Also known as Cross Product.
Cartesian Product in sets(Mathematics)
 A = { 1, 2, 3} B = { a , b}
 A x B = { (1,a) , (2,a), (3,a), (1, b), (2,b) , (3,b) }
Cartesian product
 R1 R2
 R1 x R2
A B
2 E1
4 E2
5 E3
B C
E1 5
E5 7
A B B C
2 E1 E1 5
4 E2 E1 5
5 E3 E1 5
.. .. .. ..
Cartesian product
 R1 R2
 R1 x R2
A B
2 E1
4 E2
5 E3
B C
E1 5
E5 7
A B B C
2 E1 E1 5
4 E2 E1 5
5 E3 E1 5
2 E1 E5 7
4 E2 E5 7
5 E3 E5 7
Example : Consider two tables
Table creation
Cartesian product of two table
QUERY: select * from emp2,dept;
Joins
Cartesian Product :
 Cartesian Product is the all possible combinations
between applied table rows.
 Suppose two tables are T1 and T2
 T1 has r1 rows and c1 columns
 T2 has r2 rows and c2 columns
 Cartesian product of T1 and T2 will have:
 r1 * r2 rows and c1 + c2 columns.
Natural Join: Common columns in both the tables
must have same name.
After Natural Join :
 Select * from R1, R2
where R1.B=R2.B;
OR Select *
from R1 natural join R2;
=
A B
2 E1
4 E2
5 E3
B C
E1 5
E2 7
A B B C
2 E1 E1 5
4 E2 E1 5
5 E3 E1 5
2 E1 E2 7
4 E2 E2 7
5 E3 E2 7
A B C
2 E1 5
4 E2 7
Natural JOIN:
Select * from Emp2 natural join Dept;
- Behaves Same as Inner Join
- In Natural Join common attribute must have
same name in both the tables
Example:
 Select emp_name, loc from Emp2 natural join Dept
where loc= 'NEW YORK';
Equi Join
 R1 R2
 After Equi Join :
 Select *from R1,R2
 where R1.B=R2.C;
 or Select *from R1 join R2
 on R1.B=R2.C;
A B
2 E1
4 E2
5 E3
C D
E1 5
E2 7
A B C D
2 E1 E1 5
4 E2 E1 5
5 E3 E1 5
2 E1 E2 7
4 E2 E2 7
5 E3 E2 7
A B C D
2 E1 E1 5
4 E2 E2 7
Equi Join:
 Same as inner join , it uses only equality
comparison.
 Query : Select * from Emp2, dept where
emp2.deptno=dept.deptno;
Self Join
 A self Join is a type of Join which is used to join a
table to itself. In this join both the columns belongs
to the same table.
 Syntax :
 Select T1.column, T1.column from T1 A inner join T2 B on
A.column= B.column
Self Join
 R1
After Equi Join :
Select T.B
from R1 T, R1 S
where T.A=S.A
and T.B <> S.B;
A B
2 E1
2 E2
5 E3
A B A B
2 E1 2 E1
2 E2 2 E1
5 E3 2 E1
2 E1 2 E2
2 E2 2 E2
5 E3 2 E2
2 E1 5 E3
2 E2 5 E3
5 E3 5 E3
B
E2
E1
Select * from Emp2 A , Emp2 B
where A.Deptno = B.Deptno;
Inner Join
 Inner Join combines the rows retrieved from
multiple tables on the basis of common columns of
the tables.
 It takes that rows from the cartesian product table
where the join elements ( emp.deptno and
dept.deptno in the above query) matches fully
 Syntax:
 Select T1.column, T2.column from T1 inner join T2
on T1.column=T2.column
Inner Join
 R1 R2
 After Inner Join
=
A B
2 E1
4 E2
5 E3
B C
E1 5
E2 7
A B B C
2 E1 E1 5
4 E2 E1 5
5 E3 E1 5
2 E1 E2 7
4 E2 E2 7
5 E3 E2 7
A B B C
2 E1 E1 5
4 E2 E2 7
Select * from Emp2 inner join Dept on
Emp2.Deptno = Dept.Deptno
example
 Select * from Emp2 inner join Dept on
Emp2.Deptno = Dept.Deptno
where loc= 'NEW YORK';
Example:
 Select emp_name,loc from Emp2 inner join Dept
on Emp2.Deptno = Dept.Deptno where loc= 'NEW
YORK';
Left Outer Join
 Left outer Join takes that rows which are in inner
join output.
 And it also looks for the rows in the left table
which are not in the inner join output.
 The rows are added to the output with null in right
columns.
Left Join
 R1 R2
 After Left Join
=
A B
2 E1
4 E2
5 E3
B C
E1 5
E2 7
A B B C
2 E1 E1 5
4 E2 E1 5
5 E3 E1 5
2 E1 E2 7
4 E2 E2 7
5 E3 E2 7
A B B C
2 E1 E1 5
4 E2 E2 7
5 E3 NUL
L
NUL
L
Syntax:
 Select T1.column, T2.column from T1 left outer
join T2 on T1.column=T2.column
Eg. Select * from Emp2 left outer join Dept on
Emp2.Deptno = Dept.Deptno;
Right Outer Join
 Right outer Join takes that rows which are in inner
join output.
 And it also looks for the rows in the right table
which are not in the inner join output.
 The rows are added to the output with null in left
columns.
Right Join
 R1 R2
 After right Join
=
A B
2 E1
4 E2
5 E3
B C
E1 5
E5 7
A B B C
2 E1 E1 5
4 E2 E1 5
5 E3 E1 5
2 E1 E5 7
4 E2 E5 7
5 E3 E5 7
A B B C
2 E1 E1 5
NULL NULL E5 7
Syntax:
 Select T1.column, T2.column from T1 right outer
join T2 on T1.column=T2.column
Eg. Select * from Emp2 right outer join Dept on
Emp2.Deptno = Dept.Deptno;
Full Outer Join
 Full outer Join takes that rows which are in inner
join output.
 And it also looks for the rows in the left and also
right table which are not in the inner join output.
Full Join
 R1 R2
 After Full Join
=
A B
2 E1
4 E2
5 E3
B C
E1 5
E5 7
A B B C
2 E1 E1 5
4 E2 E1 5
5 E3 E1 5
2 E1 E5 7
4 E2 E5 7
5 E3 E5 7
A B B C
2 E1 E1 5
NULL NULL E5 7
4 E2 NULL NULL
5 E3 NULL NULL
Syntax:
 Select T1.column, T2.column from T1 full outer
join T2 on T1.column=T2.column
Eg. Select * from Emp2 full outer join Dept on
Emp2.Deptno = Dept.Deptno;
Consider two table
Student Faculty
S_id S_name course
1 Amit BCA
2 Ram Btech
3 Hardik BCA
4 Pawan BCA
5 Sumit Btech
6 Aman MCA
7 Hardik BCA
F_Id course Loc
F1 BCA AB 10
F2 Btech AB 1
F3 MTech AB1
 Cartesian product:
 Select * from Student, Faculty;
 Natural Join:
 Select * from Student natural join Faculty
 Equi join:
 Select * from student join Faculty on
Student.course=Faculty.course
 Self join:
 Select A.S_name from Student A, Student B where
A.S_id= B.S_id
 Inner Join:
 Select Student.S_name from Student inner join
Faculty on Student.course= Faculty.course where
Faculty.loc=‘AB10’
 Left Join:
 Select Student.S_name from Student left outer join
Faculty on Student.course= Faculty.course where
Faculty.loc=‘AB10’
 Right Join:
 Select Student.S_name from Student right outer join
Faculty on Student.course= Faculty.course where
Faculty.loc=‘AB10’

More Related Content

PPTX
Database architecture
PPTX
VALIDATION BASED PROTOCOL AND MULTIPLE GRANULARITY.pptx
PPT
The relational database model
PPTX
Relational Database Design
PPTX
Relational model
PPT
Fundamentals of Database ppt ch02
PPTX
All data models in dbms
PPTX
Relational Algebra,Types of join
Database architecture
VALIDATION BASED PROTOCOL AND MULTIPLE GRANULARITY.pptx
The relational database model
Relational Database Design
Relational model
Fundamentals of Database ppt ch02
All data models in dbms
Relational Algebra,Types of join

What's hot (20)

PPTX
Adbms 39 algorithms for project and set operations
PPT
Entity relationship (er) modeling
PDF
Functional Dependency
PPTX
Functional dependency
PPTX
Relational Data Model Introduction
PPTX
Mining single dimensional boolean association rules from transactional
PPTX
Adbms 6 three schema database architecture
PPTX
Integrity Constraints
PPT
Fundamentals of Database ppt ch03
PDF
MongoDB Lab Manual (1).pdf used in data science
PPTX
DMBS Indexes.pptx
PPTX
17.INTRODUCTION TO SCHEMA REFINEMENT.pptx
PPTX
Chapter 09 design and analysis of algorithms
PPTX
Distribution transparency and Distributed transaction
PDF
Dbms 3: 3 Schema Architecture
PPTX
Types Of Keys in DBMS
PPTX
relational algebra (joins)
PDF
DBMS Unit - 6 - Transaction Management
PPTX
Dbms 4NF & 5NF
PPTX
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
Adbms 39 algorithms for project and set operations
Entity relationship (er) modeling
Functional Dependency
Functional dependency
Relational Data Model Introduction
Mining single dimensional boolean association rules from transactional
Adbms 6 three schema database architecture
Integrity Constraints
Fundamentals of Database ppt ch03
MongoDB Lab Manual (1).pdf used in data science
DMBS Indexes.pptx
17.INTRODUCTION TO SCHEMA REFINEMENT.pptx
Chapter 09 design and analysis of algorithms
Distribution transparency and Distributed transaction
Dbms 3: 3 Schema Architecture
Types Of Keys in DBMS
relational algebra (joins)
DBMS Unit - 6 - Transaction Management
Dbms 4NF & 5NF
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
Ad

Similar to Joins (20)

PPTX
REC-UNIT-2-DATABASEMANAGEMENTSYSTEMS.pptx
PPT
Internal tables
PDF
To excel or not?
ODP
Using Lateral derived table in Informix database
PPT
Sql joins
PPT
Aspire it sap abap training
PPT
Best SAP ABAP Training Institute with Placement in Pune | Aspire
PPTX
Join in SQL - Inner, Self, Outer Join
PDF
Lesson 6 - Relational Algebra.pdf
DOCX
Sap internal
PPT
Interactive spreadsheet basics[1]
PDF
cdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
PPTX
Lecture 6 data structures and algorithms
PPTX
Joins (A JOIN clause is used to combine rows from two or more tables, based o...
PPTX
Joins in SQL
PPTX
SQL JOINS- Reena P V
PPTX
This is a presentation on the parsing.pptx
PPTX
5.stack
DOCX
3rd Quarter_Math 7 Reviewer 22024-25 .docx
REC-UNIT-2-DATABASEMANAGEMENTSYSTEMS.pptx
Internal tables
To excel or not?
Using Lateral derived table in Informix database
Sql joins
Aspire it sap abap training
Best SAP ABAP Training Institute with Placement in Pune | Aspire
Join in SQL - Inner, Self, Outer Join
Lesson 6 - Relational Algebra.pdf
Sap internal
Interactive spreadsheet basics[1]
cdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
Lecture 6 data structures and algorithms
Joins (A JOIN clause is used to combine rows from two or more tables, based o...
Joins in SQL
SQL JOINS- Reena P V
This is a presentation on the parsing.pptx
5.stack
3rd Quarter_Math 7 Reviewer 22024-25 .docx
Ad

More from VARSHAKUMARI49 (17)

PPT
28,29. procedures subprocedure,type checking functions in VBScript
PPT
30,31,32,33. decision and loop statements in vbscript
PPT
27. mathematical, date and time functions in VB Script
PPT
Cascading style sheet
PPT
Introduction to web technology
PPT
Database normalization
PPT
Sub queries
PPT
Introduction to sql
PPT
Vbscript
PPTX
Css module1
PPT
PPTX
Css mod1
PPT
Html mod1
PPT
Register counters.readonly
PPT
Sorting.ppt read only
PPT
28,29. procedures subprocedure,type checking functions in VBScript
30,31,32,33. decision and loop statements in vbscript
27. mathematical, date and time functions in VB Script
Cascading style sheet
Introduction to web technology
Database normalization
Sub queries
Introduction to sql
Vbscript
Css module1
Css mod1
Html mod1
Register counters.readonly
Sorting.ppt read only

Recently uploaded (20)

PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
additive manufacturing of ss316l using mig welding
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Internet of Things (IOT) - A guide to understanding
PPT
Mechanical Engineering MATERIALS Selection
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Artificial Intelligence
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
UNIT 4 Total Quality Management .pptx
DOCX
573137875-Attendance-Management-System-original
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PDF
PPT on Performance Review to get promotions
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Construction Project Organization Group 2.pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
additive manufacturing of ss316l using mig welding
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
Internet of Things (IOT) - A guide to understanding
Mechanical Engineering MATERIALS Selection
UNIT-1 - COAL BASED THERMAL POWER PLANTS
CH1 Production IntroductoryConcepts.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Artificial Intelligence
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Sustainable Sites - Green Building Construction
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
UNIT 4 Total Quality Management .pptx
573137875-Attendance-Management-System-original
Fundamentals of safety and accident prevention -final (1).pptx
PPT on Performance Review to get promotions
Model Code of Practice - Construction Work - 21102022 .pdf
Construction Project Organization Group 2.pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf

Joins

  • 2. content  Joins:  Cartesian Product  Natural Join  Equi Join  Self Join  Inner Join  outer Join(Left, Right, Full)
  • 3. JOINS  A JOIN combines together information from two or more tables into one result set.  The records from the table are fetched based on some values that are common to each.  It can also specifies some condition while retrieving the data.  Join = cross product + some condition
  • 4. Cartesian product  Before understanding JOINS in SQL we should know about Cartesian Product.  Also known as Cross Product.
  • 5. Cartesian Product in sets(Mathematics)  A = { 1, 2, 3} B = { a , b}  A x B = { (1,a) , (2,a), (3,a), (1, b), (2,b) , (3,b) }
  • 6. Cartesian product  R1 R2  R1 x R2 A B 2 E1 4 E2 5 E3 B C E1 5 E5 7 A B B C 2 E1 E1 5 4 E2 E1 5 5 E3 E1 5 .. .. .. ..
  • 7. Cartesian product  R1 R2  R1 x R2 A B 2 E1 4 E2 5 E3 B C E1 5 E5 7 A B B C 2 E1 E1 5 4 E2 E1 5 5 E3 E1 5 2 E1 E5 7 4 E2 E5 7 5 E3 E5 7
  • 8. Example : Consider two tables
  • 10. Cartesian product of two table QUERY: select * from emp2,dept;
  • 12. Cartesian Product :  Cartesian Product is the all possible combinations between applied table rows.  Suppose two tables are T1 and T2  T1 has r1 rows and c1 columns  T2 has r2 rows and c2 columns  Cartesian product of T1 and T2 will have:  r1 * r2 rows and c1 + c2 columns.
  • 13. Natural Join: Common columns in both the tables must have same name. After Natural Join :  Select * from R1, R2 where R1.B=R2.B; OR Select * from R1 natural join R2; = A B 2 E1 4 E2 5 E3 B C E1 5 E2 7 A B B C 2 E1 E1 5 4 E2 E1 5 5 E3 E1 5 2 E1 E2 7 4 E2 E2 7 5 E3 E2 7 A B C 2 E1 5 4 E2 7
  • 14. Natural JOIN: Select * from Emp2 natural join Dept; - Behaves Same as Inner Join - In Natural Join common attribute must have same name in both the tables
  • 15. Example:  Select emp_name, loc from Emp2 natural join Dept where loc= 'NEW YORK';
  • 16. Equi Join  R1 R2  After Equi Join :  Select *from R1,R2  where R1.B=R2.C;  or Select *from R1 join R2  on R1.B=R2.C; A B 2 E1 4 E2 5 E3 C D E1 5 E2 7 A B C D 2 E1 E1 5 4 E2 E1 5 5 E3 E1 5 2 E1 E2 7 4 E2 E2 7 5 E3 E2 7 A B C D 2 E1 E1 5 4 E2 E2 7
  • 17. Equi Join:  Same as inner join , it uses only equality comparison.  Query : Select * from Emp2, dept where emp2.deptno=dept.deptno;
  • 18. Self Join  A self Join is a type of Join which is used to join a table to itself. In this join both the columns belongs to the same table.  Syntax :  Select T1.column, T1.column from T1 A inner join T2 B on A.column= B.column
  • 19. Self Join  R1 After Equi Join : Select T.B from R1 T, R1 S where T.A=S.A and T.B <> S.B; A B 2 E1 2 E2 5 E3 A B A B 2 E1 2 E1 2 E2 2 E1 5 E3 2 E1 2 E1 2 E2 2 E2 2 E2 5 E3 2 E2 2 E1 5 E3 2 E2 5 E3 5 E3 5 E3 B E2 E1
  • 20. Select * from Emp2 A , Emp2 B where A.Deptno = B.Deptno;
  • 21. Inner Join  Inner Join combines the rows retrieved from multiple tables on the basis of common columns of the tables.  It takes that rows from the cartesian product table where the join elements ( emp.deptno and dept.deptno in the above query) matches fully  Syntax:  Select T1.column, T2.column from T1 inner join T2 on T1.column=T2.column
  • 22. Inner Join  R1 R2  After Inner Join = A B 2 E1 4 E2 5 E3 B C E1 5 E2 7 A B B C 2 E1 E1 5 4 E2 E1 5 5 E3 E1 5 2 E1 E2 7 4 E2 E2 7 5 E3 E2 7 A B B C 2 E1 E1 5 4 E2 E2 7
  • 23. Select * from Emp2 inner join Dept on Emp2.Deptno = Dept.Deptno
  • 24. example  Select * from Emp2 inner join Dept on Emp2.Deptno = Dept.Deptno where loc= 'NEW YORK';
  • 25. Example:  Select emp_name,loc from Emp2 inner join Dept on Emp2.Deptno = Dept.Deptno where loc= 'NEW YORK';
  • 26. Left Outer Join  Left outer Join takes that rows which are in inner join output.  And it also looks for the rows in the left table which are not in the inner join output.  The rows are added to the output with null in right columns.
  • 27. Left Join  R1 R2  After Left Join = A B 2 E1 4 E2 5 E3 B C E1 5 E2 7 A B B C 2 E1 E1 5 4 E2 E1 5 5 E3 E1 5 2 E1 E2 7 4 E2 E2 7 5 E3 E2 7 A B B C 2 E1 E1 5 4 E2 E2 7 5 E3 NUL L NUL L
  • 28. Syntax:  Select T1.column, T2.column from T1 left outer join T2 on T1.column=T2.column Eg. Select * from Emp2 left outer join Dept on Emp2.Deptno = Dept.Deptno;
  • 29. Right Outer Join  Right outer Join takes that rows which are in inner join output.  And it also looks for the rows in the right table which are not in the inner join output.  The rows are added to the output with null in left columns.
  • 30. Right Join  R1 R2  After right Join = A B 2 E1 4 E2 5 E3 B C E1 5 E5 7 A B B C 2 E1 E1 5 4 E2 E1 5 5 E3 E1 5 2 E1 E5 7 4 E2 E5 7 5 E3 E5 7 A B B C 2 E1 E1 5 NULL NULL E5 7
  • 31. Syntax:  Select T1.column, T2.column from T1 right outer join T2 on T1.column=T2.column Eg. Select * from Emp2 right outer join Dept on Emp2.Deptno = Dept.Deptno;
  • 32. Full Outer Join  Full outer Join takes that rows which are in inner join output.  And it also looks for the rows in the left and also right table which are not in the inner join output.
  • 33. Full Join  R1 R2  After Full Join = A B 2 E1 4 E2 5 E3 B C E1 5 E5 7 A B B C 2 E1 E1 5 4 E2 E1 5 5 E3 E1 5 2 E1 E5 7 4 E2 E5 7 5 E3 E5 7 A B B C 2 E1 E1 5 NULL NULL E5 7 4 E2 NULL NULL 5 E3 NULL NULL
  • 34. Syntax:  Select T1.column, T2.column from T1 full outer join T2 on T1.column=T2.column Eg. Select * from Emp2 full outer join Dept on Emp2.Deptno = Dept.Deptno;
  • 35. Consider two table Student Faculty S_id S_name course 1 Amit BCA 2 Ram Btech 3 Hardik BCA 4 Pawan BCA 5 Sumit Btech 6 Aman MCA 7 Hardik BCA F_Id course Loc F1 BCA AB 10 F2 Btech AB 1 F3 MTech AB1
  • 36.  Cartesian product:  Select * from Student, Faculty;  Natural Join:  Select * from Student natural join Faculty  Equi join:  Select * from student join Faculty on Student.course=Faculty.course  Self join:  Select A.S_name from Student A, Student B where A.S_id= B.S_id
  • 37.  Inner Join:  Select Student.S_name from Student inner join Faculty on Student.course= Faculty.course where Faculty.loc=‘AB10’  Left Join:  Select Student.S_name from Student left outer join Faculty on Student.course= Faculty.course where Faculty.loc=‘AB10’  Right Join:  Select Student.S_name from Student right outer join Faculty on Student.course= Faculty.course where Faculty.loc=‘AB10’