Querylanguages PDF
Querylanguages PDF
1
Relational Algebra
A Retrieval DML Must Express Select Project
R
Criteria for selecting tuples for that result R R
S S S
qualifier
The relations that take part in the query Join Division
set generators *
2
σ Select Operator σ Select Operator
selects a subset of the tuples in a relation that
STUDENT
satisfy a selection condition studno name hons tutor year
s1 jones ca bush 2
s2 brown cis kahn 2
a boolean expression specified on the attributes
s3 smith cs goble 2
of a specified relation s4 bloggs ca goble 1
s5 jones cs zobel 1
a relation that • stands for the usual comparison operators ‘<‘, studno name hons tutor year
has the same ‘<>‘, ‘<=‘, ‘>‘, ‘>=‘, etc s4 bloggs ca goble 1
attributes as the • clauses can be arbitrarily connected with
source relation; boolean operators AND, NOT, OR
degree(result) = degree(relation);
σname=‘bloggs’(STUDENT)
card(result) <= card(relation)
3
Cartesian Product Example X Cartesian Product
STUDENT
STUDENT COURSE
studno name hons tutor year lecturer roomno
studno name hons tutor year s1 jones ca bush 2 kahn IT206
studno name courseno subject equip s1 jones ca bush 2 s1
s1
jones
jones
ca
ca
bush
bush
2
2
bush
goble
2.26
2.82
s1 jones cs250 prog sun s2 brown cis kahn 2 s1 jones ca bush 2 zobel 2.34
s1 jones ca bush 2 watson IT212
s2 brown cs150 prog sun s3 smith cs goble 2 s1 jones ca bush 2 woods IT204
s6 peters cs390 specs sun s4 bloggs ca goble 1 s1 jones ca bush 2 capon A14
s1 jones ca bush 2 lindsey 2.10
s5 jones cs zobel 1 s1 jones ca bush 2 barringer 2.125
s2 brown cis kahn 2 kahn IT206
s6 peters ca kahn 3
STUDENT X COURSE s2
s2
brown
brown
cis
cis
kahn
kahn
2
2
bush
goble
2.26
2.82
studno name courseno subject equip s2 brown cis kahn 2 zobel 2.34
STAFF s2 brown cis kahn 2 watson IT212
s1 jones cs250 prog sun s2 brown cis kahn 2 woods IT204
s1 jones cs150 prog sun lecturer roomno s2 brown cis kahn 2 capon A14
s2 brown cis kahn 2 lindsey 2.10
s1 jones cs390 specs sun kahn IT206 s2 brown cis kahn 2 barringer 2.125
s2 brown cs250 prog sun bush 2.26 s3 smith cs goble 2 kahn IT206
s3 smith cs goble 2 bush 2.26
s2 brown cs150 prog sun goble 2.82 s3 smith cs goble 2 goble 2.82
s2 brown cs390 specs sun zobel 2.34 s3
s3
smith
smith
cs
cs
goble
goble
2
2
zobel
watson
2.34
IT212
s6 peters cs250 prog sun watson IT212 s3 smith cs goble 2 woods IT204
s6 peters cs150 prog sun s3 smith cs goble 2 capon A14
woods IT204 s3 smith cs goble 2 lindsey 2.10
s6 peters cs390 specs sun capon A14 s3 smith cs goble 2 barringer 2.125
s4 bloggs ca goble 1 kahn IT206
lindsey 2.10 s4 bloggs ca goble 1 bush 2.26
barringer 2.125
ENROL
More joins stud course lab exam
no no mark mark
STUDENT s1 cs250 65 52 Natural Join Operator
studno name hons tutor year s1 cs260 80 75
Of all the types of θ-join, the equi-join is the
s1 jones ca bush 2 s1 cs270 47 34
s2 brown cis kahn 2 s2 cs250 67 55
s3 smith cs goble 2 s2 cs270 65 71 only one that yields a result in which the
s4 bloggs ca goble 1 s3 cs270 49 50
s5 jones cs zobel 1 s4 cs280 50 51 compared columns are redundant to each
s6 peters ca kahn 3 s5 cs250 0 3 other—possibly different names but same
s6 cs250 2 7
values
The natural join is an equi-join but one of the
stud name hons tutor year stud course lab exam
no no no mark mark redundant columns (simple or composite) is
s1 jones ca bush 2 s1 cs250 65 52
s1 jones ca bush 2 s1 cs260 80 75 omitted from the result
s1 jones ca bush 2 s1 cs270 47 34
s2 brown cis kahn 2 s2 cs250 67 55 Relational join is the principle algebraic
s2 brown cis kahn 2 s2 cs270 65 71
s3 smith cs goble 2 s3 cs270 49 50 counterpart of queries that involve the
s4
s5
bloggs
jones
ca
cs
goble
zobel
1
1
s4
s5
cs280
cs250
50
0
51
3 existential quantifier ∃
s6 peters ca kahn 3 s6 cs250 2 7
4
Self Join: Joins on the same relation Exercise
π (lecturer, (staff (appraiser = lecturer) staff) Get student’s name, all their courses, subject
roomno,appraiser, approom) of course, labmark for course, lecturer of
course and lecturer’s roomno for ‘ca’ students
STAFF
lecturer roomno appraiser
lecturer roomno appraiser approom
kahn IT206 watson
bush 2.26 capon
kahn IT206 watson IT212 University Schema
bush 2.26 capon A14
goble 2.82 capon goble 2.82 capon A14 STUDENT(studno,name,hons,tutor,year)
zobel 2.34 watson zobel 2.34 watson IT212
watson IT212 barringer watson IT212 barringer 2.125 ENROL(studno,courseno,labmark,exammark)
woods IT204 barringer woods IT204 barringer 2.125
capon A14 watson capon A14 watson IT212 COURSE(courseno,subject,equip)
lindsey 2.10 woods lindsey 2.10 woods IT204
barringer 2.125 null STAFF(lecturer,roomno,appraiser)
TEACH(courseno,lecturer)
select e.lecturer, e.roomno, m.lecturer appraiser, m.roomno approom
from staff e, staff m YEAR(yearno,yeartutor)
where e.appraiser = m.lecturer
lecturer
lindsey
Lecturers π(lecturer)TEACH capon The result R1 ∩ R2 is a relation that includes
kahn only those tuples in R1 that also appear in R2
Tutors π(tutor)STUDENT bush
zobel The resulting relation might have the same
Lecturers ∪ Tutors woods
capon attribute names as the first or the second
goble relation
5
Retrieve all staff that lecture and tutor − Difference Operator
STUDENT TEACH
studno name
s1 jones
hons
ca
tutor
bush
year
2
courseno
cs250
lecturer
lindsey
Definition:
s2 brown cis kahn 2 cs250 capon The difference of two relations
cs260 kahn
s3
s4
smith
bloggs
cs
ca
goble
goble
2
1 cs260 bush
R1(A1,A2,...,An) and R2(B1,B2,...,Bm) is a
s5 jones cs zobel 1 cs270 zobel relation R3(C1,C2,...,Cn) such that
dom(Ci)= dom(Ai) −dom (Bi) for 1 ≤ i ≤ n
s6 peters ca kahn 3 cs270 woods
cs280 capon
Retrieve all staff that lecture but don’t Outer Join Operation
tutor
In an equi-join, tuples without a ‘match’ are
STUDENT TEACH
studno name hons tutor year courseno lecturer eliminated
cs250 lindsey
s1 jones ca bush 2 Outer join keeps all tuples in R1 or R2 or both
s2 brown cis kahn 2 cs250 capon
s3 smith cs goble 2 cs260 kahn in the result, padding with nulls
s4 bloggs ca goble 1 cs260 bush
cs270 zobel
Left outer join R1 R2
s5 jones cs zobel 1
s6 peters ca kahn 3 cs270 woods keeps every tuple in R1
cs280 capon select * from R1, R2 where R1.a = R2.a (+)
Right outer join R1 R2
Lecturers π(lecturer)TEACH lecturer keeps every tuple in R2
lindsey
Tutors π(tutor)STUDENT capon
select * from R1, R2 where R1.a (+) = R2.a
woods Double outer join R1 R2
Lecturers -Tutors keeps every tuple in R1 and R2
select * from R1, R2 where R1.a (+) = R2.a (+)
6
Outer Self Join
π (lecturer, (staff (appraiser = lecturer) staff) Outer Union
roomno,appraiser, approom) Takes the union of tuples from two relations
STAFF lecturer roomno appraiser approom that are not union compatible
lecturer roomno appraiser kahn IT206 watson IT212
kahn IT206 watson bush 2.26 capon A14 The two relations, R1 and R2, are partially
bush 2.26 capon goble 2.82 capon A14 compatible—only some of their attributes are
goble 2.82 capon zobel 2.34 watson IT212
zobel 2.34 watson watson IT212 barringer 2.125 union compatible
watson IT212 barringer woods IT204 barringer 2.125
woods IT204 barringer capon A14 watson IT212 The attributes that are not union compatible
capon A14 watson lindsey 2.10 woods IT204
lindsey 2.10 woods
from either relation are kept in the result and
barringer 2.125 null null
barringer 2.125 null tuples without values for these attributes are
padded with nulls
select e.lecturer, e.roomno, m.lecturer appraiser, m.roomno approom
from staff e, staff m
where e.appraiser = m.lecturer (+)
default is ascending
7
Aggregation Functions Aggregation Functions in SQL
Aggregation functions on collections of data values:
ENROL select studno, count(*),
average, minimum, maximum, sum, count stud course lab exam avg(labmark)
Group tuples by value of an attribute and apply no no mark mark
s1 cs250 65 52 from enrol
aggregate function independently to each group of tuples s1 cs260 80 75 group by studno
s1 cs270 47 34
s2 cs250 67 55
ENROL <grouping attributes> ƒ <function list> (relation name) s2 cs270 65 71
stud course lab exam s3 cs270 49 50
no no mark mark studno ƒ COUNT courseno (ENROL)
s4 cs280 50 51
studno count avg
s1 cs250 65 52 s5 cs250 0 3 s1 3 64
s1 cs260 80 75 s6 cs250 2 7 s2 2 66
s1 cs270 47 34 studno count
s3 1 49
s1 3
s2 cs250 67 55 s4 1 50
s2 2 s5 1 0
s2 cs270 65 71
s3 1 s6 1 2
s3 cs270 49 50
s4 1
s4 cs280 50 51 s5 1
s5 cs250 0 3 s6 1
s6 cs250 2 7
8
Nested subqueries set comparison
Subqueries
operators
May be used in these situations:
Outer query qualifier includes a value v
compared with a bag of values V generated to define the set of rows to be inserted in the
from a subquery target table of an insert, create table
or copy command
Comparison v with V evaluates TRUE
to define one or more values to be assigned
v in V if v is one of the elements V
to existing rows in an update statement
v = any V if v equal to some value in V
v > any V if v > some value in V (same for <) to provide values for comparison in where,
v > all V if v greater than all the values in V having and start with clauses in
(same for <) select, update and delete commands
9
Conclusions on SQL
Retrieval:
many ways to achieve the same
result—though different performance costs
Comprehensive and powerful facilities
Non-procedural
10