SlideShare a Scribd company logo
1 of 34
Join Operation in
Database
Prepared by: Naimul Arif
Software Engineer
Progoti Systems Ltd.
Simple Review
What is database???
A database is a collection of
information that is organized so that
it can easily be accessed, managed,
and updated.
Why database ???
• To store data
• To retrieve data
• To update data
• To merge data
Where we need database ???
- Every sector where data is handled.
Mostly used databases:
Usage of top 10 databases in August 2014
Types of SQL join operations
• INNER JOIN
• LEFT JOIN
• RIGHT JOIN
• OUTER JOIN
• LEFT JOIN EXCLUDING INNER JOIN
• RIGHT JOIN EXCLUDING INNER JOIN
• OUTER JOIN EXCLUDING INNER JOIN
Before knowing about
SQL join we should know
about Cartesian Product.
Also known as cross join
Cartesian product of tow or more table:
Consider two tables:
PID Pname
1 Shirt
2 Pajabi
3 Lungi
SID ProductID Price
101 1 1000
102 2 800
103 5 400
104 2 600
Product Sale
Cartesian product of tow or more table:
QUERY: Select * from Product, Sale;
PID Pname SID ProductID Price
1 Shirt 101 1 1000
1 Shirt 102 2 800
1 Shirt 103 5 400
1 Shirt 104 2 600
2 Pajabi 101 1 1000
2 Pajabi 102 2 800
2 Pajabi 103 5 400
2 Pajabi 104 2 600
3 Lungi 101 1 1000
3 Lungi 102 2 800
3 Lungi 103 5 400
3 Lungi 104 2 600
Suppose two tables T1 & T1
T1 has r1 rows and c1 columns
T2 has r2 rows and c2 columns
Cartesian product of T1 & T1 will have:
r1 * r2 rows and
c1 + c2 columns.
[for more that two tables r1 * r2 * r3 ...
and c1 + c2 + c3 ...]
Cartesian Product is the all possible
combinations between applied table rows.
PID Pname
1 Shirt
2 Pajabi
3 Lungi
SID ProductID Price
101 1 1000
102 2 800
103 5 400
104 2 600
Product Sale
For ease of learning these two tables
will be used for all examples
Inner join
Query: Select * from Product p Inner join Sale s on p.PID = s.ProductID
Inner join only takes
that rows from
Cartesian Product Table
where the join elements
(Product.PID and
Sale.ProductID in the
above query) matches
fully.
Inner join
Query: Select * from Product p Inner join Sale s on p.PID = s.ProductID
PID Pname SID ProductID Price
1 Shirt 101 1 1000
1 Shirt 102 2 800
1 Shirt 103 5 400
1 Shirt 104 2 600
2 Pajabi 101 1 1000
2 Pajabi 102 2 800
2 Pajabi 103 5 400
2 Pajabi 104 2 600
3 Lungi 101 1 1000
3 Lungi 102 2 800
3 Lungi 103 5 400
3 Lungi 104 2 600
Inner join
Query: Select * from Product p Inner join Sale s on p.PID = s.ProductID
PID Pname SID ProductID Price
1 Shirt 101 1 1000
2 Panjabi 102 2 800
2 Panjabi 104 2 600
So the output of the join table is shown below.
It is to mention that, if there exists more than one matching
element having same value then all possible combination will be
taken.
Left join
Query: Select * from Product p Left join Sale s on p.PID = s.ProductID
Left join takes that rows
which are in inner join output.
And it also looks for the rows
in left table which are not in
inner join output. The rows
are added to OUTPUT with
null in right columns.
Left join
Query: Select * from Product p Left join Sale s on p.PID = s.ProductID
PID Pname SID ProductID Price
1 Shirt 101 1 1000
1 Shirt 102 2 800
1 Shirt 103 5 400
1 Shirt 104 2 600
2 Pajabi 101 1 1000
2 Pajabi 102 2 800
2 Pajabi 103 5 400
2 Pajabi 104 2 600
3 Lungi 101 1 1000
3 Lungi 102 2 800
3 Lungi 103 5 400
3 Lungi 104 2 600
Left join
Query: Select * from Product p Left join Sale s on p.PID = s.ProductID
PID Pname SID ProductID Price
1 Shirt 101 1 1000
2 Panjabi 102 2 800
2 Panjabi 104 2 600
3 Lungi null null null
So the output of the join table is shown below.
In the left table Product |PID = 3 | Pname = Lungi|
row could not be joined with any row of Sale table. So it is
added with null value in right columns.
Right join
Query: Select * from Product p Right join Sale s on p.PID = s.ProductID
Right join takes that rows
which are in inner join output.
Also looks for the rows in
right table which are not in
inner join output. The rows
are added to OUTPUT with
null in left columns.
Right join
Query: Select * from Product p Right join Sale s on p.PID = s.ProductID
PID Pname SID ProductID Price
1 Shirt 101 1 1000
1 Shirt 102 2 800
1 Shirt 103 5 400
1 Shirt 104 2 600
2 Pajabi 101 1 1000
2 Pajabi 102 2 800
2 Pajabi 103 5 400
2 Pajabi 104 2 600
3 Lungi 101 1 1000
3 Lungi 102 2 800
3 Lungi 103 5 400
3 Lungi 104 2 600
Right join
Query: Select * from Product p Right join Sale s on p.PID = p.ProductID
PID Pname SID ProductID Price
1 Shirt 101 1 1000
2 Panjabi 102 2 800
2 Panjabi 104 2 600
null null 103 5 400
So the output of the join table is shown below.
In the right table Sale |SID = 103|ProductID = 5|Price =
400|row could not be joined with any row of Product table. So
it is added with null value in left columns.
Outer join
Query: Select * from Product p Outer join Sale s on p.PID = s.ProductID
Aside of inner join output
Outer join looks for the rows
in left table which are not in
inner join output. The rows
are added to OUTPUT with
null in right columns. Similarly
the rows from right table not
in inner join output are added
to OUTPUT with null values
in left columns.
Outer join
Query: Select * from Product p Outer join Sale s on p.PID = s.ProductID
PID Pname SID ProductID Price
1 Shirt 101 1 1000
1 Shirt 102 2 800
1 Shirt 103 5 400
1 Shirt 104 2 600
2 Pajabi 101 1 1000
2 Pajabi 102 2 800
2 Pajabi 103 5 400
2 Pajabi 104 2 600
3 Lungi 101 1 1000
3 Lungi 102 2 800
3 Lungi 103 5 400
3 Lungi 104 2 600
Outer join
Query: Select * from Product p Outer join Sale s on p.PID = s.ProductID
PID Pname SID ProductID Price
1 Shirt 101 1 1000
2 Panjabi 102 2 800
2 Panjabi 104 2 600
3 Lungi null null null
null null 103 5 400
In the 4th row there is not joinable row in right. So right values
are null. Similarly in 5th row there is no joinable row in left. So
left values are null.
Keep in mind
In case of Cartesian Product there is no matching, only
taking all combination.
In case of Join operation:
 Either matching of two column values are equal.
 Or one of them is null.
 In inner join no null is taken.
 In left join right side null is taken.
 In right join left side null is taken.
 In outer join null can be taken in any side but not in both
side at a time.
 In left join all rows of left table are in output table.
 In right join all rows of right table is taken.
 Number of output rows for join is less or equal to the
number of rows in Cartesian Product.
Left excluding join
Excluding join operations (Left
excluding join, right excluding join
and outer excluding join) only takes
rows which could not be joined. So
strictly one side of the output table
remains null.
Left excluding join operation is nothing but a Left join
operation with a fixed condition.
The condition is: right key should be null.
So eventually all right columns turn null.
Left excluding join = Left join - Inner join
Left excluding join
Query: Select * from Product p Left join Sale s on p.PID = s.ProductID
where s.ProductID is null
PID Pname
1 Shirt
2 Pajabi
3 Lungi
SID ProductID Price
101 1 1000
102 2 800
103 5 400
104 2 600
Product Sale
Here 3rd row of left table has no joinable row in right table.
So output is:
PID Pname SID ProductID Price
3 Lungi null null null
Right excluding join
This operation gives the rows
from right table who have no
joinable row in left table. So left
columns of this join output table
remains null.
Right excluding join operation is nothing but a Right join
operation with a fixed condition.
The condition is: left key should be null.
So eventually all left columns turn null.
Right excluding join = Right join - Inner join
Right excluding join
Query: Select * from Product p Right join Sale s on p.PID = s.ProductID where
p.PID is null
PID Pname
1 Shirt
2 Pajabi
3 Lungi
SID ProductID Price
101 1 1000
102 2 800
103 5 400
104 2 600
Product Sale
Here 3rd row of right table has no joinable row in left table.
So output is:
PID Pname SID ProductID Price
null null 103 5 400
Outer excluding join
- This operation outputs the rows from
left table with no joinable row in right
table. So right columns are given null.
- Also outputs rows from right table
having no joinable row in left. Left
columns are given null.
- Outer excluding join
= Left outer join + Right outer join
= Outer join - Inner join
Outer excluding join
Query: Select * from Product p Outer join Sale s on p.PID = s.ProductID
where Product.PID is or Sale.ProductID is null
PID Pname
1 Shirt
2 Pajabi
3 Lungi
SID ProductID Price
101 1 1000
102 2 800
103 5 400
104 2 600
Product Sale
PID Pname SID ProductID Price
null null 103 5 400
3 Lungi null null null
Lonely row from both table with null to opposite.
Use of join
Join helps to do DB operations keeping tables small and saving
memory. In short normalized database needs join operation.
Example
In facebook there may remain a lot of comments against a single
post. If we keep post and comment info in same table it will look
like this.
Post_
Creator
Post_Time Post_Text Post_ID Comment_
Creator
Comment_
Time
Comment_
Text
Comment_
ID
Sherlock 2014-01-23
00:00:00
I need a case 1001 Moriarty 2014-01-23
00:05:00
Miss me!!! 5001
Sherlock 2014-01-23
00:00:00
I need a case 1001 Joh Watson 2014-01-23
00:06:32
u r a
psychopath!
5002
Sherlock 2014-01-23
00:00:00
I need a case 1001 Sherlock 2014-01-23
00:06:35
nope, i am a
high
functioning
sociopath
5003
Sherlock 2014-01-23
00:00:00
I need a case 1001 Irene Adler 2014-01-23
00:12:01
Let's have
dinner
5004
Use of join
So in the table same post info are inserted a lot of time.
Waste of memory. More comments, more memory waste.
Instead we can maintain three tables, one for Posts, one for
Comments and one to connect them.
The architecture is shown below.
Post CommentHas
Post_Creator PID
CID
Post_Time
Post_Text Post_ID
Comment_
Creator
Comment_
Time
Comment_
Text
Comment_
ID
*
Post_
Creator
Post_Time Post_Text Post_ID
Sherlock 2014-01-23
00:00:00
I need a case 1001
Post_ID Comment_
ID
1001 5001
1001 5002
1001 5003
1001 5004
Comment_
Creator
Comment_
Time
Comment_
Text
Comment_
ID
Moriarty 2014-01-23
00:05:00
Miss me!!! 5001
Joh Watson 2014-01-23
00:06:32
u r a
psychopath!
5002
Sherlock 2014-01-23
00:06:35
nope, i am a
high
functioning
sociopath
5003
Irene Adler 2014-01-23
00:12:01
Let's have
dinner
5004
Post Comment
Has
So memory is optimized.
But what should I do if someone try to view
the post?
Use of join
Use of join
To fetch all comment against a post we need do the
query:
SELECT
p.Post_Creator, p.Post_Time,
p.Post_ID, p.Post_Text,
c.Comment_Creator, c.Comment_Time,
c.Comment_ID, c.Comment_Text
FROM
Post p
INNER JOIN
Has h ON p.Post_ID = h.Post_ID
INNER JOIN
Comment c ON c.Comment_ID = h.Comment_ID
ORDER BY c.Comment_Time
References:
• http://www.codeproject.com/Articles/33052/Vis
ual-Representation-of-SQL-Joins
• http://www.tutorialspoint.com/sql/sql-cartesian-
joins.htm

More Related Content

What's hot (20)

SQL Joins With Examples | Edureka
SQL Joins With Examples | EdurekaSQL Joins With Examples | Edureka
SQL Joins With Examples | Edureka
 
Join
JoinJoin
Join
 
SQL JOIN
SQL JOINSQL JOIN
SQL JOIN
 
SQL BUILT-IN FUNCTION
SQL BUILT-IN FUNCTIONSQL BUILT-IN FUNCTION
SQL BUILT-IN FUNCTION
 
Types of keys in dbms
Types of keys in dbmsTypes of keys in dbms
Types of keys in dbms
 
Joins in SQL
Joins in SQLJoins in SQL
Joins in SQL
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
 
Mysql joins
Mysql joinsMysql joins
Mysql joins
 
Quicksort Presentation
Quicksort PresentationQuicksort Presentation
Quicksort Presentation
 
MYSQL.ppt
MYSQL.pptMYSQL.ppt
MYSQL.ppt
 
Normalization in DBMS
Normalization in DBMSNormalization in DBMS
Normalization in DBMS
 
Entity Relationship Diagram
Entity Relationship DiagramEntity Relationship Diagram
Entity Relationship Diagram
 
Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries Lecture
 
serializability in dbms
serializability in dbmsserializability in dbms
serializability in dbms
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
 
Relational algebra ppt
Relational algebra pptRelational algebra ppt
Relational algebra ppt
 
MySql: Queries
MySql: QueriesMySql: Queries
MySql: Queries
 
Types Of Keys in DBMS
Types Of Keys in DBMSTypes Of Keys in DBMS
Types Of Keys in DBMS
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
 
DBMS Keys
DBMS KeysDBMS Keys
DBMS Keys
 

Viewers also liked

Sub join a query optimization algorithm for flash-based database
Sub join a query optimization algorithm for flash-based databaseSub join a query optimization algorithm for flash-based database
Sub join a query optimization algorithm for flash-based databaseZhichao Liang
 
MS Sql Server: Joining Databases
MS Sql Server: Joining DatabasesMS Sql Server: Joining Databases
MS Sql Server: Joining DatabasesDataminingTools Inc
 
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NFDatabase Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NFOum Saokosal
 
SQL Joinning.Database
SQL Joinning.DatabaseSQL Joinning.Database
SQL Joinning.DatabaseUmme habiba
 
Relational Algebra,Types of join
Relational Algebra,Types of joinRelational Algebra,Types of join
Relational Algebra,Types of joinraj upadhyay
 
Join(sql)
Join(sql)Join(sql)
Join(sql)인 이
 
Geek Sync | Rewriting Bad SQL Code 101
Geek Sync | Rewriting Bad SQL Code 101Geek Sync | Rewriting Bad SQL Code 101
Geek Sync | Rewriting Bad SQL Code 101IDERA Software
 
Sql server JOIN
Sql server JOINSql server JOIN
Sql server JOINRiteshkiit
 
Database Introduction - Join Query
Database Introduction - Join QueryDatabase Introduction - Join Query
Database Introduction - Join QueryDudy Ali
 
SQL 101 for business experts and stakeholders
SQL 101 for business experts and stakeholdersSQL 101 for business experts and stakeholders
SQL 101 for business experts and stakeholdersIván Stepaniuk
 
Complex queries in sql
Complex queries in sqlComplex queries in sql
Complex queries in sqlCharan Reddy
 
Joins in databases
Joins in databases Joins in databases
Joins in databases CourseHunt
 
Database - Normalization
Database - NormalizationDatabase - Normalization
Database - NormalizationMudasir Qazi
 

Viewers also liked (20)

Sub join a query optimization algorithm for flash-based database
Sub join a query optimization algorithm for flash-based databaseSub join a query optimization algorithm for flash-based database
Sub join a query optimization algorithm for flash-based database
 
Database Join
Database JoinDatabase Join
Database Join
 
Sql join
Sql  joinSql  join
Sql join
 
SQL
SQLSQL
SQL
 
Sql joins
Sql joinsSql joins
Sql joins
 
MS Sql Server: Joining Databases
MS Sql Server: Joining DatabasesMS Sql Server: Joining Databases
MS Sql Server: Joining Databases
 
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NFDatabase Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
 
SQL Joinning.Database
SQL Joinning.DatabaseSQL Joinning.Database
SQL Joinning.Database
 
My sql join
My sql joinMy sql join
My sql join
 
Relational Algebra,Types of join
Relational Algebra,Types of joinRelational Algebra,Types of join
Relational Algebra,Types of join
 
Join(sql)
Join(sql)Join(sql)
Join(sql)
 
Geek Sync | Rewriting Bad SQL Code 101
Geek Sync | Rewriting Bad SQL Code 101Geek Sync | Rewriting Bad SQL Code 101
Geek Sync | Rewriting Bad SQL Code 101
 
SQL JOIN Explained Visually
SQL JOIN Explained VisuallySQL JOIN Explained Visually
SQL JOIN Explained Visually
 
Sql server JOIN
Sql server JOINSql server JOIN
Sql server JOIN
 
Database Introduction - Join Query
Database Introduction - Join QueryDatabase Introduction - Join Query
Database Introduction - Join Query
 
Scrum Model
Scrum ModelScrum Model
Scrum Model
 
SQL 101 for business experts and stakeholders
SQL 101 for business experts and stakeholdersSQL 101 for business experts and stakeholders
SQL 101 for business experts and stakeholders
 
Complex queries in sql
Complex queries in sqlComplex queries in sql
Complex queries in sql
 
Joins in databases
Joins in databases Joins in databases
Joins in databases
 
Database - Normalization
Database - NormalizationDatabase - Normalization
Database - Normalization
 

More from Naimul Arif

Xiaomi Marketing Strategy: Bangladesh Vs Other Countries
Xiaomi Marketing Strategy: Bangladesh Vs Other CountriesXiaomi Marketing Strategy: Bangladesh Vs Other Countries
Xiaomi Marketing Strategy: Bangladesh Vs Other CountriesNaimul Arif
 
DEMAND SIDE MANAGEMENT OF ELECTRIC- ITY FOR CONTROLLING PEAK DEMANDS IN BANGL...
DEMAND SIDE MANAGEMENT OF ELECTRIC- ITY FOR CONTROLLING PEAK DEMANDS IN BANGL...DEMAND SIDE MANAGEMENT OF ELECTRIC- ITY FOR CONTROLLING PEAK DEMANDS IN BANGL...
DEMAND SIDE MANAGEMENT OF ELECTRIC- ITY FOR CONTROLLING PEAK DEMANDS IN BANGL...Naimul Arif
 
Why business should not be involved in CSR
Why business should not be involved in CSRWhy business should not be involved in CSR
Why business should not be involved in CSRNaimul Arif
 
Basic Probability and statistics in Bangla
Basic Probability and statistics in BanglaBasic Probability and statistics in Bangla
Basic Probability and statistics in BanglaNaimul Arif
 
Class, Collaboration, Sequence Diagram of a sample project
Class, Collaboration, Sequence Diagram of a sample projectClass, Collaboration, Sequence Diagram of a sample project
Class, Collaboration, Sequence Diagram of a sample projectNaimul Arif
 
Entity relationship Diagram for Online buy and Sale Project
Entity relationship Diagram for Online buy and Sale ProjectEntity relationship Diagram for Online buy and Sale Project
Entity relationship Diagram for Online buy and Sale ProjectNaimul Arif
 
Mystery of stars
Mystery of starsMystery of stars
Mystery of starsNaimul Arif
 

More from Naimul Arif (7)

Xiaomi Marketing Strategy: Bangladesh Vs Other Countries
Xiaomi Marketing Strategy: Bangladesh Vs Other CountriesXiaomi Marketing Strategy: Bangladesh Vs Other Countries
Xiaomi Marketing Strategy: Bangladesh Vs Other Countries
 
DEMAND SIDE MANAGEMENT OF ELECTRIC- ITY FOR CONTROLLING PEAK DEMANDS IN BANGL...
DEMAND SIDE MANAGEMENT OF ELECTRIC- ITY FOR CONTROLLING PEAK DEMANDS IN BANGL...DEMAND SIDE MANAGEMENT OF ELECTRIC- ITY FOR CONTROLLING PEAK DEMANDS IN BANGL...
DEMAND SIDE MANAGEMENT OF ELECTRIC- ITY FOR CONTROLLING PEAK DEMANDS IN BANGL...
 
Why business should not be involved in CSR
Why business should not be involved in CSRWhy business should not be involved in CSR
Why business should not be involved in CSR
 
Basic Probability and statistics in Bangla
Basic Probability and statistics in BanglaBasic Probability and statistics in Bangla
Basic Probability and statistics in Bangla
 
Class, Collaboration, Sequence Diagram of a sample project
Class, Collaboration, Sequence Diagram of a sample projectClass, Collaboration, Sequence Diagram of a sample project
Class, Collaboration, Sequence Diagram of a sample project
 
Entity relationship Diagram for Online buy and Sale Project
Entity relationship Diagram for Online buy and Sale ProjectEntity relationship Diagram for Online buy and Sale Project
Entity relationship Diagram for Online buy and Sale Project
 
Mystery of stars
Mystery of starsMystery of stars
Mystery of stars
 

Recently uploaded

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 

Recently uploaded (20)

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 

SQL Join Basic

  • 1. Join Operation in Database Prepared by: Naimul Arif Software Engineer Progoti Systems Ltd.
  • 2. Simple Review What is database??? A database is a collection of information that is organized so that it can easily be accessed, managed, and updated.
  • 3. Why database ??? • To store data • To retrieve data • To update data • To merge data Where we need database ??? - Every sector where data is handled.
  • 4. Mostly used databases: Usage of top 10 databases in August 2014
  • 5. Types of SQL join operations • INNER JOIN • LEFT JOIN • RIGHT JOIN • OUTER JOIN • LEFT JOIN EXCLUDING INNER JOIN • RIGHT JOIN EXCLUDING INNER JOIN • OUTER JOIN EXCLUDING INNER JOIN
  • 6. Before knowing about SQL join we should know about Cartesian Product. Also known as cross join
  • 7. Cartesian product of tow or more table: Consider two tables: PID Pname 1 Shirt 2 Pajabi 3 Lungi SID ProductID Price 101 1 1000 102 2 800 103 5 400 104 2 600 Product Sale
  • 8. Cartesian product of tow or more table: QUERY: Select * from Product, Sale; PID Pname SID ProductID Price 1 Shirt 101 1 1000 1 Shirt 102 2 800 1 Shirt 103 5 400 1 Shirt 104 2 600 2 Pajabi 101 1 1000 2 Pajabi 102 2 800 2 Pajabi 103 5 400 2 Pajabi 104 2 600 3 Lungi 101 1 1000 3 Lungi 102 2 800 3 Lungi 103 5 400 3 Lungi 104 2 600
  • 9. Suppose two tables T1 & T1 T1 has r1 rows and c1 columns T2 has r2 rows and c2 columns Cartesian product of T1 & T1 will have: r1 * r2 rows and c1 + c2 columns. [for more that two tables r1 * r2 * r3 ... and c1 + c2 + c3 ...] Cartesian Product is the all possible combinations between applied table rows.
  • 10. PID Pname 1 Shirt 2 Pajabi 3 Lungi SID ProductID Price 101 1 1000 102 2 800 103 5 400 104 2 600 Product Sale For ease of learning these two tables will be used for all examples
  • 11. Inner join Query: Select * from Product p Inner join Sale s on p.PID = s.ProductID Inner join only takes that rows from Cartesian Product Table where the join elements (Product.PID and Sale.ProductID in the above query) matches fully.
  • 12. Inner join Query: Select * from Product p Inner join Sale s on p.PID = s.ProductID PID Pname SID ProductID Price 1 Shirt 101 1 1000 1 Shirt 102 2 800 1 Shirt 103 5 400 1 Shirt 104 2 600 2 Pajabi 101 1 1000 2 Pajabi 102 2 800 2 Pajabi 103 5 400 2 Pajabi 104 2 600 3 Lungi 101 1 1000 3 Lungi 102 2 800 3 Lungi 103 5 400 3 Lungi 104 2 600
  • 13. Inner join Query: Select * from Product p Inner join Sale s on p.PID = s.ProductID PID Pname SID ProductID Price 1 Shirt 101 1 1000 2 Panjabi 102 2 800 2 Panjabi 104 2 600 So the output of the join table is shown below. It is to mention that, if there exists more than one matching element having same value then all possible combination will be taken.
  • 14. Left join Query: Select * from Product p Left join Sale s on p.PID = s.ProductID Left join takes that rows which are in inner join output. And it also looks for the rows in left table which are not in inner join output. The rows are added to OUTPUT with null in right columns.
  • 15. Left join Query: Select * from Product p Left join Sale s on p.PID = s.ProductID PID Pname SID ProductID Price 1 Shirt 101 1 1000 1 Shirt 102 2 800 1 Shirt 103 5 400 1 Shirt 104 2 600 2 Pajabi 101 1 1000 2 Pajabi 102 2 800 2 Pajabi 103 5 400 2 Pajabi 104 2 600 3 Lungi 101 1 1000 3 Lungi 102 2 800 3 Lungi 103 5 400 3 Lungi 104 2 600
  • 16. Left join Query: Select * from Product p Left join Sale s on p.PID = s.ProductID PID Pname SID ProductID Price 1 Shirt 101 1 1000 2 Panjabi 102 2 800 2 Panjabi 104 2 600 3 Lungi null null null So the output of the join table is shown below. In the left table Product |PID = 3 | Pname = Lungi| row could not be joined with any row of Sale table. So it is added with null value in right columns.
  • 17. Right join Query: Select * from Product p Right join Sale s on p.PID = s.ProductID Right join takes that rows which are in inner join output. Also looks for the rows in right table which are not in inner join output. The rows are added to OUTPUT with null in left columns.
  • 18. Right join Query: Select * from Product p Right join Sale s on p.PID = s.ProductID PID Pname SID ProductID Price 1 Shirt 101 1 1000 1 Shirt 102 2 800 1 Shirt 103 5 400 1 Shirt 104 2 600 2 Pajabi 101 1 1000 2 Pajabi 102 2 800 2 Pajabi 103 5 400 2 Pajabi 104 2 600 3 Lungi 101 1 1000 3 Lungi 102 2 800 3 Lungi 103 5 400 3 Lungi 104 2 600
  • 19. Right join Query: Select * from Product p Right join Sale s on p.PID = p.ProductID PID Pname SID ProductID Price 1 Shirt 101 1 1000 2 Panjabi 102 2 800 2 Panjabi 104 2 600 null null 103 5 400 So the output of the join table is shown below. In the right table Sale |SID = 103|ProductID = 5|Price = 400|row could not be joined with any row of Product table. So it is added with null value in left columns.
  • 20. Outer join Query: Select * from Product p Outer join Sale s on p.PID = s.ProductID Aside of inner join output Outer join looks for the rows in left table which are not in inner join output. The rows are added to OUTPUT with null in right columns. Similarly the rows from right table not in inner join output are added to OUTPUT with null values in left columns.
  • 21. Outer join Query: Select * from Product p Outer join Sale s on p.PID = s.ProductID PID Pname SID ProductID Price 1 Shirt 101 1 1000 1 Shirt 102 2 800 1 Shirt 103 5 400 1 Shirt 104 2 600 2 Pajabi 101 1 1000 2 Pajabi 102 2 800 2 Pajabi 103 5 400 2 Pajabi 104 2 600 3 Lungi 101 1 1000 3 Lungi 102 2 800 3 Lungi 103 5 400 3 Lungi 104 2 600
  • 22. Outer join Query: Select * from Product p Outer join Sale s on p.PID = s.ProductID PID Pname SID ProductID Price 1 Shirt 101 1 1000 2 Panjabi 102 2 800 2 Panjabi 104 2 600 3 Lungi null null null null null 103 5 400 In the 4th row there is not joinable row in right. So right values are null. Similarly in 5th row there is no joinable row in left. So left values are null.
  • 23. Keep in mind In case of Cartesian Product there is no matching, only taking all combination. In case of Join operation:  Either matching of two column values are equal.  Or one of them is null.  In inner join no null is taken.  In left join right side null is taken.  In right join left side null is taken.  In outer join null can be taken in any side but not in both side at a time.  In left join all rows of left table are in output table.  In right join all rows of right table is taken.  Number of output rows for join is less or equal to the number of rows in Cartesian Product.
  • 24. Left excluding join Excluding join operations (Left excluding join, right excluding join and outer excluding join) only takes rows which could not be joined. So strictly one side of the output table remains null. Left excluding join operation is nothing but a Left join operation with a fixed condition. The condition is: right key should be null. So eventually all right columns turn null. Left excluding join = Left join - Inner join
  • 25. Left excluding join Query: Select * from Product p Left join Sale s on p.PID = s.ProductID where s.ProductID is null PID Pname 1 Shirt 2 Pajabi 3 Lungi SID ProductID Price 101 1 1000 102 2 800 103 5 400 104 2 600 Product Sale Here 3rd row of left table has no joinable row in right table. So output is: PID Pname SID ProductID Price 3 Lungi null null null
  • 26. Right excluding join This operation gives the rows from right table who have no joinable row in left table. So left columns of this join output table remains null. Right excluding join operation is nothing but a Right join operation with a fixed condition. The condition is: left key should be null. So eventually all left columns turn null. Right excluding join = Right join - Inner join
  • 27. Right excluding join Query: Select * from Product p Right join Sale s on p.PID = s.ProductID where p.PID is null PID Pname 1 Shirt 2 Pajabi 3 Lungi SID ProductID Price 101 1 1000 102 2 800 103 5 400 104 2 600 Product Sale Here 3rd row of right table has no joinable row in left table. So output is: PID Pname SID ProductID Price null null 103 5 400
  • 28. Outer excluding join - This operation outputs the rows from left table with no joinable row in right table. So right columns are given null. - Also outputs rows from right table having no joinable row in left. Left columns are given null. - Outer excluding join = Left outer join + Right outer join = Outer join - Inner join
  • 29. Outer excluding join Query: Select * from Product p Outer join Sale s on p.PID = s.ProductID where Product.PID is or Sale.ProductID is null PID Pname 1 Shirt 2 Pajabi 3 Lungi SID ProductID Price 101 1 1000 102 2 800 103 5 400 104 2 600 Product Sale PID Pname SID ProductID Price null null 103 5 400 3 Lungi null null null Lonely row from both table with null to opposite.
  • 30. Use of join Join helps to do DB operations keeping tables small and saving memory. In short normalized database needs join operation. Example In facebook there may remain a lot of comments against a single post. If we keep post and comment info in same table it will look like this. Post_ Creator Post_Time Post_Text Post_ID Comment_ Creator Comment_ Time Comment_ Text Comment_ ID Sherlock 2014-01-23 00:00:00 I need a case 1001 Moriarty 2014-01-23 00:05:00 Miss me!!! 5001 Sherlock 2014-01-23 00:00:00 I need a case 1001 Joh Watson 2014-01-23 00:06:32 u r a psychopath! 5002 Sherlock 2014-01-23 00:00:00 I need a case 1001 Sherlock 2014-01-23 00:06:35 nope, i am a high functioning sociopath 5003 Sherlock 2014-01-23 00:00:00 I need a case 1001 Irene Adler 2014-01-23 00:12:01 Let's have dinner 5004
  • 31. Use of join So in the table same post info are inserted a lot of time. Waste of memory. More comments, more memory waste. Instead we can maintain three tables, one for Posts, one for Comments and one to connect them. The architecture is shown below. Post CommentHas Post_Creator PID CID Post_Time Post_Text Post_ID Comment_ Creator Comment_ Time Comment_ Text Comment_ ID *
  • 32. Post_ Creator Post_Time Post_Text Post_ID Sherlock 2014-01-23 00:00:00 I need a case 1001 Post_ID Comment_ ID 1001 5001 1001 5002 1001 5003 1001 5004 Comment_ Creator Comment_ Time Comment_ Text Comment_ ID Moriarty 2014-01-23 00:05:00 Miss me!!! 5001 Joh Watson 2014-01-23 00:06:32 u r a psychopath! 5002 Sherlock 2014-01-23 00:06:35 nope, i am a high functioning sociopath 5003 Irene Adler 2014-01-23 00:12:01 Let's have dinner 5004 Post Comment Has So memory is optimized. But what should I do if someone try to view the post? Use of join
  • 33. Use of join To fetch all comment against a post we need do the query: SELECT p.Post_Creator, p.Post_Time, p.Post_ID, p.Post_Text, c.Comment_Creator, c.Comment_Time, c.Comment_ID, c.Comment_Text FROM Post p INNER JOIN Has h ON p.Post_ID = h.Post_ID INNER JOIN Comment c ON c.Comment_ID = h.Comment_ID ORDER BY c.Comment_Time