Binary Operations in Relational Algebra & SQL
Set Based: UNION, INTERSECTION, DIFFERENCE
UNION operation
Example
Suppose names of people are distinct
A B RESULT=STUDENT INSTRUCTOR
SQL for previous example Fig 6.4: (SELECT Fn, Ln FROM STUDENT) UNION (SELECT Fname, Lname FROM INSTRUCTOR);
3
Union Compatibility
Requirement for the traditional set operators Strong requirement
Same number of columns Each corresponding column is compatible Positional correspondence
INTERSECTION operation
Example
Suppose names of people are distinct
A B RESULT=STUDENT INSTRUCTOR
SQL for previous example Fig 6.4: (SELECT Fn, Ln FROM STUDENT) INTERSECT (SELECT Fname, Lname FROM INSTRUCTOR);
5
SET DIFFERENCE operation
Example
STUDENT - INSTRUCTOR
INSTRUCTOR - STUDENT
Suppose names of people are distinct
A
B B
(d) RESULT=INSTRUCTOR - STUDENT (e) RESULT=STUDENT - INSTRUCTOR
SQL for previous example Fig 6.4: (SELECT Fn, Ln FROM STUDENT) MINUS (SELECT Fname, Lname FROM INSTRUCTOR);
CARTESIAN PRODUCT operation
Faculty
Example
FacSSN 111-11-1111 222-22-2222 333-33-3333
Faculty PRODUCT Stude nt
FacSSN 111-11-1111 111-11-1111 111-11-1111 222-22-2222 222-22-2222 222-22-2222 333-33-3333 333-33-3333 333-33-3333 StdSSN 111-11-1111 444-44-4444 555-55-5555 111-11-1111 444-44-4444 555-55-5555 111-11-1111 444-44-4444 555-55-5555
Stude nt
StdSSN 111-11-1111 444-44-4444 555-55-5555
Relational Algebra: RESULT= Faculty Student SQL: SELECT * FROM Faculty, Student;
7
EQUI-Join
Faculty
Example:
FacSSN FacName 111-11-1111 joe 222-22-2222 sue 333-33-3333 sara
EQUI-Join EQUI-Join Natural Join of Offe ring and Faculty
FacSSN FacName OfferNo 111-11-1111 joe 1111 222-22-2222 111-11-1111 sue joe 2222 3333
Offe ring
OfferNo FacSSN 1111 111-11-1111 2222 222-22-2222 3333 111-11-1111
RESULT= Faculty ([Link]=[Link])Offering; SELECT * FROM Faculty, Offering WHERE [Link]=[Link];
8
Exercise 1 for Equi-Join
SQL query
a. T1
(T1.P=T2.A)T2
Result
b. T1
(T1.Q=T2.B)T2
NATURAL-Join
Faculty
Example:
FacSSN FacName 111-11-1111 joe 222-22-2222 sue 333-33-3333 sara
Natural Join of Offe ring and Faculty
FacSSN FacName OfferNo 111-11-1111 joe 1111 222-22-2222 111-11-1111 sue joe 2222 3333
Offe ring
OfferNo FacSSN 1111 111-11-1111 2222 222-22-2222 3333 111-11-1111
RESULT= Faculty * Offering; SELECT * FROM EMPLOYEE NATURAL JOIN DEPARTMENT
10
THETA Join
Example:
RESULT=Car {CarPrice>BoatPrice} Boat; Result=R1 {Condition} R2; Condition: {<, >, =, , , }; EquiJoin when =. SELECT * FROM Car, Boat WHERE CarPrice>BoatPrice;
11
THETA Join
Faculty
Example:
FacSSN FacName 111-11-1111 joe 222-22-2222 sue 333-33-3333 sara
EQUI-Join Theta-Join Natural Join of Offe ring and Faculty
FacSSN FacName OfferNo 111-11-1111 joe 1111 222-22-2222 111-11-1111 sue joe 2222 3333
Offe ring
OfferNo FacSSN 1111 111-11-1111 2222 222-22-2222 3333 111-11-1111
SELECT * FROM Faculty, Offering WHERE [Link]=[Link];
12
Exercise 2
Department Student
Dno Dname DHeadSsn Location
SID
Sname
Dno
SAge
Faculty
FSsn
Fname
Dno
FAge
Write Relational Algebra and SQL queries for following questions: What are the names of students who are from department Computer Science? What are the names of faculties who are younger than a student? What are the names of faculties who works in Keller Hall?
13
Summary
Binary Operation
Operation from Set Theory
UNION INTERSECTION DIFFERENCE CARTESIAN PRODUCT
Join Operation
Equi-Join Natural Join Theta Join
14
Reference
Materials in the slides are from Elmasri, Navathe, Fundamentals of Database Systems, 6th, Addison Wesley and Michael V. Mannino, Database: Design, Application Development & Administration, Third Edition, McGraw Hill
15