The Relational Model and Relational Algebra · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.

Asking questions with sets of rows

Computing · Data & Databases · ages 18-19
Name ______________________   Date ____________
  1. In π_name(σ_age>10(Students)), which operation acts on the table first?

    • The project, which is written first
    • Both run at the same moment
    • The select, which sits closest to the table
  2. Before you are allowed to write R ∪ S, what must be true about R and S?

    • They must have the same attributes, with matching types
    • They must have the same number of tuples
    • They must share at least one primary key value
    • Nothing, union works on any two tables
  3. Someone moves the bottom row of a table to the top. Nothing else changes. Is it still the same relation?

    • No, the first row is always special
    • No, row order is part of the data
    • Yes, a relation is a set, so row order does not matter
    • Only if the table has a primary key
  4. Which sentence describes a relation?

    • A set of tuples over named attributes
    • A list of numbers in a fixed order
    • A single row with no column names
  5. R holds 4 tuples. S shares 2 of them. How many tuples are in R - S?

    Answer: ______________

  6. Table Students(name, age). You want just the names of the students older than 10. Which expression gives that result?

    • π_name(σ_age>10(Students))
    • σ_age>10(π_name(Students))
    • π_age(σ_name(Students))
    • Students − π_name(Students)
  7. Relation R has 4 tuples and relation S has 3 tuples. They share 2 identical tuples. How many tuples are in R ∪ S?

    Answer: ______________

  8. In pure relational algebra, a friend claims a query result contains the exact same tuple twice. What do you know?

    • That cannot happen, because query results are sets and sets have no duplicates
    • The query is invalid and must be rewritten
    • The second copy is kept because the tuples arrived in a different order
    • It means the table has no primary key
  9. Relations R and S both have attribute a. Which pair of expressions always gives the same relation?

    • σ_a>5(R ∩ S) and σ_a>5(R) ∩ σ_a>5(S)
    • π_a(R ∩ S) and π_a(R) ∩ π_a(S)
    • R − S and S − R
    • σ_a>5(R − S) and σ_a>5(S − R)
  10. Kim projects R and S onto a first, intersects, and claims it always equals intersecting first and projecting after. What is wrong?

    • Intersection is only defined on single columns
    • Projecting first can create matches between rows that were never equal
    • Nothing, the two writings always agree
LightMySky · lightmysky.comW1-mt_unDBd_Ug8T-s1

Answer key

For grown-ups. Fold this page away before handing over the rest.

Asking questions with sets of rows W1-mt_unDBd_Ug8T-s1

  1. The select, which sits closest to the table · Expressions compose inside out, so the inner select filters rows before the project keeps names.
  2. They must have the same attributes, with matching types · Union pours the tuples of both relations into one set. That only makes sense if every tuple has the same shape, meaning the same attribute names and types.
  3. Yes, a relation is a set, so row order does not matter · A relation is a set of tuples. Sets care about membership, not arrangement, so shuffling rows leaves the relation unchanged.
  4. A set of tuples over named attributes · A relation names its columns and stores each distinct row once, with no order.
  5. 2 · Difference drops the 2 shared tuples, so 4 - 2 = 2.
  6. π_name(σ_age>10(Students)) · Select first, while the age column still exists to test, then project away the age column. Projecting first deletes the column the condition needs.
  7. 5 · A union is a set, so the 2 shared tuples are stored once, not twice. 4 + 3 counts them twice, so subtract the 2 duplicates.
  8. That cannot happen, because query results are sets and sets have no duplicates · Every operation of relational algebra takes sets and returns sets, so duplicates can never appear in a final result. Real SQL allows duplicates only because SQL tables are bags, a deliberate break from the algebra.
  9. σ_a>5(R ∩ S) and σ_a>5(R) ∩ σ_a>5(S) · Selection distributes over intersection: a tuple survives exactly when it is in both R and S and has a above 5. Projection does not distribute, because projecting first can create matches between tuples that were never equal.
  10. Projecting first can create matches between rows that were never equal · Two rows can differ on dropped columns yet agree on a, so early projection can invent shared rows.
Worksheet · LightMySky