Professional Documents
Culture Documents
Relational Algebra & SQL
Relational Algebra & SQL
Module 4
Introduction to Relational Algebra
Formal language for the relational model
Provides basic set of operations for the relational
model
Enable a user to specify basic retrieval requests as
relational algebra expressions
Its operations can be divided into two groups:
mathematical set theory – like union, intersection, set
difference, and cartesian product
operations - select, project, and join
Basic Relational Algebra Operations
(Unary):
Selection, Projection
Set Difference: R – S
Produces a relation with tuples that are in R but NOT in S.
+
Relational Algebra Operations (Set):
Cartesian Product, Intersect
Cartesian Product: R x S
Produces a relation that is concatenation of every tuple of R with every
tuple of S
Intersection: R S
All tuples that are in both R and S
+
Relational Algebra Operations (Join):
Theta Join, Natural Join
Theta Join: R F S = F (R x S)
Selectall tuples from the Cartesian product of
the two relations, matching condition F
When F contains only equality “=“, it is called
Equijoin
Natural Join: R S
Equijoin with common attributes eliminated
Unary Relational Operations
Unary – operations performed on single relation
Select
Project
Select Operation
used to choose a subset of the tuples from a relation that satisfies a
selection condition
Performs horizontal partition on the relation
In general, the SELECT operation is denoted by
E.g.,
Variations of Join
Equi Join - involves join conditions with equality
comparisons only
where F1, F2, ..., Fn are functions over the attributes in relation R
and may involve arithmetic operations and constant values
LEFT OUTER JOIN ( ) operation keeps every tuple in the first, or left,
relation R in R S; if no matching tuple is found in S, then the attributes
of S in the join result are filled or padded with NULL values
FULL OUTER JOIN, denoted by , keeps all tuples in both the left and
the right relations when no matching tuples are found, padding them with
NULL values as needed
Relational Algebra Operators
Symbol (Name) Example of Use
σ
σ
(Selection) salary > = 85000 (instructor)
Return rows of the input relation that satisfy the predicate.
Π
Π
(Projection) ID, salary (instructor)
Output specified attributes from all rows of the input relation.
Remove duplicate tuples from the output.
x
(Cartesian Product) instructor x department
Output pairs of rows from the two input relations that have the
same value on all attributes that have the same name.
∪
Π ∪ Π
(Union) name (instructor) name (student)
Output the union of tuples from the two input relations.
-
Π -- Π
(Set Difference) name (instructor) name (student)
Output the set difference of tuples from the two input relations.
⋈
(Natural Join) instructor ⋈ department
Output pairs of rows from the two input relations that have the
same value on all attributes that have the same name.
Database System Concepts - 6th Edition 2.37 ©Silberschatz, Korth and Sudarshan
SAILORS (SID, SNAME, RATING, AGE)
BOATS (BID, BNAME, COLOR)
RESERVES (SID, BID, DAY)
Database System Concepts - 6th Edition 2.38 ©Silberschatz, Korth and Sudarshan
SAILORS (SID, SNAME, RATING, AGE)
BOATS (BID, BNAME, COLOR)
RESERVES (SID, BID, DAY)
Database System Concepts - 6th Edition 2.39 ©Silberschatz, Korth and Sudarshan
SAILORS (SID, SNAME, RATING, AGE)
BOATS (BID, BNAME, COLOR)
RESERVES (SID, BID, DAY)
Database System Concepts - 6th Edition 2.40 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 6th Edition 2.41 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 6th Edition 2.42 ©Silberschatz, Korth and Sudarshan
Heuristic Query Optimization
Database System Concepts - 6th Edition 2.50 ©Silberschatz, Korth and Sudarshan
Database System Concepts - 6th Edition 2.51 ©Silberschatz, Korth and Sudarshan
Using Selectivity and Cost Estimates in Query
Optimization
• Cost-based query optimization: Estimate and compare the
costs of executing a query using different execution strategies
and choose the strategy with the lowest cost estimate.
(Compare to heuristic query optimization)
• Issues
– Cost function
– Number of execution strategies to be considered
Chapter 15-55
Using Selectivity and Cost Estimates in Query
Optimization (2)
• Cost Components for Query Execution
1. Access cost to secondary storage
2. Storage cost
3. Computation cost
4. Memory usage cost
5. Communication cost
Chapter 15-56
1. Translating SQL Queries into Relational
Algebra (1)
• Query block: the basic unit that can be translated
into the algebraic operators and optimized.
• A query block contains a single SELECT-FROM-
WHERE expression, as well as GROUP BY and
HAVING clause if these are part of the block.
• Nested queries within a query are identified as
separate query blocks.
• Aggregate operators in SQL must be included in
the extended algebra.
Chapter 15-57
Translating SQL Queries into Relational
Algebra (2)
SELECT LNAME, FNAME
FROM EMPLOYEE
WHERE SALARY > ( SELECT MAX (SALARY)
FROM EMPLOYEE
WHERE DNO = 5);
• Emp Table
Execute the Query
STEP 1
Steps in converting a query tree
during heuristic optimization.
(a) Initial (canonical) query tree for SQL query Q.
Read only
36 Bytes
STEP 2:
)
STEP 3
• Apply more restrictive ‘SELECT’ operation
First.
• In this Query Tree no Restrictive SELECT is
available.
• Ignore and proceed to STEP 4.
STEP 4:
Step 4:
Reduced to 44 bytes
from 132 bytes
Total 36 Total 20
bytes bytes
STEP 5:
Exercise 2
• Display the names of all employees who work on the project
name ‘ProjX’
PROJECT
EMPLOYEE PID PNAME
ID NAME DEPT DESIG AAA ProjX
123 SAM 111 CLERK BBB ProjY
124 TOM 222 MANAGER CCC ProjZ WORKSON
125 RAM 333 LECTURER EID PID
123 AAA
126 BOB 222 PLUMBER
124 AAA
127 SAM 333 CLERK
125 BBB
126 BBB
127 CCC
126 BBB
ProjX