Binary Operations in Relational Algebra & SQL

Presentation on theme: "Binary Operations in Relational Algebra & SQL"— Presentation transcript:

Binary Operations in Relational Algebra & SQL

Set Based: UNION, INTERSECTION, DIFFERENCE

UNION operation Example SQL for previous example Fig 6.4:
Suppose names of people are distinct A RESULT=STUDENT INSTRUCTOR B SQL for previous example Fig 6.4: (SELECT Fn, Ln FROM STUDENT) UNION (SELECT Fname, Lname FROM INSTRUCTOR);

Union Compatibility Requirement for the traditional set operators
Strong requirement Same number of columns Each corresponding column is compatible Positional correspondence How are rows compared? - Chapter 3 material: present again for review if desired - Can instead present material in Chapter 4 and skip when initially covering chapter 3 - Join: compares rows on the join column(s) - Traditional set operators compare on all columns Strong requirement: - Usually on identical tables (geographically dispersed tables) - Compatible columns: data types are comparable (numbers cannot be compared to strings) - Positional: 1st column of table A to 1st column of table B, 2nd column etc Can be applied to similar tables (faculty and student) by removing columns before traditional set operator

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);

SET DIFFERENCE operation
Example STUDENT - INSTRUCTOR INSTRUCTOR - STUDENT Suppose names of people are distinct (d) RESULT=INSTRUCTOR - STUDENT A B (e) RESULT=STUDENT - INSTRUCTOR B SQL for previous example Fig 6.4: (SELECT Fn, Ln FROM STUDENT) MINUS (SELECT Fname, Lname FROM INSTRUCTOR);

CARTESIAN PRODUCT operation
Example B Relational Algebra: RESULT= Faculty ✕ Student SQL: SELECT * FROM Faculty, Student;

EQUI-Join EQUI-Join EQUI-Join Example: RESULT= Faculty (Faculty.FacSSM=Offering.FacSSN)Offering; SELECT * FROM Faculty, Offering WHERE Faculty.FacSSN=Offering.FacSSN;

Exercise 1 for Equi-Join
SQL query Result T (T1.P=T2.A)T2 T (T1.Q=T2.B)T2

NATURAL-Join Example: RESULT= Faculty * Offering; SELECT *
FROM EMPLOYEE NATURAL JOIN DEPARTMENT

THETA Join Example: RESULT=Car {CarPrice>BoatPrice} Boat;
Result=R1 {Condition} R2; Condition: {<, >, =, ≤, ≥, ≠}; EquiJoin when “=“. SELECT * FROM Car, Boat WHERE CarPrice>BoatPrice;

THETA Join Example: SELECT * FROM Faculty, Offering
EQUI-Join Example: SELECT * FROM Faculty, Offering WHERE Faculty.FacSSM=Offering.FacSSN;

Exercise 2 Department Student Faculty
Dno Dname DHeadSsn Location SID Sname Dno SAge Student FSsn Fname Dno FAge Faculty 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’?

Summary Binary Operation Operation from Set Theory Join Operation
UNION INTERSECTION DIFFERENCE CARTESIAN PRODUCT Join Operation Equi-Join Natural Join Theta Join

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