0% found this document useful (0 votes)
56 views

Joins in SQL

SQL joins are used to combine rows from two or more tables based on a common field. There are various types of joins including inner, outer, left, right, and full joins. Inner joins return rows where there are matches in both tables, while outer joins return all rows even if there are no matches and fill missing values with NULL.

Uploaded by

abcd efgh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Joins in SQL

SQL joins are used to combine rows from two or more tables based on a common field. There are various types of joins including inner, outer, left, right, and full joins. Inner joins return rows where there are matches in both tables, while outer joins return all rows even if there are no matches and fill missing values with NULL.

Uploaded by

abcd efgh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

JOINS

JOINS
• SQL joins are used to combine rows from two or more tables
based on a common field between them
• In SQL, there are various types of joins:

1) Natural Join
• Inner Join
• Equi Join
2) Outer Join
• Left Outer Join
• Right Outer Join
• Full Outer Join
• Tables are joined on columns that have same datatype and size
Inner Join
• It is a natural join used for retrieving records from more than two
tables having common data
• Inner join can be Equi join if it is having equal condition only
Syntax:

Select column 1, column 2…., column N from Table 1 Inner Join


Table 2 ON Table1.Column1= Table2.Column2 Where condition
Order BY Column
Emp_No Name Dept_No Dept_Name Dept_No
11 Smith B001 CS B001
23 William B007 EC B007
34 Simi C006 CS C009
CS F007

Employee
Dept

List out the employee number, name and department names


in which they belong?
Select e.emp_no, e.name, d.dept_name from Employee e
INNER JOIN Dept d ON d. dept_no= e. dept_no ;

Select e.emp_no, e.name, d.dept_name from Employee e,


Dept d where d. dept_no= e. dept_ no ;
List out those employee details along with their department
name whose name starts with S

Select e.emp_no, e.name, d.dept_name from Employee e


INNER JOIN Dept d ON d. dept_no= e. dept_no where e.name
LIKE ‘S%’ ORDER BY e. name ;

Select e.emp_no, e.name, d.dept_name from Employee e,


Dept d where d. dept_no = e. dept_no and e.name LIKE ‘S%’
ORDER BY e. name ;
Employee
Emp_No Name Branch_No List out the employee details along
11 Smith B001 with their accounts and branch no of
23 William B007 Mumbai branch
34 Simi C006 Select e.emp_no, e.name, a. acct_no,
22 terry F007 b.branch_no from Account a INNER
JOIN Branch b ON a. branch_name =
b. branch_name INNER JOIN
Acct_no Branch_n employee e ON e.branch_no = b.
ame
branch_no where
1008 Mumbai Account b.branch_name=‘Mumbai’
1007 PUNE
1021 Calcutta Select e.emp_no, e.ename,
a . acct_no, b. branch_no from
account a, branch b, employee e
Branch_Name Branch_N
o where
Branch a. branch_name = b.branch_name
Mumbai B001
Mumbai B007 and
Pune C006 b.branch_no = e. branch_no and
b.branch_name=‘Mumbai’
Left Outer Join
• The LEFT JOIN keyword returns all rows from the left table
(table1), with the matching rows in the right table (table2).
• The result is NULL in the right side when there is no match.

Table 1 Table 2

Table 1 Left Join Table 2

Resultant
Table
Emp_No Name Branch_No
11 Smith B001
List out the employee details
23 William B007
along with their branch name
34 Simi C006
using Left Outer Join
Employee
Branch_Name Branch_N Select e.emp_no, e.name,
o
b.branch_name from Employee e LEFT
Mumbai B001 OUTER JOIN Branch b ON b. branch_no
Mumbai B007 = e. branch no
Pune C009
Banglore F007
Select e.emp_no, e.name,
Branch b.branch_name from Employee e,
Branch b where b. branch_no = e.
branch no (+);
Right Outer Join
• The Right JOIN keyword returns all rows from the right table
(table2), with the matching rows in the left table (table1).
• The result is NULL in the right side when there is no match.

Table 1 Table 2

Table 1 Right Join Table 2

Resultant
Table
Emp_No Name Branch_No
11 Smith B001
List out the employee details
23 William B007
along with their branch name
34 Simi C006
using Right Outer Join
Employee
Select e.emp_no, e.name,
Branch_Name Branch_N b.branch_name from Employee e
o RIGHT OUTER JOIN Branch b ON b.
Mumbai B001 branch_no = e. branch no
Mumbai B007
Pune C009 Select e.emp_no, e.name,
Banglore F007 b.branch_name from Employee e,
Branch b where b. branch_no (+) = e.
Branch
branch no;
Full Outer Join
• It does both of those operations, padding tuples from the left
relation that did not match any from the right relation
• As well as tuples from the right relation that did not match any
from the left relation and adding them to the result of the join.
Table 2
Table 1

Table 1 Full Outer Join Table 2

Resultant
Table
Emp_No Name Branch_No
11 Smith B001
List out the employee details
23 William B007
along with their branch name
34 Simi C006
using Full Join
Employee

Branch_Name Branch_N
Select e.emp_no, e.name,
o b.branch_name from Employee e
Mumbai B001 FULL OUTER JOIN Branch b ON b.
Mumbai B007
branch_no = e. branch no
Pune C009
Banglore F007

Branch

You might also like