



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Relational Algebra 1
Relational Algebra 2
a a a a
a a a a
b b b b
b b b b
c c c c
a a a a a a
b b b b b b
c c c c c c
(natural) join (*)
select (σ)
project (π)
eliminate duplicates
a a a a b c x y z z x y x z
a
Divide (÷)
Relational Algebra 3
Q1. Find the subset of employees who are in dept. 50. σDNO = 50 (EMP)
Q2. Find the subset of employees who are in dept. 25, 47 or 53. σDNO = 25 or DNO = 47 or DNO = 53 (EMP)
Q3. List names and jobs of all employees. π (^) NAME, JOB (EMP)
Relational Algebra 4
Q4. List names and jobs of employees in departments 25, 47 or 53.
π (^) NAME, JOB (σDNO = 25 or DNO = 47 or DNO = 53 (EMP))
D_EMPS ← σDNO = 25 or DNO = 47 or DNO = 53 (EMP) RESULT ← π (^) NAME, JOB (D_EMP)
Relational Algebra 5
Q5. List the numbers of those departments which have an employee named ‘Smith’ or are located in ‘Columbus’. π (^) DNO (σ (^) NAME = ‘Smith’ (EMP)) ∪ π (^) DNO (σ (^) LOC = ‘Columbus’ (DEPT))
Q6. List the DNO for departments that have no employees. π (^) DNO (DEPT) - π (^) DNO (EMP) Relational Algebra 6
Q7. List the names of all employees and the locations of their departments.
π (^) NAME, LOC (EMP ^5 EMP.DNO = DEPT.DNO (DEPT) )
π (^) NAME, LOC (EMP * DEPT) )
* Natural join
Relational Algebra 7
Natural join ()* Q ← R 1 * (^) (list1), (list2) R 2 listi has attributes from relation Ri list 1 names go in Q
Q ← R 1
Relational Algebra 8
Q8. Find the names of employees who work in Ann Arbor.
π (^) NAME (EMP (^) EMP.DNO = DEPT.DNO (σ (^) LOC = ‘Ann Arbor’(DEPT) )
π (^) NAME (EMP * (σ (^) LOC = ‘Ann Arbor’(DEPT) ) Natural join *
equi-join
Relational Algebra 9
Q9. Find the names of employees who do not work in Ann Arbor.
π (^) NAME (EMP (^) EMP.DNO = DEPT.DNO ^5 (σ (^) LOC ≠ ‘Ann Arbor’(DEPT) )
π (^) NAME (EMP * (σ (^) LOC ≠ ‘Ann Arbor’(DEPT) ) Natural join *
Relational Algebra 10
π (^) X.NAME, Y.NAME (σ (^) X.SAL > Y.SAL (X (^) Y.EMPNO = X.MGR Y))
5
Q10. For each employee whose salary exceeds his or her manager’s salary, list the employee’s name and the manager’s name.
π (^) X.NAME, Y.NAME (σ (^) X.SAL > Y.SAL and Y.EMPNO = X.MGR (X 5 Y))
Relational Algebra 11
Q11. Find the numbers of those departments that have employees who can do some job that is done by an employee in department D3.
Q12. Find the numbers of those departments that have employees who can do all the jobs that are done by an employee in department D3.
Relational Algebra 12
100 D3 electrician 200 D3 plumber 300 D3 electrician 400 D1 electrician 500 D1 plumber 600 D1 carpenter 700 D2 electrician 800 D2 carpenter 900 D2 electrician
Q11. Find the numbers of those departments that have employees who can do some job that is done by an employee in department D3. Answer: D1 and D
Relational Algebra 19
Q13. Find the average salary of all employees.
Examples (10): Functions and groups
Relational Algebra 20
Examples (10): Functions and groups continued
Q14. List the departments (DNO) and the average salary of each.
DNO
Relational Algebra 21
Examples (11): Outer joins
S(S#, SNAME, CITY) S1 Smith London S5 Adams Athens
P1 Nut London P3 Screw Rome
Q15a. For each part list all suppliers in the same city.
Q15b. For each part list all suppliers in the same city. For a part with no supplier in the city, list null.
P1 Nut S1 Smith London
natural join (inner join)
P1 Nut S1 Smith London P3 Screw?? Rome left outer join Relational Algebra 22
Examples (11): Outer joins continued
Q15c. For each part list all suppliers in the same city. For a supplier with no part in the city, list null.
Q15d. For each part list all suppliers in the same city. For a part with no supplier in the city, list null. For a supplier with no part in the city, list null.
( P#, PNAME, S#, SNAME, CITY) P1 Nut S1 Smith London ?? S5 Adams Athens
right outer join
( P#, PNAME, S#, SNAME, CITY) P1 Nut S1 Smith London P3 Screw?? Rome ?? S5 Adams Athens outer join
S1 Smith London S5 Adams Athens
P1 Nut London P3 Screw Rome
Relational Algebra 23
Examples (12): Recursive closure
Q 16. List all the superiors of EMPNO 500. 600, 950, 980
Q 17. List all those supervised by EMPNO 900. 700, 800, 100, 200, 300, 400
Can’t express these queries? WHY?
980
900 950
700 800 600 850
100 200 300 400 500
Relational Algebra 24
Examples (12): Recursive closure
Q 16. List all the superiors of EMPNO 500. 600, 950, 980
Q 17. List all those supervised by EMPNO 900. 700, 800, 100, 200, 300, 400
Can’t express these queries? WHY?
EMP( EMPNO, MGR,...) 100 700 200 700 300 800 400 800 500 600 600 950 700 900 800 900 850 950 900 980 950 980 980 980
Relational Algebra 25
Q 16. List all the superiors of EMPNO 500. SUPERIORS (EMPNO, MGR) 500 600 200 700 … 950 980 980 980 500 950 200 900 300 900 400 900 500 980 600 980 700 980 800 980 500 980 200 980 300 980 400 980
Initial table, EMP table.
Second addition. Join & Union.
First addition. Join & Union.
EMP( EMPNO, MGR,...) 100 700 200 700 300 800 400 800 500 600 600 950 700 900 800 900 850 950 900 980 950 980 980 980
Can’t express these queries? WHY? Relational Algebra 26
Relational Algebra 27