Sie sind auf Seite 1von 36

CS4432: Database

Systems II
Query Operator & Algebraic
Expressions

Why SQL

SQL is a very-high-level language.

Say what to do rather than how to do it.


Avoid a lot of data-manipulation details needed in
procedural languages like C++ or Java.

Database management system figures out


best way to execute query.

Called query optimization.

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

R, S WHERE R.A = c and S.E = 2 and R.C=S.C

Answer

B
2

D
x
4

How do we execute query?


One idea
- Form Cartesian product of all tables in FROM-clause
- Select tuples that match WHERE-clause
- Project columns that occur in SELECT-clause

RXS

R.A R.B R.C S.C S.D S.E

Bingo!
Got one...
SELECT B, D FROM

10

10

a
.
.

10

20

C
.
.

10

10

R, S WHERE R.A = c and S.E = 2 and R.C=S.C


6

But ?
Performance

would be unacceptable!

We

need a better approach for reasoning


about queries, their execution orders and
their respective costs

Formal Relational Query Languages

Relational Algebra: More operational, very useful for


representing execution plans.

Operators working on relations

Core Relational Algebra


(Recap)

Union, intersection, and difference.

Usual set operations, both operands have the same relation schema.

Selection: picking certain rows.

Projection: picking certain columns.

Products and joins: compositions of relations.

Renaming of relations and attributes.

Grouping and Aggregation: Grouping matching tuples

Duplicate Elimination: eliminates identical copies except one

Sorting: Orders tuples based on a given criteria


9

Relational Algebra
Express Query Plans
B,D

R.A=c S.E=2
R.C=S.C

X
R

S
10

Recap on Relational Algebra &


Operators

11

Algebra Behind the Query


Language
Relational Algebra
Set

of operators that operate on relations

Operator

semantics based on Set or Bag theory

Relational

algebra form underlying basis (and optimization rules) for

SQL
SELECT pNumber, count(*) AS CNT
FROM Student
WHERE sNumber > 1
GROUP BY pNumber;
12

Relational Algebra

Basic operators

Set Operations (Union: , Intersection: ,difference: )


Select:
Project:
Cartesian product: x
rename:

More advanced operators, e.g., grouping and joins

The operators take one or two relations as inputs and produce a


new relation as an output

One input unary operator, two inputs binary operator


13

Binary Op.

Union over sets:


Consider

two relations R and S that are


union-compatible (same schema)
R

R S

14

Difference over sets:

Binary Op.

R S are the tuples that appear in R and not in S


R & S must be union-compatible

Defined as:

R S = {t | t R and t S}

R-S S-R
RS

15

Binary Op.

Intersection over sets:

Consider two Relations R and S that are union-compatible

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

((A=B) ^ (D>5)) (R)

(D > C) (R)

18

Unary Op.

Project:

A1, A2, , An (R), with A1, A2, , An attributes AR

returns all tuples in R, but only columns A1, A2, , An

A1, A2, , An are called Projection List

A, C (R)

R
A

8
19

Extended Projection: L (R)


Example
Rename column A to V

C,

Compute this expression


and call it X

VA, X C*3+B

(R)

R
A

17

22

23

26
20

Cross Product (Cartesian


Product): X
Binary Op.

Each tuple in R joined with each tuple is S


R x S = {t q | t R and q S}

RXS
R

21

Binary Op.

Natural Join: R S

An implicit equality condition on the common columns

Implicit condition
(R.B = S.B and R.D = S.D)

RS

22

Theta Join: R

Binary Op.

A join based on any arbitrary condition C


It is defined as :
R C S = (C (R X S))
R

Recommendation:
Always use Theta join
(more explicit and more clear)

AR.A>=S.C
BS

R
2

23

Unary Op.

Duplicate Elimination: (R)

Delete all duplicate records

Convert a Bag (allows duplicates) to a Set (does not allow


duplicates)

(R)

24

Unary Op.

Grouping & Aggregation operator:

Grouing & Aggregate operation in relational algebra


g1,g2, gm, F1(A1), F2(A2), Fn(An) (R)

Group by these columns


(can be empty)

Aggregation functions applied


over each group
avg: average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
25

Grouping & Aggregation


Operator: Example
R

sum(c)(R)

branch_name,sum(balance)(S)

26

Assignment Operator:

Write query as a sequence of line consisting of:

Series of assignments

Result expression containing the final answer

May use a variable multiple times in subsequent expressions

Example:

R1 (

R2 R1 (R.A = T.C) T

Result R1 U R2

((A=B) ^ (D>5))

(R S)) W

27

Banking Example

branch (branch_name, branch_city, assets)

customer (customer_name, customer_street, customer_city)

account (account_number, branch_name, balance)

loan (loan_number, branch_name, amount)

depositor (customer_name, account_number)

borrower (customer_name, loan_number)

28

Example Queries

Find customer names having account balance below 100


or above 10,000
customer_name (depositor
account_number(balance <100 OR balance > 10,000 (account)))

29

Example Queries

30

Example Queries (Contd)

31

Example Queries

Find customers names who have neither accounts nor


loans
customer_name(customer) (customer_name(borrower) U customer_name(depositer))

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

Find customer names having loans with sum > 20,000

customer_name (sum > 20,000 (customer_name, sum sum(amount)(loan borrower)))

34

Example Queries

Find the branch name with the largest number of accounts


R1 branch_name, countAccounts count(account_number)(account)
R2 Max max(countAccounts)(R1)
Result branch_name(R1 countAccounts = Max R2)

35

Summary of Relational-Algebra Operators

Set operators

Union, Intersection, Difference

Selection & Projection & Extended Projection

Joins

Natural, Theta, Outer join

Rename & Assignment

Duplicate elimination

Grouping & Aggregation


36

Das könnte Ihnen auch gefallen