Download presentation
Presentation is loading. Please wait.
Published byApril Heath Modified over 9 years ago
1
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.
2
Relational Union (1) A Union B A B
3
Relational Union (2) A Union B A B
4
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.
5
Relational Difference (1) A B A Difference B
6
Relational Difference (2) A B A Difference B
7
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)
8
Relational Intersect (1) A Intersect B A B
9
Relational Intersect (2) A B A Intersect B
10
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.
11
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.
12
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’
13
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.
14
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.
15
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.
16
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.
17
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 ;
18
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).
19
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.
20
Usage of Union SELECT * FROM EMP WHERE Sal = 20000 UNION SELECT * FROM EMP WHERE M-S = ‘S’ ; is the same as SELECT * FROM EMP WHERE Sal = 20000 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.
21
Usage of Difference SELECT * FROM EMP WHERE Sal = 20000 EXCEPT SELECT * FROM EMP WHERE M-S = ‘S’ ; is the same as SELECT * FROM EMP WHERE Sal = 20000 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.
22
Usage of Intersect SELECT * FROM EMP WHERE Sal = 20000 INTERSECT SELECT * FROM EMP WHERE M-S = ‘S’ ; is the same as SELECT * FROM EMP WHERE Sal = 20000 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.
23
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.
24
Using Multiple Set Operators in SQL Example :- SELECT ….. FROM ….. ……….. UNION SELECT ….. FROM ….. ……….. INTERSECT SELECT ….. FROM ….. ……….. etc Sequence of execution
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.