Operations in the Relational Model

Operations in the Relational Model

•These operation can be expressed in an algebra, called

“relational algebra”.

•In this algebra relations are the operands and by applying any of operators to be described later, we can build progressively more complex expressions.


Operations of relational algebra fall into four broad classes:

  1. The usual set operations

    union intersection difference

  2. Operations that remove parts of a relation: selection eliminates some rows(tuples) projection eliminates some columns
  3. Operations that combine the tuples of two relations:

    Cartesian product, which pairs the tuples of two relations in all possible ways

    join” operation, which selectively pair tuples from two relations.

  4. An operation called “renaming”.

Conditions for Set Operations on Relations

We can apply three most common operations on sets- union, intersection, difference- to relations provided that these two conditions hold:

  1. R and S must have schemas with identical sets of attributes.
  2. Before applying the operations, the columns of R and S must be ordered so that the order of attributes is the same for both relations.

Set Operations on Relations

R ∪ S, the union of R and S, is the set of elements that are in R or S or both.

R ∩ S, the intersection of R and S, is the set of elements that are in both R and S.

R − S, the difference of R and S, is the set of elements that are inRbutnotinS.NotethatR− SisdifferentfromS− R.


Produces from a relation R a new relation that has only some of R’s columns.

πA1, A2,…,An(R) is a relation that has only the columns for attributes A1, A2,…, An of R.


Compute the expression πtitle, year, length(Movies) on the table:
title year length filmType studioName producerC#

Star wars 1977 124 Mighty Ducks 1991 104 Wayne’s World 1992 95

color color color

Fox Disney Paramount


Resulting relation:


Star wars Mighty Ducks Wayne’s World

year length

1977 124

  1. 1991  104
  2. 1992  95

Example (Continued)

What about πfilmtype(Movies)


The selection operator, applied to a relation R, produces a new relation with a subset of R’s tuples. The tuples in the result are those that satisfy some condition C.

We denote this operation σC( R ). The schema for the resulting relation is the same as R’s schema.


The expression σlength≥100(Movie) is:
title year length filmType studioName producerC# Star wars 1977 124 color Fox 12345 Mighty Ducks 1991 104 color Disney 67890


Suppose we want the set of tuples in the previous relation Movie such that represent Fox movies at least 100 minutes long. We can get these with a more complicated condition:

σlength≥100 And studioName=’Fox’(Movie) The tuple

title year length filmType studioName producerC#

Star wars 1977 124 color Fox 12345 Is the only one in the resulting relation.

Cartesian Product

The Cartesian Product (or just product) of two sets R and S is the set of pairs that can be formed by choosing the first

element of the pair to be any element of R and the second an element of S. This product is denoted as R×S.

The relation schema for the resulting schema is the union of the schemas for R and S. However, if R and S should happen to have some attributes in common, then we need to invent new names for at least one of each pair identical attributes.


RABSBCD 12 256 34 478

9 10 11


The resulting relation will be:

R×S A R.B S.B C D 12256 12478 1 2 9 10 11 34256 34478 3 4 9 10 11

Natural Join

Denoted as R S.
Let A1, A2,…,An be the attributes in both the schema of R and

the schema of S.

Then a tuple r from R and a tuple s from S are successfully paired if and only if r and s agree on each of the attributes

A1, A2, …, An.

Example: The natural join of the relation R and S from previous example is:

ABCD 1256 3478



The result of this operation is constructed as follows:

• Take the product of R and S.

• Select from the product only those tuples that satisfy the condition C.

Schema for the result is the union of the schema of R and S with, “R” or “S” prefix as necessary.


Compute the natural and theta join for two relations U and V: ABC BCD 123 234 678 235

9 7 8 7 8 10 Relation U Relation V


A B C D A U.B U.C V.B V.C D 1234123234 1235123235 6 7 8 10 1 2 3 7 8 10 9 7 8 10 6 7 8 7 8 10

Result U V 9 7 8 7 8 10 Result of U A<D V

Combing Operations to Form Queries

“What are the title and years of movies made by Fox that are at least 100 minutes long?”

One way to compute the answer to this query is:
• Select those Movie tuples that have length > 100.

• Select those Movie tuples that have studioName =‘Fox’. • Compute the intersection of first and second steps.

• Project the relation from the third step onto attributes title and year.

Example (Continued)

π title,year(σ length≥100(Movie) ∩ σ studioName=‘Fox’(Movie)) π title,year

σ length≥100 σ studioName=‘Fox’ Movie Movie

Another Example

Consider two relations Movie1 and Movie2,

With schemas:

Movie1(title, year, length, filmType, studioName) Movie2(title, year, starName)

Suppose we want to know:
“Find the stars of the movies that are at least 100 minutes long.”

First we join the two relations: Movie1, Movie2 Second we select movies with length at least 100 min. Then we project the starName.

Renaming Operator

In order to control the names of the attributes used for relations that are constructed by one or more applications of the relational algebra operations, it is often convenient to use an operator that explicitly renames relations.

We shall use the operator ρS(A1,A2,…,An) (R) to rename a relation R.
•The resulting relation has exactly the same tuples as R, but the name of the

relation is S.

•Moreover, the attributes of the result relation S are named A1, A2, …, An,

in order from the left.


Product(maker, model, type)
PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price)

(Exercise 5.2.1)


a) What PC models have a speed of at least 1000?

b) Which manufacturers make laptops with a hard disk of at least one gigabyte?

c) Find the model number and price of all products (of any type) made by manufacturer B.

d) Find the model numbers of all color laser printers.

e) Find those manufacturers that sell Laptops, but not PC’s.

!f) Find those hard-disk sizes that occur in two or more PC’s.

!g) Find those pairs of PC models that have both the same speed and RAM. A pair should be listed once.

!!h)Find those manufacturers of at least two different computers (PC or Laptops) with speed of at least 700.