SlideShare a Scribd company logo
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

PPTX
Inner join and outer join
PPTX
Sql joins inner join self join outer joins
PPTX
SQL JOIN
PDF
SQL JOINS
PPT
Types Of Join In Sql Server - Join With Example In Sql Server
PDF
SQL window functions for MySQL
PDF
SQL Joins With Examples | Edureka
PPTX
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
Inner join and outer join
Sql joins inner join self join outer joins
SQL JOIN
SQL JOINS
Types Of Join In Sql Server - Join With Example In Sql Server
SQL window functions for MySQL
SQL Joins With Examples | Edureka
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery

What's hot (20)

PPTX
Joins And Its Types
PPT
SQL subquery
PPT
1.1 binary tree
PPT
Sql operators & functions 3
PPTX
Sql subquery
PPT
Joins in SQL
PPT
Aggregate functions
PPTX
MYSQL join
PPTX
DATABASE CONSTRAINTS
PPTX
Aggregate function
ODP
PPTX
PPT
SQL select clause
PPTX
SQL Joins.pptx
PPTX
Sql clauses by Manan Pasricha
PPTX
Sql commands
PDF
PPTX
introdution to SQL and SQL functions
PPTX
Sql operator
Joins And Its Types
SQL subquery
1.1 binary tree
Sql operators & functions 3
Sql subquery
Joins in SQL
Aggregate functions
MYSQL join
DATABASE CONSTRAINTS
Aggregate function
SQL select clause
SQL Joins.pptx
Sql clauses by Manan Pasricha
Sql commands
introdution to SQL and SQL functions
Sql operator
Ad

Viewers also liked (20)

PPT
Sub join a query optimization algorithm for flash-based database
PPTX
Database Join
PPT
Sql join
PPT
Sql joins
PPTX
MS Sql Server: Joining Databases
PPT
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
PPT
SQL Joinning.Database
PPTX
My sql join
PPTX
Relational Algebra,Types of join
PDF
Join(sql)
PPTX
Geek Sync | Rewriting Bad SQL Code 101
PDF
SQL JOIN Explained Visually
PPTX
Sql server JOIN
PPTX
Database Introduction - Join Query
PPT
Scrum Model
PDF
SQL 101 for business experts and stakeholders
DOCX
Complex queries in sql
PPTX
joins in database
PDF
Joins in databases
Sub join a query optimization algorithm for flash-based database
Database Join
Sql join
Sql joins
MS Sql Server: Joining Databases
Database Normalization 1NF, 2NF, 3NF, BCNF, 4NF, 5NF
SQL Joinning.Database
My sql join
Relational Algebra,Types of join
Join(sql)
Geek Sync | Rewriting Bad SQL Code 101
SQL JOIN Explained Visually
Sql server JOIN
Database Introduction - Join Query
Scrum Model
SQL 101 for business experts and stakeholders
Complex queries in sql
joins in database
Joins in databases
Ad

Similar to SQL Join Basic (20)

PPT
DOC
SQL Joins
PPTX
joins dbms.pptx
PPTX
SQL JOINS- Reena P V
PPTX
PPTX
Ch1_s2_QueryMultipleTablesByUsingJoins.pptx
PPTX
PRESENTATION........................pptx
PDF
Advance database system(part 8)
PPTX
Sql joins
PPTX
Joins (A JOIN clause is used to combine rows from two or more tables, based o...
PPTX
Querying_with_T-SQL_-_03 (1).pptx
PPTX
Querying_with_T-SQL_-_03.pptx
PPTX
SQL Tips Joins.pptx
PPTX
Structured Query Language
PDF
Sql joins
PPTX
20190310 - SQL Course - JOIN
PPTX
SQL_JOINS_Presentation, INNER, OUTER Joins
PPTX
join_SQL000000000000000000000000000000000.pptx
PDF
Tipos de Joins para consultas em banco de dados.pdf
SQL Joins
joins dbms.pptx
SQL JOINS- Reena P V
Ch1_s2_QueryMultipleTablesByUsingJoins.pptx
PRESENTATION........................pptx
Advance database system(part 8)
Sql joins
Joins (A JOIN clause is used to combine rows from two or more tables, based o...
Querying_with_T-SQL_-_03 (1).pptx
Querying_with_T-SQL_-_03.pptx
SQL Tips Joins.pptx
Structured Query Language
Sql joins
20190310 - SQL Course - JOIN
SQL_JOINS_Presentation, INNER, OUTER Joins
join_SQL000000000000000000000000000000000.pptx
Tipos de Joins para consultas em banco de dados.pdf

More from Naimul Arif (7)

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

Recently uploaded (20)

PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PDF
Basic Mud Logging Guide for educational purpose
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Introduction-to-Social-Work-by-Leonora-Serafeca-De-Guzman-Group-2.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PDF
Open folder Downloads.pdf yes yes ges yes
PDF
01-Introduction-to-Information-Management.pdf
O7-L3 Supply Chain Operations - ICLT Program
2.FourierTransform-ShortQuestionswithAnswers.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Cardiovascular Pharmacology for pharmacy students.pptx
Basic Mud Logging Guide for educational purpose
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pre independence Education in Inndia.pdf
Week 4 Term 3 Study Techniques revisited.pptx
Introduction-to-Social-Work-by-Leonora-Serafeca-De-Guzman-Group-2.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Insiders guide to clinical Medicine.pdf
Anesthesia in Laparoscopic Surgery in India
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
The Final Stretch: How to Release a Game and Not Die in the Process.
Open folder Downloads.pdf yes yes ges yes
01-Introduction-to-Information-Management.pdf

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