logo
Published on

CH5. Relational Algebra on One Page

Authors
  • avatar
    Name
    seren-wib
    Twitter
Contents

Relational Algebra on One Page (all of chapter 6)

SQL says "what you want", relational algebra says "in what order to pull it out". When writing answers, breaking them into one line per step with ← keeps you from going wrong

1. Symbols at a glance

SymbolNameWhat it doesSQL
σcondition(R)SELECTRows that meet the conditionWHERE
πcolumns(R)PROJECTOnly the listed columns, duplicates removedSELECT DISTINCT
ρS(columns…)(R)RENAMERenames tables and columnsAS
R ∪ S, R ∩ S, R − SSet operationsUnion, intersection, differenceUNION, INTERSECT, EXCEPT
R × SCARTESIAN PRODUCTEvery combination of rowsFROM R, S (no condition)
R ⋈condition SJOINJoins rows that meet the conditionJOIN ON
R * SNATURAL JOINJoins on same-named columns and keeps one of eachNATURAL JOIN
R ⟕ S, R ⟖ S, R ⟗ SOUTER JOINKeeps rows even without a matchLEFT / RIGHT / FULL OUTER JOIN
grouping columnsℱfunction column(R)AggregationGroups, then COUNT, SUM…GROUP BY + aggregates
R ÷ SDIVISIONR rows that have all of SNOT EXISTS … EXCEPT
T ← expressionAssignmentNames an intermediate resultTemporary table

2. Picking rows σ

σcondition(R): only the rows that meet the condition. Columns unchanged

  • Conditions are column op value / column op column, with AND, OR, NOT. Comparisons are =, <, ≤, >, ≥, ≠

σDno=4(EMPLOYEE) → employees in department 4
σ(Dno=4 AND Salary>25000) OR (Dno=5 AND Salary>30000)(EMPLOYEE)

  • σc1(σc2(R)) = σc1 AND c2(R). Order doesn't matter
  • It's called SELECT but it's SQL's WHERE. SQL's SELECT slot is π

3. Picking columns π

πcolumn list(R): only the listed columns. The result is a set, so duplicate rows are removed

πLname, Fname, Salary(EMPLOYEE) → three columns
πSex, Salary(EMPLOYEE) → two (M, 25000) rows become one

4. Chaining and renaming ρ

Either way: nest with parentheses / break with ←. Runs from the inside (the lower line) first

πFname, Lname, Salary(σDno=5(EMPLOYEE))

DEP5_EMPS ← σDno=5(EMPLOYEE)
RESULT ← πFname, Lname, Salary(DEP5_EMPS)

How to write ρWhat it renames
ρS(B1, B2, …)(R)Table name S, column names B1, B2…
ρS(R)Table name only
ρ(B1, B2, …)(R)Column names only
R(B1, B2, …) ← expressionColumn names can also go on the left of ←

R(First_name, Last_name, Salary) ← πFname, Lname, Salary(DEP5_EMPS)

5. Set operations ∪ ∩ −

Requirement: same number of columns and the same type in each column (type compatible)

ExpressionResultExample
R ∪ SRows in at least one of them, duplicates removedStudents or instructors
R ∩ SRows in bothStudents who are also instructors
R − SRows in R but not in S. Not the same as S − RStudents who aren't instructors

RESULT1 ← πSsn(DEP5_EMPS)
RESULT2(Ssn) ← πSuper_ssn(DEP5_EMPS)
RESULT ← RESULT1 ∪ RESULT2

  • Ssns of employees in department 5 or their direct supervisors. The column names differ, so (Ssn) is added to RESULT2

6. Every combination ×

R × S: attaches every S row to each R row. n + m columns, |R| × |S| rows. Types don't need to match

  • Junk on its own. Follow it with σ to keep only matching combinations → the two together are a JOIN

FEMALE_EMPS ← σSex='F'(EMPLOYEE)
EMPNAMES ← πFname, Lname, Ssn(FEMALE_EMPS)
EMP_DEPENDENTS ← EMPNAMES × DEPENDENT
ACTUAL_DEPENDENTS ← σSsn=Essn(EMP_DEPENDENTS)
RESULT ← πFname, Lname, Dependent_name(ACTUAL_DEPENDENTS)

  • Female employees and their dependents' names. Build 3 rows × 7 rows = 21 rows, then keep only the matches with σ

7. Joining ⋈

R ⋈condition S = σcondition(R × S). Puts an R row and an S row that meet the condition on one line

DEPT_MGR ← DEPARTMENT ⋈Mgr_ssn=Ssn EMPLOYEE
RESULT ← πDname, Lname, Fname(DEPT_MGR)

  • Manager name for each department
KindConditionResult columns
THETA JOIN ⋈A θ Bθ is any of =, <, ≤, >, ≥, ≠All R columns + all S columns
EQUIJOIN ⋈A=B= onlyAll R columns + all S columns. Both equal-valued columns stay (Mgr_ssn, Ssn)
NATURAL JOIN R * S= on same-named columnsOnly one of each same-named column
  • NATURAL JOIN needs the column names to match. If they don't, align them with ρ first

DEPT ← ρ(Dname, Dnum, Mgr_ssn, Mgr_start_date)(DEPARTMENT)
PROJ_DEPT ← PROJECT * DEPT

  • Dnumber → Dnum to match PROJECT.Dnum

  • Number of result rows: 0 (nothing matches) to |R| × |S| (everything matches)

OUTER JOIN: keeps rows without a match too, with NULL in the empty cells. ⟕ all left, ⟖ all right, ⟗ both

TEMP ← EMPLOYEE ⟕Ssn=Mgr_ssn DEPARTMENT
RESULT ← πFname, Minit, Lname, Dname(TEMP)

  • Every employee + the name of the department they manage. Dname is NULL for non-managers

8. Counting ℱ

grouping columns ℱ function column, …(R): groups rows with equal grouping columns and applies the functions. The functions are COUNT, SUM, AVERAGE, MAXIMUM, MINIMUM

Dno ℱ COUNT Ssn, AVERAGE Salary(EMPLOYEE) - Employee count and average salary per department. Result columns Dno, Count_ssn, Average_salary

ℱ COUNT Ssn, AVERAGE Salary(EMPLOYEE)

  • Without grouping columns everything is one group → one row

ρR(Dno, No_of_employees, Average_sal)(Dno ℱ COUNT Ssn, AVERAGE Salary(EMPLOYEE))

  • Wrap it in ρ to rename the columns

  • Counts without removing duplicates / skips NULLs / the result is a table even if it's a single cell

9. Finding what has all of them ÷

R ÷ S: the R rows that have all values in S. S's columns must be a subset of R's, and result columns = R's columns − S's columns

R(A, B) ÷ S(A): only B values that have all of S's a1, a2, a3 remain

SMITH ← σFname='John' AND Lname='Smith'(EMPLOYEE)
SMITH_PNOS ← πPno(WORKS_ON ⋈Essn=Ssn SMITH)
SSN_PNOS ← πEssn, Pno(WORKS_ON)
SSNS(Ssn) ← SSN_PNOS ÷ SMITH_PNOS
RESULT ← πFname, Lname(SSNS * EMPLOYEE)

  • Employees who work on every project Smith works on. If SMITH_PNOS = {1, 2}, only Essns that work on both 1 and 2 remain

Template for ÷: ① build S, the list of "all" (π of one column) → ② build R, who does what (π of two columns, including S's column) → ③ R ÷ S → ④ join back to the original with * to get names

10. The complete set and query trees

Complete set {σ, π, ∪, ρ, −, ×}: these six make all the others

  • R ∩ S = (R ∪ S) − ((R − S) ∪ (S − R))
  • R ⋈c S = σc(R × S)

Query tree: the expression as a tree. Leaves = tables, inner nodes = operations, runs from the leaves upward

Query tree for the Stafford project query, drawn in the order σ → ⋈ → ⋈ → π

11. Same question, SQL next to relational algebra

QuestionSQLRelational algebra
Names and salaries of department 5 employeesSELECT Fname, Lname, Salary FROM EMPLOYEE WHERE Dno = 5πFname, Lname, Salary(σDno=5(EMPLOYEE))
Names and addresses of Research employeesSELECT Fname, Lname, Address FROM EMPLOYEE, DEPARTMENT WHERE Dname = 'Research' AND Dnumber = DnoRESEARCH ← σDname='Research'(DEPARTMENT) / RD ← RESEARCH ⋈Dnumber=Dno EMPLOYEE / πFname, Lname, Address(RD)
Employee and supervisor names… FROM EMPLOYEE AS E, EMPLOYEE AS S WHERE E.Super_ssn = S.SsnS ← ρS(Sssn, Sfn, Sln, …)(EMPLOYEE) / EMPLOYEE ⋈Super_ssn=Sssn S
Employee count per departmentSELECT Dno, COUNT(*) FROM EMPLOYEE GROUP BY DnoDno ℱ COUNT Ssn(EMPLOYEE)
Employees with no dependentsWHERE NOT EXISTS (SELECT * FROM DEPENDENT WHERE Ssn = Essn)ALL ← πSsn(EMPLOYEE) / HAS ← πEssn(DEPENDENT) / πFname, Lname((ALL − HAS) * EMPLOYEE)
Working on every department 5 projectWHERE NOT EXISTS ((SELECT Pnumber FROM PROJECT WHERE Dnum = 5) EXCEPT (SELECT Pno FROM WORKS_ON WHERE Essn = Ssn))P5 ← πPnumber(σDnum=5(PROJECT)) / SP ← πEssn, Pno(WORKS_ON) / SP ÷ P5
Employee + supervisor, including employees with no supervisorLEFT OUTER JOINEMPLOYEE ⟕Super_ssn=Sssn S

12. Question wording → symbols

When the question saysUse
who are ~ (condition)σ
the names, numbers of ~ (picking columns)π. At the very end
Columns from two tables⋈FK=PK. * if the column names match
An employee and their supervisorRename a copy of EMPLOYEE's columns with ρ, then ⋈
~ or ~∪ (with matching columns)
~ and also ~ (in both results)∩
not ~, with no ~− (all − the ones that have it)
How many, sum, averageℱ
per ~, for each ~Grouping columns before ℱ
working on all, every ~÷
Rows without a match must show too⟕ ⟖ ⟗
Column names don't match so * can't be used / renaming result columnsρ