Malaviya National Institute of Technology Jaipur: Goal
Malaviya National Institute of Technology Jaipur: Goal
GOAL:
What are joined Relations?
Brief overview of joined relations
Getting familiar with different join operations
Inner join
Left outer join
Natural inner join
Right outer join
Full outer join
Exercise:
i. Determine the names of sailors who are older than the oldest sailor with a rating of 10?
ii. Determine the names of sailors who have reserved all boats?
iii. Determine the average age of sailors with a rating of 10?
iv. Determine the name and the age of the oldest sailor?
v. Count the number of sailors?
vi. For each red boat, find the number of reservations for this boat?
Each of the variants of the join operations in SQL consists of a join type and a
join condition. The join condition denes which tuples in the two relations match
and what attributes are present in the result of the join. The join type denes how
tuples in each relation that do not match any tuple in the other relation (based on
the join condition).
Following tables gives us some join type and join condition:
LOAN BORROWER
loanNo branch_Name amount Customer_name loanNo
L-170 Downtown 3000 Jones L-170
L-230 Redwood 4000 Smith L-230
L-260 Perryridge 1700 Hayes L-155
L-155 Washigton 4500
DEPOSITOR
Customer_name Account_number
Jones HDBC2044
Smith HDBC2055
Hayes HDBC2066
Chris HDBC2077
Adam HDBC2088
Getting Familiar With Different Join Operations:
Inner Joins:
We illustrate the use of inner joins through an example:
Select * from (loan l inner join borrower b on l.loanno =b.loanno);
//observe the output carefully specifically field names and values of resultant.
The expression computes the theta join of the loan and the borrower relations, with
the join condition being loan.loan-number = borrower.loan-number. The attributes
of the result consist of the attributes of the left-hand-side relation followed by the
attribute of the right-hand-side relation.
This expression computes the natural join of the two relations. The only attribute
name common to loan and borrower is loan-number.
Difference between natural join and inner join is that the attribute loan-
number appears only once in the result of the natural join but it comes twice
in case of inner join.
The right outer join is symmetric to the left outer join. Tuples from the right-
hand- side relation that do not match any tuple in the left-hand-side relation are
padded with nulls and are added to the result of the right outer join.
Full outer join
we consider an example of the full outer join operation:
select * from ( loan l full outer join borrower b on l.loanno=b.loanno);
//observer the output carefully and compare with previous outputs
The full outer join is a combination of the left and right outer-join types. After
he operation computes the result of the inner join, it extends with nulls tuples from
the left-hand-side relation that did not match with any from the right-hand-side, and
adds them to the result. Similarly, it extends with nulls tuples from the right-hand-
side relation that did not match with any tuples from the left-hand-side relation and
adds them to the result.
Example: Find all customers who have either an account or a loan (but not both) at
the bank?
SELECT customer-name
FROM (depositor natural full outer join borrower)
WHERE account-number is null OR loan-number is null;
Exercise 1 : Create a database for an Organization as per the details given below.
Insert at least 10 sample tuples in each table and write the SQL queries for the following:
a) Retrieve the name of each employee who has a dependent with the same first name and
same sex as the employee.
b) Retrieve the names of employees who have no dependents.
c) Retrieve the name and address of every employee who works for the Research
department.
d) For each employee, retrieve the employees first and last name and the first and last
name of his or her immediate supervisor.
e) For every project located in Stafford, list the project number, the controlling
department number, and the department managers last name, address, and birthdate.
Exercise 2 : Create a database for a Banking System as per the details given below.
Insert at least 10 sample tuples in each table and write the SQL queries for the following:
i. Find the names of all branches in the loan relation.
ii. Find all loan numbers for loans made at the SBI branch.
iii. Find the loan numbers where loan amounts is between $9000 and $100,000.
iv. Find the Cartesian product of borrower and loan.
v. Find the name, loan number and loan amount of all customers having a loan at the
Perryridge branch.
vi. Find the name, loan number and loan amount of all customers; rename the column
name loan_number as loan_id.
vii. Find the customer names and their loan numbers for all customers having a loan at
some branch.
viii. Find the names of all branches that have greater assets than some branch located in
Jaipur.
ix. Find the names of all customers whose street includes the substring pur.
x. List in alphabetic order the names of all customers having a loan in Jaipur branch.
xi. Find all customers who have a loan, an account, or both.
xii. Find the average account balance at the ICICI branch.
xiii. Find the number of tuples in the customer relation.
xiv. Find the number of depositors in the bank.
xv. Find the number of depositors for each branch.
xvi. Find the names of all branches where the average account.
xvii. Find all customers who have both an account and a loan at the bank.
xviii. Find all customers who have a loan at the bank but do not have an account at the
bank.
xix. Find all customers who have both an account and a loan at the Perryridge branch.
xx. Find all branches that have greater assets than some branch located in Brooklyn.