Introduction

  • SQL is a declarative programming language.
  • Relational Algebra is a more expressive language, a procedural programming language
    • meaning the query specifies exactly what operators to use and in what order
  • Relation are sets of tuples (does not allow replication)

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

Projection

means SELECT name FROM dogs

The input parameters specify which tables to pull (like FROM key word)

Selection

Selection is used to filter rows, equivalent to SQL’s WHERE clause.

Both

and

are equivalent to

SELECT name, age FROM dogs WHERE age = 12 AND name='Timmy';

Union

Same as SQL’s UNION. For example:

When union fails

  • union 2-column-relation with 1-column-relation (columns number not match)
  • union string column with int column (columns type not match)

Set Difference

Same as SQL’s EXCEPT,

Intersection

Same as SQL’s INTERSECT operator,

Cross Product

Perform a Cartesian product like the same in SQL.

Joins

Put join condition in the subscript and put right operator on the right side.

  • Theta Join : inner join. The refers to the join condition.
  • Natural join
  • Example:
    • This is a natural join, so to simplify:

Derived from cross product

Theta joins and natural joins are derived from cross product and conjunction of selections For example:

can be rewritten as

and natural join can be rewritten as

Rename

The rename operator essentially accomplishes the same thing as aliasing in SQL.

: Renames the dogs relation’s name column to first, so there’s no conflict in column names.

Group By / Aggregation

SELECT age, SUM(weight) FROM dogs GROUP BY age HAVING COUNT(*) > 5;

can be expressed as: