Académique Documents
Professionnel Documents
Culture Documents
Relational Algebra :
Selection and Projection
Objectives
Unary
How
How
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.
Relational Algebra
Five
These
Also
(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)
8
Pearson Education 2009
Examples
Staff Table
10
Example :
Relational Algebra : salary > 10000 (Staff)
SQL Statement
12
Examples
branch
table
13
Examples
1. Suppose that the query is to retrieve information of branches
which are located in London .
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.
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.
SQL Statement :
SELECT * FROM branch
WHERE city='London OR branchno='B005;
Output:
16
Projection
col1, . . . , coln(R)
17
Example - Projection
Produce
18
Pearson Education 2009
Example - Projection
19
Example - Projection
Suppose
SQL Statement :
SELECT staffNo, fName, lname, salary
FROM staff;
20
staff Table
SQL Statement :
SELECT salary
FROM staff;
- Output:
21
SQL Statement :
SELECT DISTINCT salary
FROM staff;
- The result:
SQL Statement :
SELECT staffNo, fName, lname, (salary/12)
FROM staff;
23
24
The
result of projection :
25
26
operators :
Operator
Meaning
Equal to
>
Greater than
>=
<
Less than
<=
<> Or !=
Not equal to
28
Example
staff Table
29
Comparison operators
SQL Statement :
SELECT staffNo,salary
FROM staff
WHERE salary < 20000;
The result :
30
Logical Conditions
Operator
Meaning
AND
OR
NOT
31
Operator
BETWEEN
...AND...
Meaning
Between two values (inclusive),
IN(set)
LIKE
IS NULL
Is a null value
32
Range
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
The result:
34
Set Membership
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
The
result :
36
Pattern Match
privateowner table :
37
Pattern Match
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
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
SUMMARY
Use
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