relational query operators (ii). other joins theta join: apply any specified condition on the...

12
Relational Query Operators (ii)

Upload: alberta-hart

Post on 28-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Relational Query Operators (ii). Other Joins Theta join: Apply any specified condition on the product of two relations. Example: BoyStudent: BID, Bname,

Relational Query Operators (ii)

Page 2: Relational Query Operators (ii). Other Joins Theta join: Apply any specified condition on the product of two relations. Example: BoyStudent: BID, Bname,

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.

Page 3: Relational Query Operators (ii). Other Joins Theta join: Apply any specified condition on the product of two relations. Example: BoyStudent: BID, Bname,

Outer Join

• Records in a relation that do not have matching values are included in the result relation. Missing values are set to null.

Page 4: Relational Query Operators (ii). Other Joins Theta join: Apply any specified condition on the product of two relations. Example: BoyStudent: BID, Bname,

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

Page 5: Relational Query Operators (ii). Other Joins Theta join: Apply any specified condition on the product of two relations. Example: BoyStudent: BID, Bname,

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

Page 6: Relational Query Operators (ii). Other Joins Theta join: Apply any specified condition on the product of two relations. Example: BoyStudent: BID, Bname,

How to Implement Join

• Outer – Inner loop

• Sort – Merge

• Index on the join attributes– Faculty Join Student

Page 7: Relational Query Operators (ii). Other Joins Theta join: Apply any specified condition on the product of two relations. Example: BoyStudent: BID, Bname,

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.

Page 8: Relational Query Operators (ii). Other Joins Theta join: Apply any specified condition on the product of two relations. Example: BoyStudent: BID, Bname,

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

Page 9: Relational Query Operators (ii). Other Joins Theta join: Apply any specified condition on the product of two relations. Example: BoyStudent: BID, Bname,

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.

Page 10: Relational Query Operators (ii). Other Joins Theta join: Apply any specified condition on the product of two relations. Example: BoyStudent: BID, Bname,

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)

Page 11: Relational Query Operators (ii). Other Joins Theta join: Apply any specified condition on the product of two relations. Example: BoyStudent: BID, Bname,

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

Page 12: Relational Query Operators (ii). Other Joins Theta join: Apply any specified condition on the product of two relations. Example: BoyStudent: BID, Bname,

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