Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in.

Slides:



Advertisements
Similar presentations
Foundations of Relational Implementation (2) IS 240 – Database Management Lecture #14 – Prof. M. E. Kabay, PhD, CISSP Norwich University
Advertisements

Advanced SQL (part 1) CS263 Lecture 7.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Joining Relations in SQL Objectives of the Lecture : To consider the Natural & Generalised Joins using the SQL1 standard; To consider the Natural & Generalised.
1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
Foundations of Relational Implementation n Defining Relational Data n Relational Data Manipulation n Relational Algebra.
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
Relational Algebra on Bags A bag is like a set, but an element may appear more than once. –Multiset is another name for “bag.” Example: {1,2,1,3} is a.
CS263 Lecture 19 Query Optimisation.  Motivation for Query Optimisation  Phases of Query Processing  Query Trees  RA Transformation Rules  Heuristic.
Relational Algebra Relational Calculus. Relational Algebra Operators Relational algebra defines the theoretical way of manipulating table contents using.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Chapter 6 Relations. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Topics in this Chapter Tuples Relation Types Relation Values Relation.
Chapter 6 Relations. Topics in this Chapter Tuples Relation Types Relation Values Relation Variables SQL Facilities.
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
Retrievals & Projections Objectives of the Lecture : To consider retrieval actions from a DB; To consider using relational algebra for defining relations;
SQL. Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
Chapter 3 Section 3.4 Relational Database Operators
The Relational Model Part III. Remember: 3 Aspects of the Model It concerns 1) data objects storing it 2) data integrity making sure it corresponds to.
DBSQL 3-1 Copyright © Genetic Computer School 2009 Chapter 3 Relational Database Model.
Lecture 05 Structured Query Language. 2 Father of Relational Model Edgar F. Codd ( ) PhD from U. of Michigan, Ann Arbor Received Turing Award.
Ad Hoc Constraints Objectives of the Lecture : To consider Ad Hoc Constraints in principle; To consider Ad Hoc Constraints in SQL; To consider other aspects.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 6 The Relational Algebra.
Lecture 8 Database Theory & Practice (2) : The Relational Data Model UFCEKG-20-2 Data, Schemas & Applications.
The Data in a Relation To consider atomic data in relations; To consider data types in a relation; To consider missing data & NULLs in relations. Objectives.
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
Chapter 7 Relational Algebra. Topics in this Chapter Closure Revisited The Original Algebra: Syntax and Semantics What is the Algebra For? Further Points.
Relational Algebra A presentation for CS 457 By Dawn Haddan.
Relational Algebra References: Databases Illuminated by Catherine Ricardo, published by Jones and Bartlett in 2004 Fundamentals of Relational Databases.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)
The Relational Algebra. 1 Relational Algebra Operations From Set Theory * UNIONUNION * INTERSECTIONINTERSECTION * MINUSMINUS * CARTESIAN OPERATIONCARTESIAN.
9/7/2012ISC329 Isabelle Bichindaritz1 The Relational Database Model.
SQL Part I: Standard Queries. COMP-421: Database Systems - SQL Queries I 2 Example Instances sid sname rating age 22 debby debby lilly.
NULLs & Outer Joins Objectives of the Lecture : To consider the use of NULLs in SQL. To consider Outer Join Operations, and their implementation in SQL.
Chapter 7 Relational Algebra. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.7-2 Topics in this Chapter Closure Revisited The Original Algebra:
The Relational Algebra and Calculus
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
CS424 Relational Data Manipulation Relational Data Manipulation Relational tables are sets. Relational tables are sets. The rows of the tables can be considered.
IFS Intro to Data Management Chapter 5 Getting More Than Simple Columns.
Further GroupBy & Extend Operations Objectives of the Lecture : To consider “whole relation” Grouping; To consider the SQL Grouping option Having; To consider.
Topics Related to Attribute Values Objectives of the Lecture : To consider sorting relations by attribute values. To consider Triggers and their use for.
Relational Algebra Operators
Advanced Relational Algebra & SQL (Part1 )
Databases : Relational Algebra - Complex Expression 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof.
More Relation Operations 2014, Fall Pusan National University Ki-Joune Li.
Chapter 13 Views Oracle 10g: SQL. Oracle 10g: SQL2 Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE VIEW command Employ the.
Views, Algebra Temporary Tables. Definition of a view A view is a virtual table which does not physically hold data but instead acts like a window into.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Insert & Delete Objectives of the Lecture : To consider the insertion of tuples into a relation; To consider the deletion of tuples from a relation; To.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
1 SQL – IV Grouping data from tables in SQL –The concept of grouping –GROUP BY clause –HAVING Clause –Determining whether values are unique –Group by using.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Collection Operators These slides are.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Collection Operators These slides are.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
 CONACT UC:  Magnific training   
IFS180 Intro. to Data Management Chapter 10 - Unions.
Basic select statement
04 | Using Set Operators Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
More Relational Algebra
Lecture 16 : The Relational Data Model
Chapter 8 Advanced SQL.
Database Design: Relational Model
Lecture 16 : The Relational Data Model
Manipulating Data Lesson 3.
Presentation transcript:

Set Operations Objectives of the Lecture : To consider the Set Operations Union, Difference and Intersect; To consider how to use the Set Operations in SQL.

Relational Union (1) A Union B  A B

Relational Union (2) A Union B  A B

Definition of Union Given A Union B l The result is a relation that contains a copy of all the tuples that are in A, and a copy of all the tuples that are in B. l If a tuple appears in both A and B, then only one copy of it appears in the result. (The result cannot contain duplicate tuples). l  B Union A i.e. the result is the same regardless of which way round the operands are. So Union is said to be commutative.

Relational Difference (1) A B A Difference B 

Relational Difference (2) A B A Difference B 

Definition of Difference Given A Difference B l Result is a relation that contains all the tuples of A which do not also appear in B. l  B Difference A i.e. it does matter which way round the operands are, because it affects the result. So Difference in not commutative - it’s like subtraction in arithmetic)

Relational Intersect (1) A Intersect B  A B

Relational Intersect (2) A B A Intersect B 

Definition of Intersect Given A Intersect B l Result is a relation that contains all the tuples that appear in both A and B, but only one copy of each tuple to avoid duplicates). l  B Intersect A i.e. the result is the same regardless of which way round the operands are. So Intersect is also commutative.

Operand Type Compatibility l The 2 operands of a set operator must :  have the same number of attributes;  each corresponding attribute must have the same domain / type;  corresponding attributes are recognised by having the same name. l Otherwise the tuples of one relation cannot be compared with the tuples of the other.  the operator cannot work. l This is traditionally known as Union Compatibility, but as it applies to to all 3 operators, so it would be better to call it Type Compatibility.

Result of a Set Operation l The result has the same number of attributes as the operands. l Each result attribute has the same data type as the corresponding attributes in the operands. l Each result attribute has the same name as the corresponding attributes in the operands. l The number of tuples in the result depends on the operator used. l Note that Set Operators merge two relations into one. They provide an alternative strategy for merging relations to the Join Operators. Set Operators ‘merge tuples’, whereas Join Operators ‘merge attributes’

SQL : Set Operators l SQL does not express set operations within a SELECT statement. l It uses algebra-like operators :  UNION  EXCEPT (  Difference)  INTERSECT  The operands of an SQL set operator are provided by 2 SELECT statements. Some relational DBMSs - e.g. Oracle - use the keyword MINUS instead of EXCEPT.

Example of SQL Union SELECTS#, P# FROMORDERS UNION SELECTS#, P# FROMSHIPMENTS ; Explanation : Get all the part numbers and supplier numbers of all parts that are on order or being shipped. No semicolon. A semicolon.

Example of SQL Difference SELECTS#, P# FROMORDERS EXCEPT SELECTS#, P# FROMSHIPMENTS ; Explanation : Get all the part numbers and supplier numbers of all parts that are on order but not being shipped. MINUS in Oracle.

Example of SQL Intersect SELECTS#, P# FROMORDERS INTERSECT SELECTS#, P# FROMSHIPMENTS ; Explanation : Get all the part numbers and supplier numbers of all parts that are on order and also being shipped.

The Complexity of SQL Set Operands Each SELECT statement ‘operand’ can be as large and as complex as desired. Example :- SELECTS#, P# FROMORDERS NATURAL JOIN STOCK WHERES# = ‘S1’ AND O-QTY > 200 INTERSECT SELECTS#, P# FROMSHIPMENTS WHERES# = ‘S1’ AND Sh-QTY > 200 ;

SQL Operand Type Compatibility Corresponding columns are not determined by column name but by column position in the SELECT phrases. Thus 1 st column name in first SELECT phrase goes with 1 st column name in the second SELECT phrase, 2 nd column name in first SELECT phrase goes with 2 nd column name in the second SELECT phrase, and so on for all column.  Corresponding columns can have different names. l Corresponding columns must have the same type, or allow the DBMS to cast from one type to another e.g. may cast from CHAR(2) to CHAR(3), but not from CHAR(3) to CHAR(2).

Result of an SQL Set Operation l The result’s column names are those appearing in the first SELECT phrase, if the names differ between the SELECT phrases). l If the SQL tables contain duplicate rows, all 3 operators remove duplicate rows from the result: i.e. they work as proper set operators. Note that no SELECT phrases may include the DISTINCT keyword. It is not necessary anyway, since duplicate rows are removed.

Usage of Union SELECT * FROM EMP WHERE Sal = UNION SELECT * FROM EMP WHERE M-S = ‘S’ ; is the same as SELECT * FROM EMP WHERE Sal = OR M-S = ‘S’ ;  it is rarely useful to do a Union of two tables that are both derived from the same original table - use a Restrict type SQL statement instead.

Usage of Difference SELECT * FROM EMP WHERE Sal = EXCEPT SELECT * FROM EMP WHERE M-S = ‘S’ ; is the same as SELECT * FROM EMP WHERE Sal = AND NOT M-S = ‘S’ ;  it is rarely useful to do a Difference of two tables that are both derived from the same original table - use a Restrict type SQL statement instead.

Usage of Intersect SELECT * FROM EMP WHERE Sal = INTERSECT SELECT * FROM EMP WHERE M-S = ‘S’ ; is the same as SELECT * FROM EMP WHERE Sal = AND M-S = ‘S’ ;  it is rarely useful to do an Intersect of two tables that are both derived from the same original table - use a Restrict type SQL statement instead.

Usage of Set Operators in General l Set operators are rarely used when their operands come from the same relation, because it is usually simpler to use a Restrict condition in the WHERE phrase of a single SQL statement. l Set operators can be very useful when the operands come from different relations, but they are not often used in this way, because a consequence of the type incompatibilites of the different tables in a DB is that there are limited useful opportunities for such operations.  in practice Join operations are much more frequently used for ‘merging relations’ than are Set operations.

Using Multiple Set Operators in SQL Example :- SELECT ….. FROM ….. ……….. UNION SELECT ….. FROM ….. ……….. INTERSECT SELECT ….. FROM ….. ……….. etc Sequence of execution