Vous êtes sur la page 1sur 47

Week 10

Relational Algebra :
Selection and Projection

Pearson Education 2009

Objectives
Unary

operations - Selection and Projection.

How

to form selection and projection queries


in relational algebra.

How

to form selection and projection queries


in SQL .

Pearson Education 2009

Introduction
RA is a language used in a relational model
where it describes one or more relations to
define another relation without changing the
original relations.
Informally, relational algebra is a (high-level)
procedural language .
It can be used to inform the DBMS how to
create a new relation from one or more
relations in a databases.

Pearson Education 2009

Relational Algebra
Five

basic operations in relational algebra:


Selection, Projection, Cartesian product,
Union, and Set Difference.

These

perform most of the data retrieval


operations needed.

Also

have Join, Intersection, and Division


operations, which can be expressed in
terms of 5 basic operations.
Pearson Education 2009

Relational Algebra Operations

Pearson Education 2009

Relational Algebra Operations

Pearson Education 2009

Selection (or Restriction)


predicate

(R)
Works on a single relation R and defines a
relation that contains only those tuples (rows) of
R that satisfy the specified condition (predicate).

Operators
^ (and)
v (or)

~ (not)

that can be used :

Pearson Education 2009

Instances of Branch and Staff Relations

8
Pearson Education 2009

Examples

Staff Table

Example - Selection (or Restriction)


List

all staff with a salary greater than 10,000.

salary > 10000 (Staff)

Pearson Education 2009

10

Example - Selection (or Restriction)

The RA for selection can be translated to SQL statements


to select rows(tuple) or data from a relation(table).
predicate (R) when translated into SQL statement:
SELECT * FROM(R)
WHERE(predicate);

Example :
Relational Algebra : salary > 10000 (Staff)
SQL Statement

: SELECT * FROM Staff


WHERE salary>10000;

12

Examples
branch

table

13

Examples
1. Suppose that the query is to retrieve information of branches
which are located in London .

Relational Algebra : (city=London)(Branch)

SQL Statement :
SELECT * FROM branch
WHERE city='London

Output:

14

Examples
2. Suppose that the query is to retrieve information of branch
number B005 which is located in London.

Relational Algebra : (city=London branchno=B005 )(Branch)

SQL Statement :
SELECT * FROM branch
WHERE city='London AND branchno='B005;

Output:

15

Examples
3. Suppose that the query is to retrieve information of branch
number B005 or branches which are located in London.

Relational Algebra : (city=London branchno=B005 )(Branch)

SQL Statement :
SELECT * FROM branch
WHERE city='London OR branchno='B005;

Output:

16

Projection
col1, . . . , coln(R)

Works on a single relation R and defines a


relation that contains a vertical subset of R,
extracting the values of specified attributes and
eliminating duplicates.

Pearson Education 2009

17

Example - Projection
Produce

a list of salaries for all staff, showing only


staffNo, fName, lName, and salary details.
staffNo, fName, lName, salary(Staff)

18
Pearson Education 2009

Example - Projection

The RA for projection can be translated to SQL statements


to select columns from a relation(table)
col1, . . . , coln(R) when translated into SQL statement:
SELECT column_name(s)
FROM table_name
[WHERE conditions]
[GROUP BY column_name(s)]
[HAVING conditions]
[ORDER BY column_name(s)];

19

Example - Projection
Suppose

that the query is to retrieve staff number,


names and salary.

Relational Algebra : staffNo, fName, lName, salary(Staff)

SQL Statement :
SELECT staffNo, fName, lname, salary
FROM staff;

20

Eliminating duplicate rows - Distinct

staff Table

Suppose that the query is to retrieve staff salary.

Relational Algebra : salary(Staff)

SQL Statement :
SELECT salary
FROM staff;

- Output:

21

Eliminating duplicate rows - Distinct

Suppose that the query is to retrieve staff salary.

Relational Algebra : salary(Staff)

SQL Statement :
SELECT DISTINCT salary
FROM staff;

- The result:

Note that the DISTINCT operator is used in the SQL statement


in order to remove the duplicate tuple(s) in the projections
22
result.

Projection with derived columns

A derived column could be based on the result of


mathematical operations(+, *, -, /) or other operators such
as concatenation.

Example : To retrieve monthly salaries for all staffs


together with staff number and names.

Relational Algebra : staffNo, fName, lName, (salary/12)(Staff)

SQL Statement :
SELECT staffNo, fName, lname, (salary/12)
FROM staff;
23

Projection with derived columns


The

result of projection is shown below :

24

Projection with derived columns


Example

: To retrieve the full names of staffs


together with their staff number.

Relational Algebra :staffNo, fName || lName, (salary/12)(Staff)


SQL Statement :
SELECT staffNo, fName ||lname, (salary/12)
FROM staff;

The

result of projection :

25

Projection with derived columns

Columns of projection can be renamed by using AS


keyword.
SELECT staffNo, fName||lname as name, salary/12) as
monthly_salary
FROM staff;

The Result of projection:

26

Projection and Selection

Five basic search conditions (predicates)


Comparison: compare the value of one expression to
the value of another expression
Range: test whether the value of an expression falls
within a specified range of values
Set membership: test whether the value of an express
equals one of a set of values
Pattern match: test whether a string matches a
specified pattern
Null: test whether a column has a null value

Projection and Selection


Comparison

operators :

Operator

Meaning

Equal to

>

Greater than

>=

Greater than or equal to

<

Less than

<=

Less than or equal to

<> Or !=

Not equal to
28

Example

staff Table

29

Comparison operators

To retrieve the staffs (identified by staff number) whose salary below


20000.

Relational Algebra :staffNo, salary(salary<20000)(Staff)

SQL Statement :
SELECT staffNo,salary
FROM staff
WHERE salary < 20000;

The result :

30

Logical Conditions
Operator

Meaning

AND

Returns TRUE if both component


conditions are true

OR

Returns TRUE if either component


condition is true

NOT

Returns TRUE if the following


condition is false

31

Other Comparison Conditions

Operator
BETWEEN
...AND...

Meaning
Between two values (inclusive),

IN(set)

Match any of a list of values

LIKE

Match a character pattern

IS NULL

Is a null value

32

Range

Example : To retrieve the staffs (identified by staff number) whose


salary between 20000 and 30000.

Relational Algebra :
staffNo, salary(salary>20000 salary <=30000)(Staff)

SQL Statement :
SELECT staffNo,salary
FROM staff
WHERE salary >= 20000 AND salary<=30000;

The result :

33

Range

Alternatively, you can write the following SQL


Statement
SELECT staffNo,salary
FROM staff
WHERE salary BETWEEN 20000 AND 30000;

The result:

34

Set Membership

Example : To retrieve the staffs who are manager or


supervisor and display their staff number, name and position.

Relational Algebra :
staffNo, fname, lname, position (position (manager v supervisor)) (Staff)

SQL Statement :
SELECT staffNo, fname, lname, position
FROM staff
WHERE position IN (Manager, Supervisor);

35

Set Membership

Alternatively, we can write the query as :


SELECT staffNo, fname, lname, position
FROM staff
WHERE position = Manager OR position=Supervisor;

The

result :

36

Pattern Match

Consider relation privateowner with schema


privateowner(ownerno, fname, lname,
address, telno, email, password).

privateowner table :

37

Pattern Match

The query is to retrieve information about the owner of


properties in Glasgow, displaying their owner numbers
and address.

Relational Algebra :
ownerNo, address(address LIKE %Glasgow%) (Privateowner)

SQL Statement :
SELECT ownerNo, address

FROM privateowner
WHERE address LIKE %Glasgow%;
38

Pattern Match

The result:

39

Pattern Match
2 types (%)percent and underscore (_)
Example:
LIKE H% the first character must be H, the rest
can be anything
LIKE H_ _ _ _ there must be exactly five
characters and the first must be H
LIKE %e any sequence of characters but the last
one should be an e
LIKE %Glasgow% any length containing
Glasgow
NOT LIKE H% the first character is not an H
40

Null

Consider relation viewing with schema


viewing(clientNo, propertyNo, Viewdate,
comments).

viewing table

41

Null

The query is to retrieve information about the clients who did not give
comments on properties viewed, displaying their client numbers, view
date and comment.

Relational Algebra :
clientNo, viewdate ( comments IS NULL ) (viewing)

SQL Statement :
SELECT clientNo, viewdate
FROM viewing
WHERE comments IS NULL;

- The result:
42

SUMMARY
Selection

and Projection are basic relational algebra


operation that works on a single relation.
Selection () operation defines a relation that contains the
values of all attributes but only for those rows that satisfy
the predicate (or condition) (Horizontal subset of R)
Project () operation defines a relation that contains the
values of specified attributes for all rows and eliminate
duplicates. (Vertical subset of R)
Basic operator in Predicate: comparison, range, set
membership, pattern match, null comparison.
43

SUMMARY
Use

DISTINCT keyword in Select statement to


eliminate duplicate rows.
Use AS keyword to rename column.
Use LIKE operator for pattern matching.
Use BETWEEN operator for range checking.
Use IN operator for set membership. Especially when
the number of members in the set is large.
Use IS NULL operator to compare attribute value with
null.
44

Exercise 1

Write the R.A. and SQL statement for the following query:
1.List the details of all female staffs.
2.List the details of all staffs whose position is not manager.
3.List the details of all staffs whose position is manager or supervisor.
4.List the details of all staffs who works at branch B003 and earn less
than 15,000.
45

Exercise 2

Write the R.A. and SQL statement for the following query:
1.List the first and last name of staffs who is more than 60 years old.
2.List the last name of staffs which begins with the letter B.
3.List the staffNo and salary of staffs who earn more than 10,000 but
less than 20,000.
4.Find the name of staff who is the manager of branch B005.
46

47

Vous aimerez peut-être aussi