5. Relational Algebra Flashcards

1
Q

Why do we need relational algebra (in addition to SQL)?

A

SQL is a declarative programming language, i.e. you specify what you want, but you don’t have to specify how to do it.

When we study query optimization, we want a way to express the many different valid plans a database can use to execute a query. For this, we will use Relational Algebra, a procedural programming language (specifies exactly what operators to use and in what order)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What inputs and outputs have operators in relational algebra

A

All of the operators in relational algebra take in a relation and output a relation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What operators relational algebra has? (9)

A

Projection (π)

Selection (σ)

Union (∪)

Set Difference (-)

Intersection (∩)

Cross Product (×)

Joins

Rename (ρ)

Group By / Aggregation (γ)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Projection (π) - explain

A

selects only the columns specified.

π name (dogs) - get only name column from dogs relation

equivalent to: SELECT name FROM dogs ;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Selection (σ) - explain

A

used to filter rows based on a certain condition.

σ age=12 (π name,age (dogs))

is equivalent to:

SELECT name, age FROM dogs WHERE age = 12;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Union (∪) - explain

A

we take all the rows from each relation and combine them removing duplicates along the way. Same as union in SQL.

E.g.

π name (dogs) ∪ π name (cats)

would return a set of names of both cats and dogs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Set Difference (-) - explain

A

It returns every row in the first table except the rows that also show up in the second table

E.g.

π name (dogs) − π name (cats)

will show names included in the dogs table and at the same time not included in the cats table.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Intersection (∩) - explain

A

it only keeps rows that occur in both tables in the intersection

E.g.

π name (dogs) ∩ π name (cats)

will return names included in both dogs and cats

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Cross Product (×) - explain

A

Create one tuple for every possible pair of tuples (with removing duplicates).

E.g.

dogs × parks

will return a set of all possible dog-park pairs

so it we have N dogs and M parks, we’ll get N * M results

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Joins - explain

A

Equivalent to SQL joins.

joins can actually be derived from just a cross product (×) and conjunction of selections (σ)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Rename (ρ) - explain

A

accomplishes the same thing as aliasing in SQL

E.g.

π dname( ρ name−>dname (dogs))

will return names of dogs, but the column name will be dname instead.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Group By / Aggregation (γ) - explain

A

equivalent to using the GROUP BY and HAVING clauses in SQL.

E.g.

γ age,COUNT(∗)>5 (dogs)

is equivalent to

SELECT ∗ FROM dogs GROUP BY age HAVING COUNT( ∗ ) > 5;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How duplicates are handled in relational algebra

A

In relational algebra, relations can’t have duplicates at all, so they are removed on every step.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly