Database Management System 12: Chittaranjan Pradhan School of Computer Engineering, KIIT University
Database Management System 12: Chittaranjan Pradhan School of Computer Engineering, KIIT University
Database Management System 12: Chittaranjan Pradhan School of Computer Engineering, KIIT University
Chittaranjan Pradhan
Aggregate
System 12 Functions(g)
Join
Outer Join
Left Outer Join
Right Outer Join
Full Outer Join
Self Join
Chittaranjan Pradhan
School of Computer Engineering,
KIIT University
12.1
JOIN
Generalized Projection
Chittaranjan Pradhan
Generalized Projection
Aggregate
Functions(g)
Generalized Projection
Join
The generalized-projection operation extends the projection Inner Join
Outer Join
Left Outer Join
Self Join
Ex:Emp=(ssn, salary, deduction, years_service) be a relation.
A report may be required to show net_salary=salary-deduction,
bonus=2000*years_service and tax=0.25*salary
12.2
JOIN
Aggregate Functions(g)
Chittaranjan Pradhan
Aggregate Functions(g)
Aggregate functions take a collection of values and return a Generalized Projection
Aggregate
single value as a result. NULL value will not participate in the Functions(g)
Outer Join
Query: Find the total sum of salaries of all the employees Left Outer Join
Self Join
Query: Find the total sum of salaries of all the employees in
each branch
Ans: branch_name g SUM(salary ) (Works)
Generalized Projection
Join Aggregate
Functions(g)
Tables are joined on columns that have the same datatype and Inner Join
Theta Join
data width in the tables Equi Join
Natural Join
Outer Join
Join operation joins two relations by merging those tuples from Left Outer Join
Right Outer Join
two relations that satisfy a given condition. The condition is Full Outer Join
12.4
JOIN
Inner Join
Chittaranjan Pradhan
Generalized Projection
Aggregate
Functions(g)
Join
• Equi Join
• Natural Join
12.5
JOIN
Theta Join(o
n θ)
Chittaranjan Pradhan
Generalized Projection
Theta Join(o
n θ) Aggregate
Functions(g)
Inner Join
column from each relation. This condition specifies that the two Theta Join
Outer Join
The comparison operator can be any of the six: <, ≤, >, ≥, = Left Outer Join
Right Outer Join
and 6= Full Outer Join
Self Join
12.6
JOIN
Theta Join(o
n θ )...
Chittaranjan Pradhan
Generalized Projection
Aggregate
Functions(g)
Account Loan Join
acc_no branch_name balance loan_no branch_name amount
A101 Bhubaneswar Main 100,000.00 L201 Bhubaneswar Main 50,000,000.00 Inner Join
A102 Shastri Nagar 50,000.00 L202 Bhubaneswar Main 5,000,000.00 Theta Join
A103 India Gate 5,000,000.00 L203 Mumbai Main 100,000,000.00 Equi Join
A104 Juhu 600,000.00 L204 Juhu 60,000,000.00 Natural Join
Account o
n balance≥amount Loan
acc_no branch_name balance loan_no branch_name amount
A103 India Gate 5,000,000.00 L202 Bhubaneswar Main 5,000,000.00
A105 Mumbai Main 10,000,000.00 L202 Bhubaneswar Main 5,000,000.00
12.7
JOIN
Equi Join(o
n =)
Chittaranjan Pradhan
Generalized Projection
Aggregate
Functions(g)
Join
Equi Join(o
n =)
Inner Join
Theta Join
The equi join is the theta join based on equality of specified Equi Join
columns. That means the equi join is the special type of theta Natural Join
Outer Join
join where the comparison operator is = Left Outer Join
Right Outer Join
Full Outer Join
The general form of theta join is: Self Join
Ron = S = π all (σ = (R × S))
• Degree (Result) = Degree (R) + Degree (S)
• Cardinality (Result) ≤ Cardinality(R) × Cardinality(S)
12.8
JOIN
Equi Join(o
n = )...
Chittaranjan Pradhan
Generalized Projection
Aggregate
Functions(g)
Borrower Loan Join
cust_name loan_no loan_no branch_name amount
Inner Join
Ramesh L201 L201 Bhubaneswar Main 50,000,000.00 Theta Join
Ramesh L202 L202 Bhubaneswar Main 5,000,000.00 Equi Join
Natural Join
Mahesh L203 L203 Mumbai Main 100,000,000.00
Outer Join
Rishi L204 L204 Juhu 60,000,000.00
Left Outer Join
Right Outer Join
Q: Find the customer name and their loan details Full Outer Join
Self Join
Borrower o
n Borrower .loan_no=Loan.loan_no Loan
cust_name Borrower.loan_no Loan.loan_no branch_name amount
Ramesh L201 L201 Bhubaneswar Main 50,000,000.00
Ramesh L202 L202 Bhubaneswar Main 5,000,000.00
Mahesh L203 L203 Mumbai Main 100,000,000.00
Rishi L204 L204 Juhu 60,000,000.00
12.9
JOIN
Natural Join(o
n)
Chittaranjan Pradhan
Generalized Projection
Natural Join(o
n) Aggregate
Functions(g)
To perform natural join on two relations, they should contain at Join
least one common attributes. It is just like the equi join with the Inner Join
Theta Join
elimination of the common attributes. The natural join is Equi Join
Outer Join
Left Outer Join
12.10
JOIN
Natural Join(o
n)...
Chittaranjan Pradhan
Generalized Projection
Aggregate
Borrower Loan Functions(g)
cust_name loan_no loan_no branch_name amount Join
Ramesh L201 L201 Bhubaneswar Main 50,000,000.00 Inner Join
Ramesh L202 L202 Bhubaneswar Main 5,000,000.00 Theta Join
Equi Join
Mahesh L203 L203 Mumbai Main 100,000,000.00 Natural Join
Rishi L204 L204 Juhu 60,000,000.00 Outer Join
Left Outer Join
Right Outer Join
Q: Find the customer name and their loan details Full Outer Join
Self Join
Borrower o
n Loan
cust_name loan_no branch_name amount
Ramesh L201 Bhubaneswar Main 50,000,000.00
Ramesh L202 Bhubaneswar Main 5,000,000.00
Mahesh L203 Mumbai Main 100,000,000.00
Rishi L204 Juhu 60,000,000.00
12.11
JOIN
Outer Join
Chittaranjan Pradhan
Generalized Projection
Aggregate
Outer Join Functions(g)
Inner Join
missing information. The outer join consists of two steps: Theta Join
Equi Join
• First, a natural join is executed Natural Join
• Then if any record in one relation does not match a record Outer Join
Left Outer Join
from the other relation in the natural join, that unmatched Right Outer Join
Full Outer Join
record is added to the join relation, and the additional Self Join
columns are filled with NULLs
The different types of outer join are:
• Left Outer Join
• Right Outer Join
• Full Outer Join
12.12
JOIN
Left Outer Join
Chittaranjan Pradhan
Generalized Projection
Aggregate
Functions(g)
./ Inner Join
Theta Join
) Equi Join
Natural Join
The left outer join preserves all tuples in left relation. The left Outer Join
Left Outer Join
outer join is denoted by symbol: Right Outer Join
Full Outer Join
Self Join
./
12.13
JOIN
Left Outer Join...
Chittaranjan Pradhan
Customer Borrower
cust_name cust_street cust_city cust_name loan_no Generalized Projection
Outer Join
Q: Find out the customer details who have taken loans as well Left Outer Join
Self Join
Customer ./ Borrower
cust_name cust_street cust_city loan_no
Rishi India Gate New Delhi L204
Ramesh M. G. Road Bhubaneswar L201
Ramesh M. G. Road Bhubaneswar L202
Mahesh Juhu Mumbai L203
Sarthak M. G. Road Bangalore NULL
Manas Shastri Nagar Bhubaneswar NULL
12.14
JOIN
Right Outer Join
Chittaranjan Pradhan
Generalized Projection
Aggregate
Functions(g)
./ Inner Join
Theta Join
) Equi Join
Natural Join
The right outer join preserves all tuples in right relation. The Outer Join
Left Outer Join
right outer join is denoted by symbol: Right Outer Join
Full Outer Join
Self Join
./
12.15
JOIN
Right Outer Join...
Chittaranjan Pradhan
Borrower Customer
cust_name loan_no cust_name cust_street cust_city Generalized Projection
Outer Join
Q: Find out the customer details who have taken loans as well Left Outer Join
Self Join
Borrower ./ Customer
cust_name loan_no cust_street cust_city
Rishi L204 India Gate New Delhi
Ramesh L201 M. G. Road Bhubaneswar
Ramesh L202 M. G. Road Bhubaneswar
Mahesh L203 Juhu Mumbai
Sarthak NULL M. G. Road Bangalore
Manas NULL Shastri Nagar Bhubaneswar
12.16
JOIN
Full Outer Join
Chittaranjan Pradhan
Generalized Projection
Aggregate
Functions(g)
./ Inner Join
Theta Join
) Equi Join
Natural Join
The full outer join preserves all tuples in both relations. The full Outer Join
Left Outer Join
outer join is denoted by symbol: Right Outer Join
Full Outer Join
Self Join
./
12.17
JOIN
Self Join
Chittaranjan Pradhan
Generalized Projection
Aggregate
Functions(g)
Join
Self Join
Inner Join
The self join is similar to the theta join. It joins a relation to itself Theta Join
Equi Join
by a condition. The self join can be viewed as a join of two Natural Join
Thus, the self join creates two alias or copies of the same
relation; then performs the theta join by a condition based on
the attributes of these two copies
12.18
JOIN
Self Join...
Chittaranjan Pradhan
Generalized Projection
Aggregate
Functions(g)
cust_name cust_street cust_city Join
Rishi India Gate New Delhi
Inner Join
Sarthak M. G. Road Bangalore
Customer Manas Shastri Nagar Bhubaneswar
Theta Join
Equi Join
Natural Join
Ramesh M. G. Road Bhubaneswar
Outer Join
Mahesh Juhu Mumbai Left Outer Join
Right Outer Join
Q: Find out the customer details as well as the others’ staying Full Outer Join
Self Join
in the same cust_city
C1 o
n C1.cust_city =C2.cust_city C2
C1.cust_name C1.cust_street C1.cust_city C2.cust_name C2.cust_street C2.cust_city
Manas Shastri Nagar Bhubaneswar Ramesh M. G. Road Bhubaneswar
Ramesh M. G. Road Bhubaneswar Manas Shastri Nagar Bhubaneswar
12.19