Lec 9
Lec 9
Lecture 9
Inserting from Another Table
2
select *
from one left join two
on one.x = two.x;
X A X B
1 a .
2 b 2 x
4 d .
s105d07
Right Join
5
Table Two Table One
X B X A
2 x 1 a
3 y 4 d
5 v 2 b
select *
from two right join one
on one.x = two.x;
X B X A
. 1 a
2 x 2 b
. 4 d
Full Join
6
select *
from one full join two
on one.x = two.x;
X A X B
1 a .
2 b 2 x
. 3 y
4 d .
. 5 v
Aliases and ‘Self-Joins’
7
A.Name
John
Andy
Anne
Union U
Intersection ∩
Difference -
The two relation
must be union
compatible
Union Operator
11
Mexico D.F.
New Orleans
To select duplicate values
15
Ann Arbor
Berlin
London
Mexico D.F.
Mexico D.F.
New Orleans
Exercise
16
selects all the different German cities (only distinct
values) and the country from "Customers" and
"Suppliers“
Customers(CID,Cname,City,Country)
Supplier(SID,Sname, City,Country)
SELECT City, Country FROM Customers
WHERE Country='Germany'
UNION
SELECT City, Country FROM Suppliers
WHERE Country='Germany'
ORDER BY City;
Borrower( ID, Customer_name, amount)
Depositor( ID, customer_name, balance)
Set Operations
17
Find all customers who have both a loan, an account or both.
(select customer_name from depositor)
union
(select customer_name from borrower)
Find all customers who have both a loan and an account.
(select customer_name from depositor)
intersect
(select customer_name from borrower)
Find all customers who have an account but no loan.
(select customer_name from depositor)
except
(select customer_name from borrower)
Processing Multiple Tables -- Subqueries
18
Subquery is embedded in
parentheses. In this case it
returns a list that will be used
in the WHERE clause of the
outer query
Example
21
Get all the products names that have been sold at least
once.
Product ProductSales
Pid Name Description ID Pid Unit price Qty
1 TV 52 inch 1 1 450 7
High 2 3 1200 8
2 Camera
resolution
Very thin 3 3 1200 20
3 Laptop
black color
22
Get all the products that have been sold at least once.
High 2 3 1200 8
2 Camera
resolution
Very thin 3 3 1200 20
3 Laptop
black color
Exercise 1
24
1 TV 52 inch 1 1 450 7
High 2 3 1200 8
2 Camera
resolution
Very thin 3 3 1200 20
3 Laptop
black color
Exercise
25
Product ProductSales
Pid Name Description ID Pid Unit price Qty
1 TV 52 inch 1 1 450 7
High 2 3 1200 8
2 Camera
resolution
Very thin 3 3 1200 20
3 Laptop
black color
Exercise
26
Select Pid
From Product
Except
Select Pid
From productSales
Product
ProductSales
Pid Name Description
ID Pid Unit price Qty
1 TV 52 inch
1 1 450 7
High
2 Camera
resolution
2 3 1200 8
Very thin
3 Laptop
black color 3 3 1200 20
Exercise 2
28
Employee(SSN, Fname, Lname, Salary, DepId)
Dependent(DSSN, Name, SSN)
Department(DepId,Depname, Location)
Retrieve the name of each employee who has a dependent
with the same first name as the employee.[ write in two different ways]
Exercise 2
29
Employee(SSN, Fname, Lname, Salary, DepId)
Dependent(DSSN, Name, SSN)
Department(DepId,Depname, Location)
Retrieve the name of each employee who has a dependent
with the same first name as the employee.[ write in two different ways]
SELECT E.FNAME, E.LNAME
FROM EMPLOYEE AS E
WHERE E.SSN IN (SELECT SSN
FROM DEPENDENT As D
WHERE E.FNAME=D.NAME)