Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Relational Algebra
Relational Algebra
Relational Algebra
Last
Smith
Green
Keen
Jones
Age
22
28
23
32
First
Last
Age
Forrest
Gump
36
Sally
Green
28
DonJuan DeMarco 27
Union: RUS
Last
Age
Bill
Smith
22
Sally
Green
28
Mary
Keen
23
Tony
Jones
32
Forrest
Gump
36
DonJuan DeMarco 27
6
Difference: R - S
Last
Age
Bill
Smith
22
Mary
Keen
23
Tony
Jones
32
Intersection: RS
Last
Green
Age
28
10
Additional properties
Cartesian Product: RS
S
Last
Smith
Keen
Jones
Age
22
23
32
Dinner
Dessert
Steak
Ice Cream
Lobster
Cheesecake
12
Cartesian Product: RS
RS:
First
Last
Age
Dinner
Dessert
Bill
Smith
22
Steak
Ice Cream
Bill
Smith
22
Lobster
Cheesecake
Mary
Keen
23
Steak
Ice Cream
Mary
Keen
23
Lobster
Cheesecake
Tony
Jones
32
Steak
Ice Cream
Tony
Jones
32
Lobster
Cheesecake
13
Relational Algebra
14
Selection Operator
15
Selection Operator
Selection Operator
Comparison operators:
Logical operators:
T F
T F
T T
T T
F F
F T
F T
17
Selection Examples
Office
400
220
160
420
500
Dept
CS
Econ
Econ
CS
Fin
Rank
Assistant
Adjunct
Assistant
Associate
Associate
18
Selection Examples
19
Selection Examples
400
CS
Rank
Assistant
20
Selection Examples
Rank
Smith
400
CS
Assistant
Jones
220
Econ Adjunct
Green
160
Econ Assistant
21
Selection Examples
22
Projection Operator
23
Projection Operator
24
Projection Examples
Dept
CS
Econ
Econ
CS
Fin
25
26
Results:
Name
Green
Smith
Rank
Assistant
Associate
27
Aggregate Functions
SUM
MINIMUM
MAXIMUM
AVERAGE, MEAN, MEDIAN
COUNT
28
Aggregate Functions
Office
400
220
160
420
500
Dept
CS
Econ
Econ
CS
Fin
Salary
45000
35000
50000
65000
60000
29
30
31
32
33
Join Operation
Join Operation
35
Join Examples
Name
Smith
Jones
Green
Brown
Smith
Office
400
220
160
420
500
Dept
CS
Econ
Econ
CS
Fin
Salary
45000
35000
50000
65000
60000
Dept
CS
Econ
Fin
Hist
MainOffice
404
200
501
100
Phone
555-1212
555-1234
555-4321
555-9876
36
Join Examples
37
Join Examples
EMP
emp.Dept = depart.Dept
DEPART
Name
Office
EMP.Dept
Phone
Smith
400
CS
45000
CS
404
555-1212
Jones
220
Econ
35000
Econ
200
555-1234
Green
160
Econ
50000
Econ
200
555-1234
Brown
420
CS
65000
CS
404
555-1212
Smith
500
Fin
60000
Fin
501
555-4321
38
Join Examples
39
Join Examples
EMP
depart.dept)
DEPART
Name
Office
EMP.Dept
Phone
Smith
400
CS
45000
CS
404
555-1212
Green
160
Econ
50000
Econ
200
555-1234
Smith
500
Fin
60000
Fin
501
555-4321
40
Natural Join
Name
Smith
Jones
Green
Brown
Smith
Office
400
220
160
420
500
Dept
CS
Econ
Econ
CS
Fin
Salary
45000
35000
50000
65000
60000
MainOffice
404
200
200
404
501
Phone
555-1212
555-1234
555-1234
555-1212
555-4321
42
Outer Join
43
S
(Left) outer join is a join in which tuples from R that
do not have matching values in common columns of
S are also included in result relation.
Name
Alice
Bill
Carl
Dina
Age
21
24
23
19
Food
Hamburger
Pizza
Beer
Shrimp
Food
Pizza
Hamburger
Chicken
Pasta
Tacos
Day
Monday
Tuesday
Wednesday
Thursday
Friday
45
PEOPLE
Name
Alice
Bill
Carl
Dina
Age
21
24
23
19
people.food = menu.food
people.Food
Hamburger
Pizza
Beer
Shrimp
MENU
menu.Food
Hamburger
Pizza
NULL
NULL
Day
Tuesday
Monday
NULL
NULL
46
PEOPLE
Name
Bill
Alice
NULL
NULL
NULL
Age
24
21
NULL
NULL
NULL
people.food = menu.food
people.Food
Pizza
Hamburger
NULL
NULL
NULL
MENU
menu.Food
Pizza
Hamburger
Chicken
Pasta
Tacos
Day
Monday
Tuesday
Wednesday
Thursday
Friday
47
PEOPLE
people.food = menu.food
Name
Alice
Bill
Carl
Dina
NULL
NULL
NULL
people.Food
Hamburger
Pizza
Beer
Shrimp
NULL
NULL
NULL
Age
21
24
23
19
NULL
NULL
NULL
MENU
menu.Food
Hamburger
Pizza
NULL
NULL
Chicken
Pasta
Tacos
Day
Tuesday
Monday
NULL
NULL
Wednesday
Thursday
Friday
48
SELECT statement
SELECT [DISTINCT | ALL]
{* | [columnExpression [AS newName]] [,...] }
FROM
TableName [alias] [, ...]
[WHERE condition]
[GROUP BY
columnList] [HAVING condition]
[ORDER BY
columnList]
49
SELECT Statement
FROM
WHERE
GROUP BY
HAVING
SELECT
ORDER BY
50
Projection
Example: The table E (for EMPLOYEE)
nr name salary
1 John
100
5 Sarah 300
7 Tom
100
51
SQL
Result
Relational algebra
salary
select distinct
salary from E
100
300
nr
select nr,
salary from E
salary(E)
salary
100
300
100
nr, salary(E)
52
SQL
select * from
E where salary
< 200
select * from
E where salary
< 200 and
nr >= 7
Result
nr
name salary
John
100
Tom
100
nr
name salary
Tom
100
Relational algebra
53
Result
Relational algebra
name salary
select name,
salary from
E where
salary < 200
John
100
Tom
100
name, salary (
< 200(E))
salary
54
Cartesian Product
table E (for EMPLOYEE)
enr ename edept
1
Bill
A
2
Sarah
C
3
John
A
55
Cartesian Product
SQL
select
*from E,
D
Relational
algebra
Result
enr ename edept
dnr
dname
Bill
Marketing
Bill
Sales
Bill
Legal
Sarah
Marketing
Sarah
Sales
Sarah
Legal
John
Marketing
John
Sales
John
Legal
ED
56
Relational
algebra
Result
enr ename
dept
dnr
Bill
Marketing
Sarah
Legal
John
select *
2
from E, D 3
where
dept = dnr
dname
dept = dnr (E D)
57
Aggregate functions
name
John
Sarah
Tom
Anne
salary
100
300
100
null
dept
A
C
A
C
58
Sum
SQL
select
sum(salary)
from E
Result
sum
500
Relational algebra
Fsum(salary)(E)
Count:
Count
SQL
Result
select
count(salary)
from E
count
3
select
count(distinct
salary)from E
count
2
Relational algebra
Fcount(salary)(E)
Fcount(salary)(salary(E))
60
Aggregate Functions
Result
dept
select
sum(salary)
from E group
by dept
Relational algebra
sum
200
300
deptFsum(salary)(E)
61
Aggregate Functions
select
sum(salary),
count(*)from
E group by
dept
Relational
algebra
Result
dept
sum
count
200
300
deptFsum(salary),
count(*)(E)
62
Outer join
ename
Bill
Sarah
John
dept
A
B
A
dnr
dname
A
Marketing
B
Sales
C
Legal
Outer join
64
Outer join
SQL
select *
from (E
right outer
join D on
dept = dnr)
Relational
algebra
Result
enr
ename
dept
dnr
dname
Bill
Marketing
Sarah
Sales
John
Marketing
null
null
null
Legal
edept = dnr
65
Outer Join
SQL
Result
dnr
select dnr,
dname,
count(*)from
(E right outer
join D on dept
= dnr)group by
dnr, dname
dname
Relational algebra
count
Marketing 2
Sales
Legal
dnr, dnameFcount(*)(E
dnr
dept =
D)
66
Outer Join
SQL
select dnr,
dname,
count(enr)from
(E right outer
join D on edept
= dnr) group by
dnr, dname
Result
dnr
dname
Relational algebra
count
Marketing 2
Sales
Legal
dnr, dnameFcount(enr)(E
dept = dnr
D)
67