1. Home
  2. Explore
  3. Computer Science
  4. Relational Algebra

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Relational Algebra

Speaker 1Speaker 2

Relational Algebra

12:56
0:00-12:56
  1. Maria

    Welcome to Module 9 on relational algebra. Today we’ll explain what relational algebra is, explore its operations, and work through examples and exercises using the RelaX online relational algebra calculator.

  2. Corey

    So what exactly is relational algebra? It is a data manipulation language for the relational model. It is procedural, which means the user specifies how to retrieve the expected data rather than only describing what data is wanted.

  3. Maria

    The operations take one or two relations as input and produce a new relation as their result. Several operations can be combined into a relational algebra expression. In algebra, the operands are the values being operated on, while the operators define how those operands are combined; in relational algebra, the operands are relations or variables representing relations.

  4. Corey

    And the result of evaluating an expression is also a relation. The module groups the operations into four types: set operations, unary operations, binary operations, and extended operations.

  5. Maria

    The demonstrations use RelaX. When you open the calculator, the default data set appears on the left, and you enter a relational algebra expression on the right before executing it. You can load another data set, such as the IMDb sample, and view a relation either by typing its name, like actors, or by clicking the relation name and executing the query.

  6. Corey

    Before discussing set operations, let’s establish the vocabulary. A set is a collection of items of the same type. A tuple is an immutable, ordered sequence of values, equivalent to a row in a relational database, and a relation is a group of tuples, equivalent to a table.

  7. Maria

    An attribute is a field or column in a relation. Its domain is the data type of values permitted for that attribute. Two relations are union-compatible when they have the same degree, meaning the same number of attributes, and corresponding attributes belong to the same domains.

  8. Corey

    Set operations are binary operations because they take two relations as input. The four set operations are union, intersection, set difference, and Cartesian product.

  9. Maria

    Union, written R union S, produces the tuples found in either relation R or relation S. The result has the same degree as the two input relations, and duplicate tuples are removed. R and S must be union-compatible.

  10. Corey

    For example, the writers and clients relations in the user data set are union-compatible because each has four attributes and corresponding attributes have the same domains. Their union combines the tuples from both relations, keeping only one copy of any duplicate, such as a tuple that appears in both.

  11. Maria

    Intersection is written R intersect S. It returns only the tuples that occur in both relations, again with duplicates removed, and the result has the same degree as the input relations. Like union, intersection requires union-compatible relations.

  12. Corey

    Using writers and clients, if John Cena is the only tuple shared by both relations, then the intersection contains only John Cena. This is the key distinction from union: intersection keeps the overlap rather than everything from either relation.

  13. Maria

    Set difference, written R minus S, returns tuples that belong to R but do not belong to S. The relations must be union-compatible, and the order of the operands matters.

  14. Corey

    If writers minus clients is evaluated, the result contains writer tuples that are not clients; John Cena is excluded because that tuple appears in both. If we reverse the expression to clients minus writers, we get client tuples that are not writers, such as Mary, Harold, and Joy.

  15. Maria

    The fourth set operation is the Cartesian product, written R cross S. It concatenates every tuple in R with every tuple in S, so every tuple from the first relation is paired with every tuple from the second.

  16. Corey

    The degree of the result is the sum of the degrees of R and S. If both relations have four attributes, the result has eight attributes, and the number of tuples is the number in R multiplied by the number in S. Unlike union, intersection, and difference, Cartesian product does not require union compatibility and is useful when building join operations.

  17. Maria

    The set-operation exercises ask learners to combine operations to produce specific results: the first and last names of writers and admins, clients who are admins, admins who are not writers, and the product of writers, clients, and admins. The important strategy is to use unary operations first when needed, then apply the set operation.

  18. Corey

    That strategy matters when relations are not initially union-compatible. For example, actors and directors may have different attributes, so their union produces an error. Projecting the actors relation onto matching attributes such as id, first name, and last name can make the two relations compatible before taking their union.

  19. Maria

    Now let’s move to unary operations. A unary operation takes one relation as input and produces one relation as output. The four operations in this section are select, project, rename, and assignment.

  20. Corey

    Selection is written with sigma followed by a condition and a relation. It returns the rows that satisfy the condition, so it is a horizontal filter. Conditions can use comparison operators and can be combined with AND, OR, and LIKE.

  21. Maria

    For example, selection can retrieve actors whose last name ends with a particular pattern. It can also retrieve movies released in 2003 with a rank greater than 7 by applying both conditions to the movies relation.

  22. Corey

    Projection is written with pi followed by a list of attributes and a relation. It is a vertical filter: it keeps selected columns and discards the others. Projecting first name from actors returns only that attribute.

  23. Maria

    Selection and projection are often combined. To find actors whose first name is Michael and display only their first and last names, first select the rows where first name equals Michael, then project first name and last name from that result.

  24. Corey

    Rename changes the name of a relation, its attributes, or both. Renaming the directors relation to producers changes the relation name. You can also rename id, first name, and last name to director id, f name, and l name, respectively.

  25. Maria

    Assignment makes complex queries easier to express as a sequence of steps. The result of an expression is assigned to a temporary relation variable, and later expressions can use that variable. For example, you can select male actors into a variable called male actors and then project attributes from that temporary relation.

  26. Corey

    Assignment can also organize a longer expression: combine a projection of actors with the movie genres relation, assign the result to t, and then select from t where genre equals action. The final expression displays the result of the sequence.

  27. Maria

    The unary-operation exercises use the IMDb sample. One asks for actors whose names start with “ch.” Another asks for the first and last names of actors whose first name is Stanley, with those attributes renamed to f name and l name.

  28. Corey

    Binary operations also combine two input relations into one output relation. The four covered here are theta join, equijoin, natural join, and division. Selection and projection may be used as part of these operations.

  29. Maria

    A theta join, also called a conditional join, joins two relations according to a specified condition. The condition can use comparison operators such as less than, greater than, or equal to. For example, joining PCs and laptops where PC speed is less than laptop speed pairs each qualifying PC with laptops that are faster.

  30. Corey

    A demonstration instead uses the condition that PC RAM is greater than laptop RAM. The result includes combinations such as PC model 1001, with 1024 RAM, and a laptop with only 512 RAM. The condition determines which pairs are included.

  31. Maria

    An equijoin is a theta join that uses only the equality condition. If PCs and laptops are joined where their RAM values are equal, only pairs with matching RAM values appear. For example, PC model 1001 matches a laptop whose RAM is also 1024.

  32. Corey

    A natural join does not explicitly use comparison operators. Instead, the two relations must share at least one attribute with the same name and domain. The operation matches equal values of those common attributes and removes the duplicate copy of the common attribute.

  33. Maria

    For instance, project maker and model from product, then naturally join that result with laptop. Because model is common, the operation can identify which maker produces each laptop model without keeping two copies of the model attribute.

  34. Corey

    Division returns tuples from one relation that are associated with every tuple in another relation. In the example dividing laptop by PC produces laptop models 2001, 2004, 2006, and 2010 because those models meet the required relationship with every relevant PC tuple.

  35. Maria

    A useful detail about division is that attributes used in the division do not appear in the result. In the laptop example, the result contains model and price rather than the speed attribute used in the division.

  36. Corey

    The binary-operation exercises include joining laptops and PCs to find PCs more expensive than a laptop, selecting and projecting PCs with a specified hard-drive value, and dividing the resulting relation by laptop. These tasks reinforce how joins, selection, projection, and division can be combined.

  37. Maria

    Extended operations increase the power of basic relational algebra. The four covered are generalized projection, aggregation, outer join, and outer union.

  38. Corey

    Generalized projection extends ordinary projection by allowing arithmetic expressions in the projection list. For credit information, the expression can display customer name and calculate credit consumed as credit limit minus credit balance, naming the calculated attribute credit consumed.

  39. Maria

    Aggregation takes a collection of values and returns one value. The aggregate functions covered are sum, average, count, minimum, and maximum. Aggregation may include grouping attributes, or the grouping attributes may be omitted depending on the query.

  40. Corey

    For example, to find the total salary for each department, group by department and apply sum to salary. The result contains each department and its total department salary.

  41. Maria

    Outer joins are similar to ordinary joins, but they preserve tuples that do not have matching values. Missing attributes from the other relation are filled with null values. There are three variations: left outer join, right outer join, and full outer join.

  42. Corey

    A left outer join keeps every tuple from the left relation and adds nulls on the right when no match exists. A right outer join keeps every tuple from the right relation and adds nulls on the left. A full outer join keeps tuples from both relations, using nulls wherever a match is missing.

  43. Maria

    In the example with a relation containing square values and another containing cube values, a left outer join preserves the tuple for number 4 even though it has no matching cube value. A right outer join preserves number 5 when it has no matching square value, and a full outer join preserves both unmatched tuples.

  44. Corey

    In the photos database demonstration, a left outer join between employees and photographers preserves all seven employees, with null photographer values for employees who are not photographers. A right outer join between seniors and employees preserves all employee records, including employees without a senior record whose bonus equals their salary.

  45. Maria

    A full outer join between boats and cars, matching boat price with car price, includes tuples from both three-row relations. Since some prices do not match, the six-row result contains null values for the missing boat or car data.

  46. Corey

    Outer union is similar to union, but it also considers common attributes even when the relations are not necessarily type-compatible. It is equivalent to a full outer join using the common attributes, merging common columns and placing null values in fields that exist in only one relation.

  47. Maria

    For example, two relations may share the attributes value and var, while one has max and the other has increment. The outer union combines the shared columns, preserves tuples from both relations, and fills non-existing data with nulls or, where the data type requires it, a default such as zero.

  48. Corey

    In the photos database example, an outer union-style operation between persons and salespersons, matching person id with employee id, includes tuples from both relations and adds null values where data is absent.

  49. Maria

    The extended-operation exercises ask for two applications. Using generalized projection, calculate the total cost of each laptop model after adding a 200-peso delivery charge. Then, using an outer join with the department relation, retrieve the list of employees.

  50. Corey

    The final challenge uses the IMDb data set. Retrieve the number of movies created by each director where the movie rank is at least 8 and the movie was shown between 1960 and 2000. The solution should combine filtering, joining or otherwise relating the needed data, grouping, and counting.

  51. Maria

    So the central idea is that relational algebra provides a systematic way to transform relations into new relations. Set operations combine compatible relations, unary operations filter or reshape one relation, binary operations relate two relations, and extended operations add calculations, summaries, and ways to preserve unmatched data.

  52. Corey

    And practicing in RelaX helps connect the notation to actual results. Try the exercises and share your solution to the final challenge in the discussion board. That concludes our module on relational algebra.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent