From Algebra to SQL
SQL Queries
◼ Basic SQL structure
SELECT A1, A2, …, An
FROM R1, R2, …, Rm
WHERE P
where
Ai are attributes from…
Ri which are relations
P is a predicate
• can replace Ai’s if all attributes are to be retrieved
• A1, A2, …, An(F)
• F is an expression with R1, R2, …, Rm as
2
From Algebra to SQL
◼ Basic SQL Query:
SELECT A1,…, An
FROM R1,…, Rk
WHERE f(R1,..., Rk)
◼ Algebra
A1…An(F(R1,…, Rk))
F is a formulae in Algebra with
f: expresses the selections and join conditions
Types of Queries
◼ Simple (Level 1)
⚫ Retrieve tuples from a table that match a search criteria
⚫ The table can be an existing table in the database
schema or constructed using tables in the schema
⚫ Level 1 queries are expresses using Selection,
Projection and Join
◼ Complex (Level 2)
⚫ Retrieve tuples from a reference table R that do not
qualify (R1)
⚫ Compute R-R1 to get the answer
4
Example Queries
Emp (Eno, Ename, Title, City) (note we added City)
Project(Pno, Pname, Budget, City) (note we added City)
Pay(Title, Salary)
Works(Eno, Pno, Resp, Dur)
◼ List names of all employees.
⚫ Ename(Emp)
⚫ SELECT Ename
FROM EMP
◼ List names of all projects together with their
budgets.
⚫ Pname,Budget(Project)
⚫ SELECT Pname, Budget
FROM Project 5
Example Queries
Emp (Eno, Ename, Title, City) (note we added City)
Project(Pno, Pname, Budget, City) (note we added City)
Pay(Title, Salary)
Works(Eno, Pno, Resp, Dur)
◼ Find all job titles to which at least one employee has been
hired.
⚫ Title(Emp)
⚫ SELECT Title
FROM EMP
◼ Find the records of all employees who live in Newark.
⚫ City=‘Newark’(Emp)
⚫ SELECT *
FROM EMP
Where City = ‘Newark’
6
6
Example Queries
Emp (Eno, Ename, Title, City)
Project(Pno, Pname, Budget, City)
Pay(Title, Salary)
Works(Eno, Pno, Resp, Dur)
◼ Find all cities where either an employee works or
a project exists.
⚫ City(Emp) City(Project)
◼ Find all cities that has a project but no employees
who work there.
⚫ City(Project) − City(Emp)
Example Queries
Emp (Eno, Ename, Title, City)
Project(Pno, Pname, Budget, City)
Pay(Title, Salary)
Works(Eno, Pno, Resp, Dur)
◼ Find all cities where either an employee works, or
a project exists.
⚫ City(Emp) City(Project)
⚫ SELECT DISTINCT City
FROM EMP
UNION
(SELECT City
FROM Project)
8
8
Example Queries
Emp (Eno, Ename, Title, City)
Project(Pno, Pname, Budget, City)
Pay(Title, Salary)
Works(Eno, Pno, Resp, Dur)
◼ Find all cities that has a project but no employees
who work there.
◼ City(Project) − City(Emp)
⚫ SELECT City
FROM Project
Minus
(SELECT City
FROM Emp)
9
Example Queries
Emp (Eno, Ename, Title, City)
Project(Pno, Pname, Budget, City)
Pay(Title, Salary)
Works(Eno, Pno, Resp, Dur)
◼ Find the names of all projects with budgets greater
than $225,000.
⚫ Pname(Budget>225000 (Project))
⚫ SELECT Pname
FROM Project
WHERE Budget>225000
10
10
Example Queries
Emp (Eno, Ename, Title, City)
Project(Pno, Pname, Budget, City)
Pay(Title, Salary)
Works(Eno, Pno, Resp, Dur)
◼ List the names and budgets of projects on which
employee E1 works.
⚫ Pname, Budget(Project (Eno=‘E1’ (Works)))
⚫ Pname, Budget(Emp.Eno=Works.Eno (Project Eno=‘E1’
(Works)))
⚫ SELECT Pname, Budget
FROM Project P, Works W
WHERE W.PNO=P.PNO
AND W.ENO=‘E1’ 11
11
Example Queries
Emp (Eno, Ename, Title, City)
Project(Pno, Pname, Budget, City)
Pay(Title, Salary)
Works(Eno, Pno, Resp, Dur)
◼ Find the name of all the employees who live in a city
where no project is located.
⚫ Ename(Emp (City(Emp) − City(Project))
⚫ SELECT E.Ename
FROM Emp E
WHERE E.City NOT IN
(SELECT City
FROM Project )
12
12
Example Queries
Emp (Eno, Ename, Title, City)
Project(Pno, Pname, Budget, City)
Pay(Title, Salary)
Works(Eno, Pno, Resp, Dur)
◼ Find the name of all the employees who live in a city
where no project is located.
⚫ Ename(Emp (City(Emp) − City(Project))
⚫ SELECT E.Ename
FROM Emp E
WHERE NOT EXISTS
(SELECT *
FROM Project P
WHERE P.City= E.City) )
13
13
Example Queries
Emp (Eno, Ename, Title, City)
Project(Pno, Pname, Budget, City)
Pay(Title, Salary)
Works(Eno, Pno, Resp, Dur)
◼ Find pairs of employees working on the same
projects.
⚫ Ename(Works Pno=PnoWorks)
⚫ Ename(Works Pno=Pno^Eno!=EnoWorks)
⚫ Ename(Works Pno=Pno^Eno<EnoWorks)
⚫ SELECT W1.Eno, W2.Eno
FROM Works W1, Works W2
WHERE W1.Pno=W2.Pno
And W1.Eno<W2.Eno
14
14
Division (Quotient)
Given relations
⚫ R of degree k1 (R = {A1,…,Ak1})
⚫ S of degree k2 (S = {B1,…,Bk2})
Let A = {A1,…,Ak1} [i.e., R(A)]and B = {B1,…,Bk2}
[i.e., S(B)] and B A.
Then, T = R ÷ S gives T of degree k1-k2 [i.e., T(Y) where
Y = A-B] such that for a tuple t to appear in T, the
values in t must appear in R in combination with every
tuple in S.
15
15
Division (cont’d)
R S
X Y X
x1 y1 x1
x2 y1 x2
x3 y1 X3
T1 Y(R)
x4 y1 x4
T2 Y((S T1) − R)
x1 y2 T T1 − T2
x3 y2
T
x2 y3
Y
x3 y3
y1
x4 y3
y4
x1 y4
x2 y4
x3 y4
x4 y4
16
16
Complex Queries - Division
Emp (Eno, Ename, Title, City)
Project(Pno, Pname, Budget, City)
Pay(Title, Salary)
Works(Eno, Pno, Resp, Dur)
◼ Find all the employees who work on every project
⚫ Not(Employees NOT working on at least 1 project).
⚫ Eno, Pno(Works) ÷ Pno(Project)
SELECT W1.Eno
FROM Works W1
WHERE W1.Eno NOT IN
(SELECT W2.Eno
FROM Works W2, Project P
WHERE W2.ENO NOT IN
(SELECT W3.ENO
FROMWorks W3
WHERE P.PNO=W3.PNO))
17
17
Complex Queries - Division
Emp (Eno, Ename, Title, City)
Project(Pno, Pname, Budget, City)
Pay(Title, Salary)
Works(Eno, Pno, Resp, Dur)
◼ Find all the employees who work on every project.
⚫ Employees such that there is NO project they are not working on)
⚫ Eno, Pno(Works) ÷ Pno(Project)
SELECT W1.ENO
FROM Works W1
WHERE NOT EXISTS
(SELECT *
FROM Project P
WHERE NOT EXISTS
(SELECT W2.ENO
FROMWorks W2
WHERE P.PNO=W2.PNO
AND W1.ENO= W2.ENO))
18
18
Complex Queries - NOT Division
branch (B-name, Address, City, Assets)
customer(C-name, Street, City)
deposit(Acc-number, C-name, B-name, Balance)
borrow(Acc-number, C-name, B-name, Amount)
◼ Find the names and addresses of all the customers
who have no deposit in the bank.
⚫ NOT Customer who have deposit in at least 1 branch)
SELECT C-name, Street, City
FROM customer
WHERE NOT EXISTS
(SELECT *
FROM deposit
WHERE customer.C-name=deposit.C-name)
19
19
Complex Queries - Division
branch (B-name, Address, City, Assets)
customer(C-name, Street, City)
deposit(Acc-number, C-name, B-name, Balance)
borrow(Acc-number, C-name, B-name, Amount)
◼ Find all the customers who have deposits in every
branch of the bank.
⚫ Find all customers such that there is no bank branch where
they do not have deposits
SELECT *
FROM customer
WHERE NOT EXISTS
(SELECT *
FROM branch
WHERE NOT EXISTS
(SELECT *
FROM deposit
WHERE branch.B-name=deposit.B-name
AND customer.C-name = deposit.C-name) 20
20
Complex Queries - Division
branch (B-name, Address, City, Assets)
customer(C-name, Street, City)
deposit(Acc-number, C-name, B-name, Balance)
borrow(Acc-number, C-name, B-name, Amount)
◼ Find all the customers who have deposits in every
branch of the bank.
⚫ NOT (Customers who do not have a deposit in at least one
branch)
SELECT *
FROM customer
WHERE C-name NOT IN
(SELECT C.C-name
FROM branch B, Customer C
WHERE C.C-name NOT IN
(SELECT D.C-name
FROM deposit D
WHERE B.B-name=D.B-name))
21
21