Set Operators. Union Intersect Difference Cartesian product.

Slides:



Advertisements
Similar presentations
© Abdou Illia MIS Spring 2014
Advertisements

A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Binary Operations in Relational Algebra & SQL
COP-5725 Practice Exercises
Chapter (7): Advanced SQL
Relational Algebra Rohit Khokher. Relational Algebra Set Oriented Operations UnionIntersectionDifference Cartesian Product Relation Oriented Operations.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Normalization ISYS 464. Database Design Based on ERD Strong entity: Create a table that includes all simple attributes –Composite Weak entity: add owner.
SQL Sub (or Nested ) Query. Examples Q: Find students whose GPA is below the average. –The criteria itself requires a SQL statement. –SELECT * FROM student.
 CS 405G: Introduction to Database Systems Lecture 7: Relational Algebra II Instructor: Chen Qian Spring 2014.
Data Modeling and Relational Database Design ISYS 650.
Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.
SQLPlus Commands. Oracle Account Server: libra.sfsu.edu Telnet: libra.sfsu.edu How to use Oracle: –
Introduction to Database ISYS 363. File Concepts File consists of a group of records. Each record contains a group of fields. Example: Student file –SIDSnameMajorSexGPA.
1 SQL (Simple Query Language). 2 Query Components A query can contain the following clauses –select –from –where –group by –having –order by Only select.
Structured Query Language Review ISYS 650. Language Overview Three major components: –Data definition language, DDL Create, Drop and Alter Tables or Views.
Relational Algebra The mathematical foundation for SQL Basic operators  select  project  union  intersect  set difference  cross product (i.e. Cartesian.
Complex Conditions. Logical Operators: AND, OR, NOT AND Cond1Cond2Cond1 AND Cond2T TF FTF OR Cond1Cond2Cond1 OR Cond2T TF FTF NOT CondNOT Cond T F.
Introduction to Database ISYS 363. File Concepts File consists of a group of records. Each record contains a group of fields. Example: Student file –SIDSnameMajorSexGPA.
Introduction to Database. File Formats Comma delimited file –"s1","peter",3 –"s2","paul",2.5 –"s3","mary",3.5 –Demo: Excel – Data/Import Extended Markup.
RELATIONAL ALGEBRA (III) Prof. Sin-Min LEE Department of Computer Science.
Basic SQL Select Commands. Basic Relational Query Operations Selection Projection Natural Join Sorting Aggregation: Max, Min, Sum, Count, Avg –Total –Sub.
Querying Database ISYS 363. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy.
Introduction to Access BUS 782. Access Objects Tables –Open –Design –New –Wizard Queries Forms Reports Pages.
Cs3431 Relational Algebra : #I Based on Chapter 2.4 & 5.1.
Relational Algebra. Set operations: Union, intersection, difference, Cartesian product Relational operations: Selection, projection, join, division.
Querying Database. Basic Query Language Operations Selection Projection Join Aggregates: Max, Min, Sum, Avg, Count –Totals and SubTotals –GroupBy Calculated.
RELATIONAL ALGEBRA (II) Prof. Sin-Min LEE Department of Computer Science.
Relational Operators, SQL, and Access Query ISYS 562.
Structured Query Language Introduction to SQL What is SQL? – –When a user wants to get some information from a database file, he can issue a query.1.
SQL: Interactive Queries (1) John Ortiz Lecture 11SQL: Interactive Queries (1)2 Basic Select Statement  Basic form of the select statement: select target-attribute-list.
Relational Algebra 2 Chapter 5.2 V3.0 Napier University Dr Gordon Russell.
Copyright © Curt Hill The Relational Algebra What operations can be done?
Relational Query Operators (ii). Other Joins Theta join: Apply any specified condition on the product of two relations. Example: BoyStudent: BID, Bname,
(C) 2000, The University of Michigan 1 Database Application Design Handout #4 January 28, 2000.
RELATIONAL ALGEBRA Relational Database Handout - 3.
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Advanced SQL for Decision Support ISYS 650. Set Operators Union Intersect Difference Cartesian product.
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 5 © Akhilesh Bajaj, 2000, 2002, 2003, All.
Introduction to Access ISYS 363. Creating a New Database MS Office button/New –Blank database –New database name and location.
Relational Query Operators (Algebra). Relational Query Operators Set operations: Union, intersection, difference, Cartesian product Relational operations:
Relational Algebra MBAD 613 R. Nakatsu. Relational Data Manipulation Language Query-by-Example; Query-by-Form Transform-Oriented Languages Relational.
Advanced Relational Algebra & SQL (Part1 )
Querying Database ISYS 363.
Relational Algebra Relational Calculus
CMPT 258 Database Systems SQL Queries (Chapter 5).
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Introduction to Access BUS 782. Creating a New Database MS Office button/New –Blank database –New database name and location.
Introduction to Access ISYS 363. Access Objects Tables –Open –Design –New –Wizard Queries Forms Reports Pages.
SQL Miscellaneous Topics. Views A database view is: – a virtual or logical table based on a query. – a stored query. CREATE VIEW viewname AS query; –CREATE.
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 4 © Akhilesh Bajaj, 2000, 2002, 2004, All.
SQL advanced select using Oracle 1 Multiple Tables: Joins and Set Operations Subqueries: Nested Queries.
SQL BY MR.PRASAD SAWANT INDIA Introduction to SQL What is SQL? –When a user wants to get some information from a database file, he can issue a query.1.
Section 1.2 – 1.3 Outline Intersection  Disjoint Sets (A  B=  ) AND Union  OR Universe The set of items that are possible for membership Venn Diagrams.
Introduction to Access ISYS 363. Access Objects Tables –Open a table: Double click the table name –Home/View: Datasheet view Design view Queries Forms.
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez-Martínez Electrical and Computer Engineering Department Lecture 10 – September 18,
BELL RINGER. MULTIPLYING A MONOMIAL BY A POLYNOMIAL.
Normalization ISYS 464. Database Design Based on ERD Strong entity: Create a table that includes all simple attributes –Composite Weak entity: add owner.
Introduction to Database
Introduction to Database
Introduction to Relational Databases
Relational Databases Relational Algebra (1) Select, project, join.
Querying Database ISYS 363.
Database Design: Relational Model
SQL: Structured Query Language
Syllabus Introduction Website Management Systems
Introduction to Database
Presentation transcript:

Set Operators

Union Intersect Difference Cartesian product

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’

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’

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

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

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

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

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.

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.

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

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

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

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 ( ….);

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

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

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

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;

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

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