Other Joins ISYS 464
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 Example Product Table: –PIDPname –P1TV –P2VCR –P3Computer –P4Tape –P5DVD TotalSales –PIDTotalSales –P150 –P360 –P540 Product Join TotalSales Product OuterJoin Totalsales PID Pname TotalSales P1TV50 P2VCR P3Computer 60 P4Tape P5DVD40
Branch: BIDCity B1SF B2SM B3SJ Full Outer Join: BIDCityPID B1SFP3 B2SMNull B3SJP2 NullLAP1 Right Outer Join: BIDCityPID B1SFP3 B3SJP2 NullLAP1 Property: PIDCity P1LA P2SJ P3SF Left Outer Join: BIDCityPID B1SFP3 B2SMNull B3SJP2
Outer Join SELECT s.*, f.* FROM –student s FULL JOIN faculty f ON s.fid = f.fid; SELECT s.*, f.* FROM –student s RIGHT JOIN faculty f ON s.fid = f.fid; SELECT s.*, f.* FROM –student s LEFT JOIN faculty f ON s.fid = f.fid;
Use Outer Join to Find Record in One Table with No Related Records in a Related Table SELECT faculty.fid,fname FROM faculty left join student on faculty.fid=student.fid WHERE sid IS Null; Find faculty who do not advise any student.
Outer Join with Access Query design view: –Add two tables to join –Right click the link and select Join Properties Option 1: natural join Option 2: Right outer join Option 3: Left outer join Note: How to do full outer join with Access?
Self Join Recursive relationship: –Employee supervise Employee Ex: Empployee: eid, eName, Sex, Salary, Hire_date, SupervisorID Find employees’ supervisor name: –SELECT e.eid, e1.ename,s.eid as superID, s.ename as sueprname –FROM empployee e, empployee s –WHERE e.SupervisorID=s.eid;
Theta Join Join by any condition SELECT * FROM table1, table2 WHERE criteria;
Theta Join Example: match male employees with female employees who have higher salary: –SELECT e1.eid,e1.ename,e2.eid,e2.ename –FROM empployee e1, empployee e2 –WHERE e1.sex=‘M’ and e2.sex=‘F’ –AND e1.salary < e2.salary;
Theta Join Example: match male students with female students who have higher GPA: –select s1.sid as BID, s1.sname as BNAME, s1.gpa as BGPA, s2.sid as GID, s2.sname as GNAME, s2.gpa as GGPA –from student s1, student s2 –where s1.sex='m' and s2.sex='f' and s1.gpa<s2.gpa;
How to Implement Join Outer – Inner loop Sort – Merge Index on the join attributes –Faculty Join Student