Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

SafeTraceHub Pvt.

Limited

SQL JOINS

Presented by
Venugopal
AGENDA
• Introduction to joins
• What are joins in SQL
• Types of joins
• Difference between sub queries and joins
• Analyzation of performance
• Code and explanation
• Practical session
• Joins with three tables
• Conclusion
Introduction to joins
• Joins in SQL which are used to combined rows from two or more tables,
based on related column between those two tables. They predominantly
used when a user is trying to extract data from tables, which have one-to-
many or many-to-many relationship between them.
• SELECT w.Worker_ID,w.FIRST_NAME,b.BONUS_AMOUNT,
b.WORKER_ID from Worker w JOIN Bonus b
• ON w.WORKER_ID=b.WORKER_ID;

• =
What are joins in SQL
• Mainly these can be performed • Cross join
• Self join
• Natural join
Types of joins
• Inner join • Outer join
• SELECT w.Worker_ID,w.FIRST_NAME,b.BONUS_AMOUNT,b.WORKER_ID • SELECT w.Worker_ID, w.FIRST_NAME, b.BONUS_AMOUNT, b.WORKER_ID
from Worker w inner JOIN Bonus b ON w.WORKER_ID=b.WORKER_ID; from Worker w full JOIN Bonus b ON w.WORKER_ID=b.WORKER_ID
Continues…
• Left join • Right join
• SELECT w.Worker_ID, w.FIRST_NAME, b.BONUS_AMOUNT, • SELECT w.Worker_ID, w.FIRST_NAME, b.BONUS_AMOUNT, b.WORKER_ID
b.WORKER_ID from Worker w left JOIN Bonus ON from Worker w right JOIN Bonus b ON w.WORKER_ID=b.WORKER_ID
w.WORKER_ID=b.WORKER_ID
Continues…
• Cartesian Product
Difference between natural and inner joins
Self join

• SELECT W.FIRST_NAME, w.SALARY, w.Worker_ID FROM Worker w join worker ww ON


w.WORKER_ID = ww.worker_id
Difference between sub query and join
There is no difference performance wise we can prove it practically. But some limitations we can
prove that also by practically.
We have two tables with 600 records
By sub query results

select * from tblAuthors t,tblBooks tt where t.Id IN(select id from


tblBooks where id<=500)
Continues…
• By Join results
• SELECT t.Author_name,t.country,t.Id,tt.Auhthor_id,tt.Edition,
tt.Id,tt.Price FROM tblAuthors t inner join tblBooks tt ON tt.id<=500
Loopholes in sub query
select * from tblAuthors t,tblBooks where not exists(select * from tblBooks tt
where tt.id=t.Id)
Join at performance
SELECT t.Author_name,t.country,t.Id
FROM tblAuthors t left join tblBooks tt ON tt.Id=t.Id where tt.id is null
Write An SQL Query To Print Details Of The Workers Who Are Also
Managers.

• select * from worker where WORKER_ID IN(select


WORKER_ref_ID from Title where WORKER_TITLE='Manager' )

• SELECT DISTINCT W.FIRST_NAME, T.WORKER_TITLE


FROM Worker w inner join Title t ON w.WORKER_ID =
t.WORKER_REF_ID and t.WORKER_TITLE in ('Manager');
Performance analyzation
Join with three table
• SELECT *
FROM worker w,bonus b,title t
where w.worker_Id = b.worker_Id
and b.worker_Id = t.Worker_ref_id

• SELECT *
FROM worker w
INNER JOIN bonus b ON w.worker_Id = b.worker_Id
INNER JOIN title t ON b.worker_Id = t.Worker_ref_id
Join with three table
Conclusion
• Readability
• Writability
• Performance
Thank you

You might also like