relational query operators (ii). other joins theta join: apply any specified condition on the...
TRANSCRIPT
Relational Query Operators (ii)
Other Joins
• Theta join: Apply any specified condition on the product of two relations.
• Example:
• BoyStudent: BID, Bname, GPA
• GirlStudent: GID, Gname, GPA
• Boys want to date with girls who have better GPA.
Outer Join
• Records in a relation that do not have matching values are included in the result relation. Missing values are set to null.
Outer Join Exmple• Product Table:
– PID Pname– P1 TV– P2 VCR– P3 Computer– P4 Tape– P5 DVD
• TotalSales– PID TotalSales– P1 50– P3 60– P5 40
• Product Join TotalSales• Product OuterJoin Totalsales
PID Pname TotalSales
P1 TV 50P2 VCRP3 Computer 60P4 TapeP5 DVD 40
Branch:BID CityB1 SFB2 SMB3 SJ
Full Outer Join:BID City PIDB1 SF P3B2 SM NullB3 SJ P2Null LA P1
Right Outer Join:BID City PIDB1 SF P3B3 SJ P2Null LA P1
Property:PID CityP1 LAP2 SJP3 SF
Left Outer Join:BID City PIDB1 SF P3B2 SM NullB3 SJ P2
How to Implement Join
• Outer – Inner loop
• Sort – Merge
• Index on the join attributes– Faculty Join Student
Division
• Assume relation R is defined over the attribute set A and relation S is defined over the attribute set B where B is a subset of A. Let C = A – B, then the division operation defines a relation over the attribute C that consists of the set of records from R that match the combination of every records in S.
Division Example
• Relation R:– A B– a x– a y– a z– b x– c z
Relation S:
B
x
z
R ÷ S:
A
a
Applicant Position Skill
ApplicantSkill PositionSkill
AID SID PID SID
A1 S1 P1 S1
A1 S2 P1 S2
A2 S2 P2 S1
A3 S1 P3 S2
A4 S1
A4 S2
A4 S3
Query: Find applicants that meet the skills required by position P1.
Aggregate Functions
• Max, Min, Sum, Count, Avg
• Ex. Student: SID,Sname, GPA, Sex, Major– F count(SID) (Student)
– F avg(GPA) (Student)
– F count(SID), avg(GPA) (Student)
Aggregates by Group
• Sex F count(SID) (Student)
• Major F count(SID) (Student)
• Major, Sex F count(SID) (Student)
• GPA F count(SID) (Student) ?
• Grouping field:– EmpID, Ename, DOB, Salary, Gender, Race
Examples
• Student: SID,Sname, GPA, Sex, Major• StudentCourse: SID, CID• Course: CID, Cname, Credits• Queries:
– Number of students • in each major• In each sex group• In major, sex group
– Number of students in each course• CID, CName, NumbeOfStudents
– Total credits for each student:• SID, Sname, TotalCredits