SlideShare a Scribd company logo
1 of 16
Deepthi Rachumallu
SQL Joins
SQL joins are used to get data from two
or more tables bases on relationship
between some of the columns in tables.
In most of the cases we will use primary
key of first table and foreign key of
secondary table to get data from tables
Deepthi Rachumallu
Types of Joins
Deepthi Rachumallu
Tables for examples
EMPNO ENAME JOB MGR DEPTNO
111 Ramesh Analyst 444 10
222 Khilan Clerk 333 20
333 Kaushik Manager 111 10
444 Chaitali engineer 222 40
DEPTNO DNAME LOC
10 INVENTORY HYDERABAD
20 FINANCE BANGALORE
30 HR MUMBAI
Deepthi Rachumallu
INNER JOIN OR EQUIJOIN
clause is used to combine rows from two or more tables,
based on a common field between them.
INNER JOIN creates a new result table by combining
column values of two tables (table1 and table2) based
upon the join-condition.
Keyword used is INNER JOIN or simply JOIN.
Deepthi Rachumallu
INNER JOIN OR EQUIJOIN
Syntax SELECT table1.column1, table2.column2...
FROM table1 INNER JOIN table2 ON
table1.common_field = table2.common_field;
SELECT emp.ename,emp.job,dept.dname,dept.loc from EMPLOYYE emp
INNER JOIN DEPARTMENT dept ON emp.deptno = dept.deptno;
Deepthi Rachumallu
Result……
ENAME JOB DNAME LOC
Ramesh Analyst INVENTORY HYDERABAD
Khilan Clerk FINANCE BANGALORE
Kaushik Manager INVENTORY HYDERABAD
Deepthi Rachumallu
Left Join/Left Outer Join
LEFT JOIN returns all rows from the left table (table1),
even if there are no matches in the right table(table2).
If there are no matches found in right table the result is
null in the right table.
Keyword used is LEFT JOIN.
Deepthi Rachumallu
Left Join Continuation…
Syntax  SELECT table1.column1, table2.column2...
FROM table1
LEFT JOIN table2 ON table1.common_field =
table2.common_field;
SELECT emp.ename,emp.job,dept.dname,dept.loc from EMPLOYYE emp
LEFT JOIN DEPARTMENT dept ON emp.deptno = dept.deptno;
OR
SELECT emp.ename,emp.job,dept.dname,dept.loc from EMPLOYYE emp ,
DEPARTMENT dept WHERE emp.deptno(+) = dept.deptno;
ENAME JOB DNAME LOC
Ramesh Analyst INVENTORY HYDERABAD
Khilan Clerk FINANCE BANGALORE
Kaushik Manager INVENTORY HYDERABAD
Chaitali engineer NULL NULL
Deepthi Rachumallu
Right Join/Right Outer Join
RIGHT JOIN returns all rows from the right table (table2),
even if there are no matches in the left table(table1).
If there are no matched found in let table the result is null
in the left table.
Keyword used is RIGHT JOIN.
Deepthi Rachumallu
Right Join Continuation…
Syntax  SELECT table1.column1, table2.column2...
FROM table1
RIGHT JOIN table2 ON table1.common_field =
table2.common_field;
SELECT emp.ename,emp.job,dept.dname,dept.loc from EMPLOYYE emp
RIGHT JOIN DEPARTMENT dept ON emp.deptno = dept.deptno;
OR
SELECT emp.ename,emp.job,dept.dname,dept.loc from EMPLOYYE emp,
DEPARTMENT dept WHERE emp.deptno = dept.deptno(+);
ENAME JOB DNAME LOC
Ramesh Analyst INVENTORY HYDERABAD
Khilan Clerk FINANCE BANGALORE
Kaushik Manager INVENTORY HYDERABAD
NULL NULL HR MUMBAI
Deepthi Rachumallu
Full Join
The SQL FULL JOIN combines the results of both left and
right outer joins.
The joined table will contain all records from both tables,
and fill in NULLs for missing matches on either side.
Keyword used is FULL JOIN. In some data bases it is also
known as FULL OUTER JOIN.
Deepthi Rachumallu
Full Join Continuation…
Syntax  SELECT table1.column1, table2.column2...
FROM table1
FULL JOIN table2 ON table1.common_field =
table2.common_field;
SELECT emp.ename,emp.job,dept.dname,dept.loc from EMPLOYYE emp
FULL JOIN DEPARTMENT dept ON emp.deptno = dept.deptno;
ENAME JOB DNAME LOC
Ramesh Analyst INVENTORY HYDERABAD
Khilan Clerk FINANCE BANGALORE
Kaushik Manager INVENTORY HYDERABAD
NULL NULL HR MUMBAI
Chaitali engineer NULL NULL
Deepthi Rachumallu
Self Join
SELF JOIN is used to join a table to itself as if the table
were two tables
Syntax  SELECT a.column_name, b.column_name... FROM
table1 a, table1 b WHERE a.common_field = b.common_field;
Deepthi Rachumallu
Self Join Continuation….
SELECT emp1.ename, emp1.job, emp2.ename as manager
FROM EMPLOYEE emp1, EMPLOYEE emp2 where emp1.mgr =
emp2.empno;
ENAME JOB MANAGER
Ramesh Analyst Chaitali
Khilan Clerk Kaushik
Kaushik Manager Ramesh
Chaitali engineer Khilan
Deepthi Rachumallu
Deepthi Rachumallu

More Related Content

What's hot

What's hot (20)

joins in database
 joins in database joins in database
joins in database
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
 
Sql Constraints
Sql ConstraintsSql Constraints
Sql Constraints
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
Relational Algebra,Types of join
Relational Algebra,Types of joinRelational Algebra,Types of join
Relational Algebra,Types of join
 
Aggregate functions in SQL.pptx
Aggregate functions in SQL.pptxAggregate functions in SQL.pptx
Aggregate functions in SQL.pptx
 
SQL Joins and Query Optimization
SQL Joins and Query OptimizationSQL Joins and Query Optimization
SQL Joins and Query Optimization
 
Types of keys dbms
Types of keys dbmsTypes of keys dbms
Types of keys dbms
 
Group By, Order By, and Aliases in SQL
Group By, Order By, and Aliases in SQLGroup By, Order By, and Aliases in SQL
Group By, Order By, and Aliases in SQL
 
MySQL JOINS
MySQL JOINSMySQL JOINS
MySQL JOINS
 
Sql(structured query language)
Sql(structured query language)Sql(structured query language)
Sql(structured query language)
 
Sql commands
Sql commandsSql commands
Sql commands
 
Sql clauses by Manan Pasricha
Sql clauses by Manan PasrichaSql clauses by Manan Pasricha
Sql clauses by Manan Pasricha
 
Database Triggers
Database TriggersDatabase Triggers
Database Triggers
 
set operators.pptx
set operators.pptxset operators.pptx
set operators.pptx
 
Joins in SQL
Joins in SQLJoins in SQL
Joins in SQL
 
Aggregate function
Aggregate functionAggregate function
Aggregate function
 
Types of keys in database | SQL
Types of keys in database | SQLTypes of keys in database | SQL
Types of keys in database | SQL
 
SQL select clause
SQL select clauseSQL select clause
SQL select clause
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
 

Viewers also liked

SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
Vikas Gupta
 
Data definition language (ddl)
Data definition language (ddl)Data definition language (ddl)
Data definition language (ddl)
Dex Winadha
 

Viewers also liked (20)

SQL JOIN
SQL JOINSQL JOIN
SQL JOIN
 
SQL Joins
SQL JoinsSQL Joins
SQL Joins
 
Semi join
Semi joinSemi join
Semi join
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 
Sql server select queries ppt 18
Sql server select queries ppt 18Sql server select queries ppt 18
Sql server select queries ppt 18
 
Datastage Introduction To Data Warehousing
Datastage Introduction To Data WarehousingDatastage Introduction To Data Warehousing
Datastage Introduction To Data Warehousing
 
MySQL JOIN & UNION
MySQL JOIN & UNIONMySQL JOIN & UNION
MySQL JOIN & UNION
 
SQL DDL
SQL DDLSQL DDL
SQL DDL
 
HRIS
HRISHRIS
HRIS
 
Data definition language (ddl)
Data definition language (ddl)Data definition language (ddl)
Data definition language (ddl)
 
History of C/C++ Language
History of C/C++ LanguageHistory of C/C++ Language
History of C/C++ Language
 
Capturing Data Requirements
Capturing Data RequirementsCapturing Data Requirements
Capturing Data Requirements
 
C++ history session 00 history
C++ history session 00   historyC++ history session 00   history
C++ history session 00 history
 
C/C++ History in few slides
C/C++ History in few slides C/C++ History in few slides
C/C++ History in few slides
 
History of c++
History of c++ History of c++
History of c++
 
Oracle: Joins
Oracle: JoinsOracle: Joins
Oracle: Joins
 
153 Oracle dba interview questions
153 Oracle dba interview questions153 Oracle dba interview questions
153 Oracle dba interview questions
 
C++ language
C++ languageC++ language
C++ language
 
Oracle Complete Interview Questions
Oracle Complete Interview QuestionsOracle Complete Interview Questions
Oracle Complete Interview Questions
 
SQL Outer Joins for Fun and Profit
SQL Outer Joins for Fun and ProfitSQL Outer Joins for Fun and Profit
SQL Outer Joins for Fun and Profit
 

Similar to Sql joins inner join self join outer joins

Similar to Sql joins inner join self join outer joins (20)

types of SQL Joins
 types of SQL Joins types of SQL Joins
types of SQL Joins
 
joins and subqueries in big data analysis
joins and subqueries in big data analysisjoins and subqueries in big data analysis
joins and subqueries in big data analysis
 
V19 join method-c
V19 join method-cV19 join method-c
V19 join method-c
 
Joins in SQL
Joins in SQLJoins in SQL
Joins in SQL
 
MS SQLSERVER:Joining Databases
MS SQLSERVER:Joining DatabasesMS SQLSERVER:Joining Databases
MS SQLSERVER:Joining Databases
 
MS Sql Server: Joining Databases
MS Sql Server: Joining DatabasesMS Sql Server: Joining Databases
MS Sql Server: Joining Databases
 
MS SQL SERVER: Joining Databases
MS SQL SERVER: Joining DatabasesMS SQL SERVER: Joining Databases
MS SQL SERVER: Joining Databases
 
JOINS.pptx
JOINS.pptxJOINS.pptx
JOINS.pptx
 
Join in SQL - Inner, Self, Outer Join
Join in SQL - Inner, Self, Outer JoinJoin in SQL - Inner, Self, Outer Join
Join in SQL - Inner, Self, Outer Join
 
Lab4 join - all types listed
Lab4   join - all types listedLab4   join - all types listed
Lab4 join - all types listed
 
Assignment 4
Assignment 4Assignment 4
Assignment 4
 
Sql joins
Sql joinsSql joins
Sql joins
 
database .pptx
database .pptxdatabase .pptx
database .pptx
 
Les04
Les04Les04
Les04
 
Joins
JoinsJoins
Joins
 
Day-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptxDay-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptx
 
Presentation of Joins In Database
Presentation of Joins In DatabasePresentation of Joins In Database
Presentation of Joins In Database
 
Oracle: Joins
Oracle: JoinsOracle: Joins
Oracle: Joins
 
App C
App CApp C
App C
 
Joining
JoiningJoining
Joining
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
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
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Recently uploaded (20)

Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
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
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
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
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
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
 
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
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
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.
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 

Sql joins inner join self join outer joins

  • 2. SQL Joins SQL joins are used to get data from two or more tables bases on relationship between some of the columns in tables. In most of the cases we will use primary key of first table and foreign key of secondary table to get data from tables Deepthi Rachumallu
  • 4. Tables for examples EMPNO ENAME JOB MGR DEPTNO 111 Ramesh Analyst 444 10 222 Khilan Clerk 333 20 333 Kaushik Manager 111 10 444 Chaitali engineer 222 40 DEPTNO DNAME LOC 10 INVENTORY HYDERABAD 20 FINANCE BANGALORE 30 HR MUMBAI Deepthi Rachumallu
  • 5. INNER JOIN OR EQUIJOIN clause is used to combine rows from two or more tables, based on a common field between them. INNER JOIN creates a new result table by combining column values of two tables (table1 and table2) based upon the join-condition. Keyword used is INNER JOIN or simply JOIN. Deepthi Rachumallu
  • 6. INNER JOIN OR EQUIJOIN Syntax SELECT table1.column1, table2.column2... FROM table1 INNER JOIN table2 ON table1.common_field = table2.common_field; SELECT emp.ename,emp.job,dept.dname,dept.loc from EMPLOYYE emp INNER JOIN DEPARTMENT dept ON emp.deptno = dept.deptno; Deepthi Rachumallu
  • 7. Result…… ENAME JOB DNAME LOC Ramesh Analyst INVENTORY HYDERABAD Khilan Clerk FINANCE BANGALORE Kaushik Manager INVENTORY HYDERABAD Deepthi Rachumallu
  • 8. Left Join/Left Outer Join LEFT JOIN returns all rows from the left table (table1), even if there are no matches in the right table(table2). If there are no matches found in right table the result is null in the right table. Keyword used is LEFT JOIN. Deepthi Rachumallu
  • 9. Left Join Continuation… Syntax  SELECT table1.column1, table2.column2... FROM table1 LEFT JOIN table2 ON table1.common_field = table2.common_field; SELECT emp.ename,emp.job,dept.dname,dept.loc from EMPLOYYE emp LEFT JOIN DEPARTMENT dept ON emp.deptno = dept.deptno; OR SELECT emp.ename,emp.job,dept.dname,dept.loc from EMPLOYYE emp , DEPARTMENT dept WHERE emp.deptno(+) = dept.deptno; ENAME JOB DNAME LOC Ramesh Analyst INVENTORY HYDERABAD Khilan Clerk FINANCE BANGALORE Kaushik Manager INVENTORY HYDERABAD Chaitali engineer NULL NULL Deepthi Rachumallu
  • 10. Right Join/Right Outer Join RIGHT JOIN returns all rows from the right table (table2), even if there are no matches in the left table(table1). If there are no matched found in let table the result is null in the left table. Keyword used is RIGHT JOIN. Deepthi Rachumallu
  • 11. Right Join Continuation… Syntax  SELECT table1.column1, table2.column2... FROM table1 RIGHT JOIN table2 ON table1.common_field = table2.common_field; SELECT emp.ename,emp.job,dept.dname,dept.loc from EMPLOYYE emp RIGHT JOIN DEPARTMENT dept ON emp.deptno = dept.deptno; OR SELECT emp.ename,emp.job,dept.dname,dept.loc from EMPLOYYE emp, DEPARTMENT dept WHERE emp.deptno = dept.deptno(+); ENAME JOB DNAME LOC Ramesh Analyst INVENTORY HYDERABAD Khilan Clerk FINANCE BANGALORE Kaushik Manager INVENTORY HYDERABAD NULL NULL HR MUMBAI Deepthi Rachumallu
  • 12. Full Join The SQL FULL JOIN combines the results of both left and right outer joins. The joined table will contain all records from both tables, and fill in NULLs for missing matches on either side. Keyword used is FULL JOIN. In some data bases it is also known as FULL OUTER JOIN. Deepthi Rachumallu
  • 13. Full Join Continuation… Syntax  SELECT table1.column1, table2.column2... FROM table1 FULL JOIN table2 ON table1.common_field = table2.common_field; SELECT emp.ename,emp.job,dept.dname,dept.loc from EMPLOYYE emp FULL JOIN DEPARTMENT dept ON emp.deptno = dept.deptno; ENAME JOB DNAME LOC Ramesh Analyst INVENTORY HYDERABAD Khilan Clerk FINANCE BANGALORE Kaushik Manager INVENTORY HYDERABAD NULL NULL HR MUMBAI Chaitali engineer NULL NULL Deepthi Rachumallu
  • 14. Self Join SELF JOIN is used to join a table to itself as if the table were two tables Syntax  SELECT a.column_name, b.column_name... FROM table1 a, table1 b WHERE a.common_field = b.common_field; Deepthi Rachumallu
  • 15. Self Join Continuation…. SELECT emp1.ename, emp1.job, emp2.ename as manager FROM EMPLOYEE emp1, EMPLOYEE emp2 where emp1.mgr = emp2.empno; ENAME JOB MANAGER Ramesh Analyst Chaitali Khilan Clerk Kaushik Kaushik Manager Ramesh Chaitali engineer Khilan Deepthi Rachumallu