Presentation is loading. Please wait.

Presentation is loading. Please wait.

Set Operators. Union Intersect Difference Cartesian product.

Similar presentations


Presentation on theme: "Set Operators. Union Intersect Difference Cartesian product."— Presentation transcript:

1 Set Operators

2 Union Intersect Difference Cartesian product

3 Union Set1={A, B, C} Set2={C, D, E} Union: Members in Set 1 or in Set 2 –Set1 U Set 2 = {A, B, C, D, E} Logical operator OR –Major = ‘CIS’ OR Major = ‘Acct’ –JobSkill = ‘Java’ OR JobSkill = ‘VB’

4 Intersect Members in Set 1 and in Set 2 –Set1 ∩ Set2={C} Logical operator AND –Major = ‘CIS’ AND GPA > 3.0 –JobSkill = ‘Java’ AND Experience > 5 –Note: Registration table: SID, CID Find students taking ISYS363 and ISYS464 CID = ‘ISYS363’ AND CID = ‘ISYS464’

5 Difference Set1={A, B, C} Set2={C, D, E} Set1 – Set2: Members in Set1 but not in set2 = {A,B} Set2 – Set1:Members in Set2 but not in set1 = {D, E} Set1-Set2 ≠ Set2 – Set1 Logical operator: NOT

6 Use Union and Difference to Simulate Intersect Set1 ∩ Set2 = Set1 – (Set1 – Set2)

7 Venn Diagram Set 1: Young: Age<30 Set 2: Rich: Income>100,000’ Set 3: Smart: IQ > 150

8 Files as Sets Business students’ file: BusSt Science student’s file: SciSt –BusSt U SciSt: –BusSt ∩ SciSt –BusSt – SciSt Spring 06 Student file: S06St Fall 06 Student file: F06St –S06St – F06St –F06St – S06St

9 Union Compatibility Two relations that have the same number of attributes and same type of attributes. Union, Intersect and difference operators require the two relations to be union compatible.

10 Union Compatibility Examples File 1: –SID – 9 characters –Sname – 25 characters File 2: –SSN – 9 characters –Ename – 25 characters File 3: –Ename – 25 characters –EID – 9 characters File 1 and file 2 are union compatible; file 1 and file 3 are not; file 2 and file 3 are not.

11 Product Set1 = {a, b, c} Set2 = {X, Y, Z} Set1 X Set2 = {aX, aY aZ, bX, bY, bZ, cX, cY, cZ}

12 Faculty File: FIDFname F1Chao F2Smith Student File: SIDSname FID S1PeterF1 S2PaulF2 S3SmithF1 Faculty X Student:

13 SQL Set Operators Union compatible Union: –(SELECT * FROM table1) UNION (SELECT * FROM table2); Intersect: (SELECT * FROM table1) INTERSECT (SELECT * FROM table2); Minus: (SELECT * FROM table1) MINUS (SELECT * FROM table2);

14 Find students taking 263 and acct 101 SELECT sid FROM registration WHERE cid='ISYS263' INTERSECT SELECT sid FROM registration WHERE cid='acct101‘; Note 1: This condition is always false: –SELECT sid FROM registration –WHERE cid='ISYS263‘ AND cid='acct101‘; Note 2: How to get student name? –SELECT sid,sname FROM student –WHERE sid IN ( ….);

15 Use IN to do intersect SELECT sid,sname FROM student WHERE sid IN ( SELECT sid FROM registration WHERE cid='ISYS263‘) AND sid IN (SELECT sid FROM registration WHERE cid='acct101‘); Other methods? –Using Join –SELECT sid,sname FROM student –WHERE sid IN( select sid from registration where cid=‘ISYS263') natural join (select sid from registration where cid=‘acct101');

16 Find students taking 1-unit and 3- units courses; Select sid, sname From student Where sid IN (select sid from registration where cid IN (select cid from course where units=1)) AND sid IN (select sid from registration where cid IN (select cid from course where units=3)); Select sid, sname From student Where sid IN (select sid from registration where cid IN (select cid from course where units=1) INTERSECT select sid from registration where cid IN (select cid from course where units=3));

17 Use NOT IN to do difference Q: Display faculty’s name and phone if the faculty does not advise any student. –SELECT fid, fname FROM faculty –WHERE fid NOT IN (SELECT DISTINCT fid FROM student);

18 Cartesian Product SELECT fields FROM table1, table2; SELECT * FROM student, faculty; table name alias: Ex: SELECT aid,sname,s.fid,fname FROM student s, faculty f; Take the product of a table itself. –SELECT * FROM emp e1, emp e2; –SELECT * FROM student s1, student s2;

19 CROSS JOIN = Product SELECT * FROM student CROSS JOIN course;

20 Access Demo How to do product with Access? Union? Access query wizards: –Find duplicates query. –Find unmatched query


Download ppt "Set Operators. Union Intersect Difference Cartesian product."

Similar presentations


Ads by Google