exercise 3 - cse.chalmers.se · the dbms uses relational algebra a dbms may have many different...
TRANSCRIPT
![Page 1: Exercise 3 - cse.chalmers.se · The DBMS uses relational algebra A DBMS may have many different ways of implementing the relational algebra operations. The aim of query optimization](https://reader034.vdocuments.mx/reader034/viewer/2022052518/5f09f0147e708231d4293975/html5/thumbnails/1.jpg)
Exercise 3
SQL queries
Relational algebra
![Page 2: Exercise 3 - cse.chalmers.se · The DBMS uses relational algebra A DBMS may have many different ways of implementing the relational algebra operations. The aim of query optimization](https://reader034.vdocuments.mx/reader034/viewer/2022052518/5f09f0147e708231d4293975/html5/thumbnails/2.jpg)
Example
SELECT name FROM Courses WHERE nrStudents > 20
name(nrStudents>20(Courses))
Courses
Code Name nrStudents
AB123 Math 19
CD234 Physics 24
EF345 Karaoke 23
GH456 PERL 20
Physics
Karaoke
![Page 3: Exercise 3 - cse.chalmers.se · The DBMS uses relational algebra A DBMS may have many different ways of implementing the relational algebra operations. The aim of query optimization](https://reader034.vdocuments.mx/reader034/viewer/2022052518/5f09f0147e708231d4293975/html5/thumbnails/3.jpg)
The most common question
![Page 4: Exercise 3 - cse.chalmers.se · The DBMS uses relational algebra A DBMS may have many different ways of implementing the relational algebra operations. The aim of query optimization](https://reader034.vdocuments.mx/reader034/viewer/2022052518/5f09f0147e708231d4293975/html5/thumbnails/4.jpg)
The DBMS uses relational algebra
A DBMS may have many different ways of implementing the relational algebra operations.
The aim of query optimization is to choose the most efficient one.
To do this, it uses formulae that estimate the costs for a number of options and selects the one with the lowest cost.
![Page 5: Exercise 3 - cse.chalmers.se · The DBMS uses relational algebra A DBMS may have many different ways of implementing the relational algebra operations. The aim of query optimization](https://reader034.vdocuments.mx/reader034/viewer/2022052518/5f09f0147e708231d4293975/html5/thumbnails/5.jpg)
Projection
Which attributes
Which columns
SELECT A,C,E
A,C,E
A B C D E F
![Page 6: Exercise 3 - cse.chalmers.se · The DBMS uses relational algebra A DBMS may have many different ways of implementing the relational algebra operations. The aim of query optimization](https://reader034.vdocuments.mx/reader034/viewer/2022052518/5f09f0147e708231d4293975/html5/thumbnails/6.jpg)
Selection
Which tuples
Which rows
WHERE E > 5
E > 5
DON’T confuse SELECT (projection) for selection!!
A B C D E F
4
7
5
8
3
![Page 7: Exercise 3 - cse.chalmers.se · The DBMS uses relational algebra A DBMS may have many different ways of implementing the relational algebra operations. The aim of query optimization](https://reader034.vdocuments.mx/reader034/viewer/2022052518/5f09f0147e708231d4293975/html5/thumbnails/7.jpg)
SQL vs. Relational Algebra
SELECT X
FROM T
WHERE C
GROUP BY Y
HAVING D
ORDER BY Z
Z(X(D(Y(C(T)))))
![Page 8: Exercise 3 - cse.chalmers.se · The DBMS uses relational algebra A DBMS may have many different ways of implementing the relational algebra operations. The aim of query optimization](https://reader034.vdocuments.mx/reader034/viewer/2022052518/5f09f0147e708231d4293975/html5/thumbnails/8.jpg)
Combining tables
Set (actually bag) operations
Cartesian product
Joins
![Page 9: Exercise 3 - cse.chalmers.se · The DBMS uses relational algebra A DBMS may have many different ways of implementing the relational algebra operations. The aim of query optimization](https://reader034.vdocuments.mx/reader034/viewer/2022052518/5f09f0147e708231d4293975/html5/thumbnails/9.jpg)
Set (actually bag) operations
R= S=
R UNION ALL SR S
R INTERSECT SR S
R MINUS SR - S
A B
1 2
3 4
A B
1 2
5 6
A B
1 2
3 4
1 2
5 6
A B
1 2
A B
3 4
![Page 10: Exercise 3 - cse.chalmers.se · The DBMS uses relational algebra A DBMS may have many different ways of implementing the relational algebra operations. The aim of query optimization](https://reader034.vdocuments.mx/reader034/viewer/2022052518/5f09f0147e708231d4293975/html5/thumbnails/10.jpg)
Joins
3 ”basic” joins:
–Cartesian product
–Conditional join
Theta join, Inner join, Equi join, Nonequi join, Natural join
–Outer join
![Page 11: Exercise 3 - cse.chalmers.se · The DBMS uses relational algebra A DBMS may have many different ways of implementing the relational algebra operations. The aim of query optimization](https://reader034.vdocuments.mx/reader034/viewer/2022052518/5f09f0147e708231d4293975/html5/thumbnails/11.jpg)
Cartesian product
R= S=
R , S
R S
A B
1 2
3 4
C D
5 6
7 8
R.A R.B S.C S.D
1 2 5 6
1 2 7 8
3 4 5 6
3 4 7 8
![Page 12: Exercise 3 - cse.chalmers.se · The DBMS uses relational algebra A DBMS may have many different ways of implementing the relational algebra operations. The aim of query optimization](https://reader034.vdocuments.mx/reader034/viewer/2022052518/5f09f0147e708231d4293975/html5/thumbnails/12.jpg)
Conditional join = Inner join
R= S=
R,S
WHERE C
or
R JOIN S ON C
R C S
If C is ”R.A = S.B”
If C is equality –Equi join
If C is inequality –Nonequi join
Beware of NULL!
A B
1 2
3 4
B C
3 4
4 5
R.A R.B S.B S.C
3 4 3 4
![Page 13: Exercise 3 - cse.chalmers.se · The DBMS uses relational algebra A DBMS may have many different ways of implementing the relational algebra operations. The aim of query optimization](https://reader034.vdocuments.mx/reader034/viewer/2022052518/5f09f0147e708231d4293975/html5/thumbnails/13.jpg)
Special case – Natural join
R= S=
R NATURAL JOIN S
R S
A B
1 2
3 4
B C
4 5
6 7
R.A B S.C
3 4 5
![Page 14: Exercise 3 - cse.chalmers.se · The DBMS uses relational algebra A DBMS may have many different ways of implementing the relational algebra operations. The aim of query optimization](https://reader034.vdocuments.mx/reader034/viewer/2022052518/5f09f0147e708231d4293975/html5/thumbnails/14.jpg)
Outer join
R= S=
R FULL/LEFT/RIGHT
OUTER JOIN S ON C
All rows in both/left/right
table(s) will appear, and the
rest will be filled with null if
C does not match.
R FULL OUTER JOIN S
ON R.B = S.C
A B
1 2
3 4
C D
4 5
6 7R.A R.B S.C S.D
1 2 NULL NULL
3 4 4 5
NULL NULL 6 7
![Page 15: Exercise 3 - cse.chalmers.se · The DBMS uses relational algebra A DBMS may have many different ways of implementing the relational algebra operations. The aim of query optimization](https://reader034.vdocuments.mx/reader034/viewer/2022052518/5f09f0147e708231d4293975/html5/thumbnails/15.jpg)
There is more…
Grouping
Renaming
Sorting
Also note that the terminology regarding joins is
confused.
Inner join = Equi join (orafaq.com)
Inner join = Conditional join (Wikipedia)
Theta join = Conditional join (Course book)