Professional Documents
Culture Documents
Binary Operations in Relational Algebra & SQL
Binary Operations in Relational Algebra & SQL
Binary Operations in Relational Algebra & SQL
UNION operation
Example
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
SQL for previous example Fig 6.4: (SELECT Fn, Ln FROM STUDENT) INTERSECT (SELECT Fname, Lname FROM INSTRUCTOR);
5
STUDENT - INSTRUCTOR
INSTRUCTOR - STUDENT
B B
SQL for previous example Fig 6.4: (SELECT Fn, Ln FROM STUDENT) MINUS (SELECT Fname, Lname FROM INSTRUCTOR);
Example
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:
Offe ring
OfferNo FacSSN 1111 111-11-1111 2222 222-22-2222 3333 111-11-1111
SQL query
a. T1
(T1.P=T2.A)T2
Result
b. T1
(T1.Q=T2.B)T2
NATURAL-Join
Faculty
Example:
Offe ring
OfferNo FacSSN 1111 111-11-1111 2222 222-22-2222 3333 111-11-1111
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:
Offe ring
OfferNo FacSSN 1111 111-11-1111 2222 222-22-2222 3333 111-11-1111
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