Beruflich Dokumente
Kultur Dokumente
Systems II
Query Operator & Algebraic
Expressions
Why SQL
Query Processing
SQL Query
SELECT pNumber, count(*) AS CNT
FROM Student
WHERE sNumber > 1
GROUP BY pNumber;
Query Plans
Query Example
SELECT B, D FROM
Answer
B
2
D
x
4
RXS
Bingo!
Got one...
SELECT B, D FROM
10
10
a
.
.
10
20
C
.
.
10
10
But ?
Performance
would be unacceptable!
We
Usual set operations, both operands have the same relation schema.
Relational Algebra
Express Query Plans
B,D
R.A=c S.E=2
R.C=S.C
X
R
S
10
11
Operator
Relational
SQL
SELECT pNumber, count(*) AS CNT
FROM Student
WHERE sNumber > 1
GROUP BY pNumber;
12
Relational Algebra
Basic operators
Binary Op.
R S
14
Binary Op.
Defined as:
R S = {t | t R and t S}
R-S S-R
RS
15
Binary Op.
R S
16
Unary Op.
Selection:
Select: c (R):
c is a condition on Rs attributes
Select subset of tuples from R that satisfy
selection condition c
(C 6) (R)
R
A
17
Selection: Example
R
(D > C) (R)
18
Unary Op.
Project:
A, C (R)
R
A
8
19
C,
VA, X C*3+B
(R)
R
A
17
22
23
26
20
RXS
R
21
Binary Op.
Natural Join: R S
Implicit condition
(R.B = S.B and R.D = S.D)
RS
22
Theta Join: R
Binary Op.
Recommendation:
Always use Theta join
(more explicit and more clear)
AR.A>=S.C
BS
R
2
23
Unary Op.
(R)
24
Unary Op.
sum(c)(R)
branch_name,sum(balance)(S)
26
Assignment Operator:
Series of assignments
Example:
R1 (
R2 R1 (R.A = T.C) T
Result R1 U R2
((A=B) ^ (D>5))
(R S)) W
27
Banking Example
28
Example Queries
29
Example Queries
30
31
Example Queries
32
Example Queries
For branches that gave loans > 100,000 or hold accounts with
balances >50,000, report the branch name along whether it is
reported because of a loan or an account
R1 branch_name, Loan As Type (amount >100,000 (loan))
R2 branch_name, Account As Type(balance > 50,000 (account)))
Result R1 U R2
33
Example Queries
34
Example Queries
35
Set operators
Joins
Duplicate elimination