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)

joins in database
 joins in database joins in database
joins in database
 
Using the set operators
Using the set operatorsUsing the set operators
Using the set operators
 
SQL JOINS
SQL JOINSSQL JOINS
SQL JOINS
 
Mysql
MysqlMysql
Mysql
 
Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries Lecture
 
Mysql joins
Mysql joinsMysql joins
Mysql joins
 
Subqueries
SubqueriesSubqueries
Subqueries
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
 
DATABASE CONSTRAINTS
DATABASE CONSTRAINTSDATABASE CONSTRAINTS
DATABASE CONSTRAINTS
 
SQL Constraints
SQL ConstraintsSQL Constraints
SQL Constraints
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
Join
JoinJoin
Join
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
 
Sql Constraints
Sql ConstraintsSql Constraints
Sql Constraints
 
Nested queries in database
Nested queries in databaseNested queries in database
Nested queries in database
 
Index in sql server
Index in sql serverIndex in sql server
Index in sql server
 
Sql commands
Sql commandsSql commands
Sql commands
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
SQL Joins.pptx
SQL Joins.pptxSQL Joins.pptx
SQL Joins.pptx
 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
 

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

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxAmita Gupta
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 

Recently uploaded (20)

Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 

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