Professional Documents
Culture Documents
Úvod Do Databázových Systém U: Radim Ba Ca
Úvod Do Databázových Systém U: Radim Ba Ca
Radim Bača
radim.baca@vsb.cz
dbedu.cs.vsb.cz
Relational algebra
SQL
Inner Join
Self Join
Outer Join
Relational algebra
Selection σcondition
Projection π
Example
Employee
Department
Example
Employee
Department
Example
Employee
σsales>10
Department
Example
Employee
σsales>10 πemployee.name
Department
Result
Restriction (selection)
Restriction (selection)
Projection
Projection
Duplications
Duplications
Cartesian Product
Cartesian Product
⇓ Employee × Department
eID eName position dID dID dName sales
Combination of Operations
Employee Department
eID eName position dID dID dName sales
Combination of Operations
Employee Department
eID eName position dID dID dName sales
Combination of Operations
Employee Department
eID eName position dID dID dName sales
Combination of Operations
Employee Department
eID eName position dID dID dName sales
Natural Join
Join Operation
R ∪S R ∩S R −S
Intersection
Final Notes
SQL
SQL
SELECT A1 , ... , An
FROM R1 , ... , Rm
WHERE condition
SELECT A1 , ... , An
FROM R1 , ... , Rm
WHERE condition
m
πA1 ,...,An (σcondition (R1 × · · · × Rm ))
SELECT A1 , ... , An
FROM R1 , ... , Rm
WHERE condition
m
πA1 ,...,An (σcondition (R1 × · · · × Rm ))
SELECT A1 , ... , An
FROM R1
JOIN R2 ON join_condition - Theta join between tables
WHERE condition
m
πA1 ,...,An (σcondition (R1 ./join_condition R2 ))
SELECT A1 , ... , An
FROM R1
JOIN R2 ON join_condition - Theta join between tables
WHERE condition
m
πA1 ,...,An (σcondition (R1 ./join_condition R2 ))
Sakila Database
Database model
Student Studies
Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
Student Studies
Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
Student Studies
stID Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
Student Studies
stID Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
Student Studies
stID Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
Student Studies
stID Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
Student Studies
stID Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
Student Studies
stID Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
Student Study
sID name birth_date sID pID year points
SELECT distinct pID
1 Petr 1990 1 35 2010 23
FROM Student 2 Pavel 1991 1 35 2011 55
3 Ivana 1990 1 21 2010 89
JOIN Study On Student.sID = Study.sID 2 46 2010 59
WHERE year = 2011 and name = ’Petr’
πpID
Student ./ Study
Student Study
sID name birth_date sID pID year points
SELECT distinct pID
1 Petr 1990 1 35 2010 23
FROM Student 2 Pavel 1991 1 35 2011 55
3 Ivana 1990 1 21 2010 89
JOIN Study On Student.sID = Study.sID 2 46 2010 59
WHERE year = 2011 and name = ’Petr’
πpID
Student_Study
σyear =2011 ∧ name=0 Petr 0
sID name birth_date sID pID year points
Student ./ Study 1 Petr 1990 1 35 2010 23
1 Petr 1990 1 35 2011 55
1 Petr 1990 1 21 2010 89
2 Pavel 1991 2 46 2010 59
Student Study
sID name birth_date sID pID year points
SELECT distinct pID
1 Petr 1990 1 35 2010 23
FROM Student, Study 2 Pavel 1991 1 35 2011 55
3 Ivana 1990 1 21 2010 89
JOIN Study On Student.sID = Study.sID 2 46 2010 59
WHERE year = 2011 and name = ’Petr’
πpID
Student_Study
σyear =2011 ∧ name=0 Petr 0
sID name birth_date sID pID year points
Student ./ Study 1 Petr 1990 1 35 2010 23
1 Petr 1990 1 35 2011 55
1 Petr 1990 1 21 2010 89
2 Pavel 1991 2 46 2010 59
Student Study
sID name birth_date sID pID year points
SELECT distinct pID
1 Petr 1990 1 35 2010 23
FROM Student, Study 2 Pavel 1991 1 35 2011 55
3 Ivana 1990 1 21 2010 89
JOIN Study On Student.sID = Study.sID 2 46 2010 59
WHERE year = 2011 and name = ’Petr’
πpID
Student_Study Duplication!
σyear =2011 ∧ name=0 Petr 0
sID name birth_date sID pID year points
Student ./ Study 1 Petr 1990 1 35 2010 23
1 Petr 1990 1 35 2011 55
1 Petr 1990 1 21 2010 89
2 Pavel 1991 2 46 2010 59
Student_Study
sID name birth_date sID pID year points
SELECT distinct pID
1 Petr 1990 1 35 2010 23
FROM Student, Study 1 Petr 1990 1 35 2011 55
1 Petr 1990 1 21 2010 89
JOIN Study On Student.sID = Study.sID 2 Pavel 1991 2 46 2010 59
πpID
Student_Study
sID name birth_date sID pID year points
SELECT distinct pID
1 Petr 1990 1 35 2011 55
FROM Student, Study
πpID
35
SELECT *
FROM A A B
JOIN B ON A.k = B.k
SELECT *
FROM A A B
JOIN B ON A.k = B.k
SELECT *
FROM A A B
JOIN B ON A.k = B.k
Note that set B is usually smaller (i.e. the visualization is not very
accurate)
Student Studies
Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
Student Studies
stID Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
Student Studies
stID suID Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
Student Studies
stID suID Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
Student Studies
stID suID Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
Student birth_year
Student
P stID P stID
name name
birth_year birth_year
Student birth_year
Student
P stID P stID
name name
birth_year birth_year
Student birth_year
Student
P stID P stID
name name
birth_year birth_year
Outer Join
Student Study
sID pID year points
sID name birth_date
1 35 2010 23
1 Petr 1990
1 35 2011 55
2 Pavel 1991
1 21 2010 89
3 Ivana 1990
2 46 2010 59
Student Studies
Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
Student Studies
stID Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
Student Studies
stID Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
Student Studies
stID Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
Student Studies
stID Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
Student Studies
stID Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
Student Studies
stID Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
1 FROM
2 JOIN
Semantic order
3 WHERE
Not a query processing order!
4 SELECT
5 ...
SELECT *
FROM A
LEFT JOIN B A B
ON A.k = B.k
SELECT *
FROM A A B
FULL JOIN B
ON A.k = B.k
Example: Difference
Student Studies
stID Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
Example: Difference
Student Studies
stID Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
SELECT name
FROM Student
LEFT JOIN Studies ON
Student.stID = Studies.stID
WHERE Studies.stID IS NULL
We will show other ways how to implement the difference
later
UDBS 2019 -37- 2019-2020
SQL . Outer Join
Example: Difference
Student Studies
stID Subject
P stID PF stID
P suID
PF suID
name name
P year
birth_year study_year
gained_points
SELECT name
FROM Student
LEFT JOIN Studies ON
Student.stID = Studies.stID
WHERE Studies.stID IS NULL
We will show other ways how to implement the difference
later
UDBS 2019 -37- 2019-2020
Reference
References