← 🔣 Relational Algebra & Calculus
Subtopic 2 of 4

2.2Joins, outer joins and division

The idea

Joins combine related rows. The natural join matches equal values of common columns. Outer joins also keep unmatched rows. Division answers "for all" questions.

Think of it like this: Matching guests to tables by ticket number; outer joins also list the guests with no table and the empty tables.

Why it exists

Join results, outer joins and division are asked nearly every year.

How it works

  1. Natural join: pair rows with equal common attributes, one copy of the common columns.
  2. Left outer keeps every left row; right keeps every right row; full keeps both.
  3. Unmatched rows get NULL in the other side's columns.
  4. R ÷ S: values of the extra attributes that occur with every row of S.

Worked example: Takes(sid, cid) ÷ Req(cid) with Req = {DB, OS}.

Students who took both DB and OS: 1 and 2. Student 3 took only DB.

Now play with it

  • →Switch between natural, left, right and full and note where NULLs appear.
  • →Run the division example and explain why student 3 is missing.
Emp
eidnamedid
1Asha10
2Ben20
3Chen10
4DiaNULL
Dept
diddname
10CSE
20ECE
30ME
→
Result: 3 rows
eidnamediddname
1Asha10CSE
2Ben20ECE
3Chen10CSE

Matches on the common column did. Dia (did NULL) and ME (no employee) disappear.

Check yourself

The result of R ÷ S (R(A, B), S(B)) consists of

Remember: Outer joins keep unmatched rows with NULLs. Division means "for all".
Exam notes (the dense version)

Natural join ⋈ matches on all common attributes. A theta join is σ over a cross product. Outer joins keep unmatched rows with NULLs (left, right, full). Division R ÷ S returns the values of the extra attributes of R that appear with every row of S: "students who took all required courses". R ÷ S = π(R) − π((π(R) × S) − R).

Previous year questions: Relational Algebra & Calculus (12)

PYQs are grouped by topic, so this is the full set for Relational Algebra & Calculus; try the ones that match this subtopic. Open all 12 on the topic page.