Chapter 6: Set Operators

Slides:



Advertisements
Similar presentations
Using the Set Operators
Advertisements

Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
Introduction to SQL Session 2 Retrieving Data From Multiple Tables.
11 Chapter 3: Displaying Query Results 3.1 Presenting Data 3.2 Summarizing Data.
XP Chapter 3 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Analyzing Data For Effective Decision Making.
9 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Using the Set Operators Assist. Prof. Pongpisit Wuttidittachotti, Ph.D. Faculty.
Chapter 6 SQL: Data Manipulation Cont’d. 2 ANY and ALL u ANY and ALL used with subqueries that produce single column of numbers u ALL –Condition only.
PROC SQL – Select Codes To Master For Power Programming Codes and Examples from SAS.com Nethra Sambamoorthi, PhD Northwestern University Master of Science.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
Enhancements to the GROUP BY Clause Fresher Learning Program January, 2012.
Database Programming Sections 5– GROUP BY, HAVING clauses, Rollup & Cube Operations, Grouping Set, Set Operations 11/2/10.
1 Chapter 4: Creating Simple Queries 4.1 Introduction to Querying Data 4.2 Filtering and Sorting Data 4.3 Creating New Columns with an Expression 4.4 Grouping.
11 Chapter 2: Basic Queries 2.1: Overview of the SQL Procedure 2.2: Specifying Columns 2.3: Specifying Rows.
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
Concepts of Database Management Seventh Edition
11 Chapter 7: Creating Tables and Views 7.1 Creating Views with the SQL Procedure 7.2 Creating Tables with the SQL Procedure (Self-Study) 7.3 Integrity.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
1 Chapter 6: Using Prompts in Tasks and Queries 6.1 Prompting in Projects 6.2 Creating and Using Prompts in Tasks 6.3 Creating and Using Prompts in Queries.
15 Copyright © Oracle Corporation, All rights reserved. Using SET Operators.
Copyright © 2004, Oracle. All rights reserved. Using the Set Operators.
1 Chapter 7: Customizing and Organizing Project Results 7.1 Combining Results 7.2 Updating Results 7.3 Customizing the Output Style (Self-Study)
Chapter 4: Combining Tables Vertically using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
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.
11 Chapter 4: Subqueries 4.1: Noncorrelated Subqueries 4.2: Correlated Subqueries (Self-Study)
 CONACT UC:  Magnific training   
IFS180 Intro. to Data Management Chapter 10 - Unions.
Chapter 3: Getting Started with Tasks
Session 1 Retrieving Data From a Single Table
Chapter 10: Accessing Relational Databases (Self-Study)
Restricting and Sorting Data
Retrieving Data Using the SQL SELECT Statement
SQL Query Getting to the data ……..
Writing Basic SQL SELECT Statements
Basic select statement
Using the Set Operators
Basic Queries Specifying Columns
Chapter 5: Using DATA Step Arrays
Using Subqueries to Solve Queries
Writing Basic SQL SELECT Statements
Chapter 18: Modifying SAS Data Sets and Tracking Changes
David M. Kroenke and David J
Using the Set Operators
More SQL Nested and Union queries, and more
Noncorrelated subquery
Correlated Subqueries
Creating and Modifying Queries
Chapter Name SQL: Data Manipulation
A more complex example.
Displaying Queries 2 Display a query’s results in a specified order.
Subsetting Rows with the WHERE clause
Outer Joins Inner joins returned only matching rows. When you join tables, you might want to include nonmatching rows as well as matching rows.
Combining Data Sets in the DATA step.
Writing Basic SQL SELECT Statements
5 The EXCEPT Operator Unique rows from the first result set that are not found in the second result set are selected.
Lab 3 and HRP259 Lab and Combining (with SQL)
Reporting Aggregated Data Using the Group Functions
The INTERSECT Operator
3 Specifying Rows.
3 Views.
Subqueries.
SQL set operators and modifiers.
Reporting Aggregated Data Using the Group Functions
UNION Operator keywords Displays all rows from both the tables
Using the Set Operators
Reporting Aggregated Data Using the Group Functions
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Manipulating Data Lesson 3.
Restricting and Sorting Data
Aggregating Data Using Group Functions
Presentation transcript:

Chapter 6: Set Operators 5 Chapter 6: Set Operators 6.1: Introduction to Set Operators 6.2: The EXCEPT Operator 6.3: The INTERSECT Operator 6.4: The UNION Operator 6.5: The OUTER UNION Operator

Chapter 6: Set Operators 5 Chapter 6: Set Operators 6.1: Introduction to Set Operators 6.2: The EXCEPT Operator 6.3: The INTERSECT Operator 6.4: The UNION Operator 6.5: The OUTER UNION Operator

Objectives Describe SQL set operators and modifiers.

Types of Set Operators Set operators vertically combine rows from two result sets. There are four set operators: Except Union Intersect Outer Union

Default Behavior of Set Operators Columns are matched by position and must be the same data type. Column names in the final result set are determined by the first result set. EXCEPT INTERSECT UNION All columns from both result sets are selected. OUTER UNION

Set Operator Syntax General form of an SQL query using a set operator: The set operator operates on the result sets produced by the two SELECT statements, not on the actual tables themselves. SELECT … EXCEPT | INTERSECT | UNION | OUTER UNION <CORR> <ALL>

Types of Set Operators EXCEPT Unique rows from the first table that are not found in the second table are selected. select * from one except from two; ...

Types of Set Operators INTERSECT Common unique rows from both tables are selected. select * from one intersect from two; ...

Types of Set Operators UNION Unique rows from both tables are selected with columns overlaid. select * from one union from two; ...

Types of Set Operators OUTER UNION All rows from both tables, unique as well as non-unique, are selected. Columns are not overlaid. select * from one outer union from two;

Modifiers You can use two modifiers to modify the behavior of set operators: ALL CORRESPONDING

Modifiers ALL does not remove duplicate rows, and thus avoids an extra pass through the data. Use the ALL modifier for better performance when it is possible. is not allowed in connection with an OUTER UNION operator. It is implicit.

Modifiers CORRESPONDING overlays columns by name, instead of by position removes any columns not found in both tables when used in EXCEPT, INTERSECT, and UNION operations causes common columns to be overlaid when used in OUTER UNION operations can be abbreviated as CORR.

6.01 Poll By default the EXCEPT, INTERSECT, and UNION set operators remove duplicate rows from the query output.  True  False True

6.01 Poll – Correct Answer By default the EXCEPT, INTERSECT, and UNION set operators remove duplicate rows from the query output.  True  False Type answer here

Chapter 6: Set Operators 5 Chapter 6: Set Operators 6.1: Introduction to Set Operators 6.2: The EXCEPT Operator 6.3: The INTERSECT Operator 6.4: The UNION Operator 6.5: The OUTER UNION Operator

Objectives Describe the SQL process when you use the EXCEPT set operator and keywords. Use the EXCEPT set operator.

EXCEPT Unique rows from the first result set that are not found in the second result set are selected.

Business Scenario Create a report that displays the employee identification number and job title of the non-Sales staff employees. Considerations: The orion.Employee_organization table contains information about all current Orion Star employees. The orion.Sales table contains information about current Sales employees only.

The EXCEPT Operator You need a query that returns information from rows that exist in orion.Employee_organization, but not in orion.Sales. The EXCEPT operator could be useful. orion.Employee_organization non-sales orion.Sales

Flow Diagram: EXCEPT Operator CORR Yes Remove nonmatching columns. No ALL No Yes Remove duplicate rows. Remove matching rows. End

The EXCEPT Operator Table TWO Table ONE Display the unique rows in Table ONE that are not found in Table TWO. Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w select * from one except from two; s106d01 ...

The EXCEPT Operator Table TWO Table ONE The SQL processor removes duplicate rows within the tables. Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w select * from one except from two; s106d01 ...

The EXCEPT Operator Intermediate Results Table ONE Table TWO The SQL processor creates an intermediate result set by returning the rows that are found only in Table ONE. Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w Intermediate Results   1 a b 2 c 4 e 6 g select * from one except from two; s106d01 ...

The EXCEPT Operator Final Results Table ONE Table TWO The column names are determined by Table ONE in the final result set. Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w Final Results X A 1 a b 2 c 4 e 6 g select * from one except from two; s106d01 ...

The EXCEPT Operator with ALL Display the rows (duplicates included) that are found in Table ONE, but not in Table TWO. Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w select * from one except all from two; s106d02 ...

The EXCEPT Operator with ALL The SQL processor creates an intermediate result set by returning the rows that are found only in Table ONE. Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w Intermediate Results   1 a b 2 c 4 e 6 g select * from one except all from two; s106d02 ...

The EXCEPT Operator with ALL The column names are determined by Table ONE in the final result set. Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w Final Results X A 1 a b 2 c 4 e 6 g select * from one except all from two; s106d02 ...

The EXCEPT Operator with CORR Display the unique rows that exist in Table ONE and not in Table TWO, based on same-named columns. Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w select * from one except corr from two; s106d03 ...

The EXCEPT Operator with CORR The SQL processor eliminates any columns not found in both tables. Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w select * from one except corr from two; s106d03 ...

The EXCEPT Operator with CORR The SQL processor eliminates duplicate rows. Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w select * from one except corr from two; s106d03 ...

The EXCEPT Operator with CORR The SQL processor creates an intermediate result set by returning rows that are found only in Table ONE. Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w Intermediate Results  4 6 select * from one except corr from two; s106d03 ...

The EXCEPT Operator with CORR The SQL processor creates an intermediate result set by returning rows that are found only in Table ONE. Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w Final Results X 4 6 select * from one except corr from two; s106d03

6.02 Quiz Table BETA Table ALPHA What are the results when you combine ALL with CORR? Run the program s106a01 and review the results. Table ALPHA X A 1 x y 3 z 4 v 5 w Table BETA X B 1 x 2 y 3 z v 5 select * from alpha except all corr from beta; X=1 X=4 s106a01

6.02 Quiz – Correct Answer Table BETA Table ALPHA What are the results when you combine ALL with CORR? Table ALPHA X A 1 x y 3 z 4 v 5 w Table BETA X B 1 x 2 y 3 z v 5 select * from alpha except all corr from beta; Type answer here X 1 4 Final result set

Step 1: Using ALL with CORR CORR specifies that only same-named columns be used. ALL specifies that all values of X be used, including duplicates. Step 1 Table ALPHA X A 1 x y 3 Z 4 v 5 w Table BETA X B 1 x 2 y 3 z v 5 select * from alpha except all corr from beta; ...

Step 2: Using ALL with CORR EXCEPT specifies that only X values found in ALPHA and not in BETA are used. Step 2 Table ALPHA X A 1 x y 3 Z 4 v 5 w Table BETA X B 1 x 2 y 3 z v 5 select * from alpha except all corr from beta; X 1 4 Final result set

Business Scenario (Review) Create a report that displays the employee identification number and job title of the employees who are not Sales staff. non-sales orion.Employee_organization orion.Sales proc sql; select Employee_ID, Job_Title from orion.Employee_organization except all from orion.Sales; quit; s106d04

Non-Sales Staff Employees The EXCEPT Operator Partial PROC SQL Output (10 out of 259) Non-Sales Staff Employees Employee_ID Job_Title ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ 120101 Director 120104 Administration Manager 120105 Secretary I 120106 Office Assistant II 120107 Office Assistant III 120108 Warehouse Assistant II 120109 Warehouse Assistant I 120110 Warehouse Assistant III 120111 Security Guard II 120112 Security Guard I

6.03 Quiz Answer the questions about the following program: Why is the CORR keyword not used in this example? 2. Would adding the CORR keyword to this example change the outcome? select Employee_ID, Job_Title from orion.Employee_organization except all from orion.Sales; 1. Both SELECT lists specify the same column names in the same order, so CORR is not necessary. 2. No, adding CORR produces the same results. s106a02

6.03 Quiz – Correct Answer Answer the questions about the following program: Why is the CORR keyword not used in this example? Both SELECT lists specify the same column names in the same order, so CORR is not necessary. select Employee_ID, Job_Title from orion.Employee_organization except all from orion.Sales; Type answer here

6.03 Quiz – Correct Answer Answer the questions about the following program: Why is the CORR keyword not used in this example? Both SELECT lists specify the same column names in the same order, so CORR is not necessary. 2. Would adding the CORR keyword in this example change the outcome? No, adding CORR produces the same results. select Employee_ID, Job_Title from orion.Employee_organization except all from orion.Sales; Type answer here

Using the CORR Keyword This demonstration illustrates the use of the CORR keyword with the EXCEPT set operator. s106d04a s106d04a

The EXCEPT Operator This query can easily become an in-line view used to determine how many managers, who are not Sales staff, are employed at Orion Star. proc sql; select count(*) 'No. Non-Sales Managers' from (select distinct Manager_ID from orion.Employee_organization except all select Employee_ID from orion.Sales) ; quit; s106d05  A manager might have multiple direct reports, so use the DISTINCT keyword in the first part of the query. You can confidently use the ALL keyword because the first query returns distinct values, and the Sales table contains no duplicate records. s106d05

The EXCEPT Operator PROC SQL Output No. Non-Sales Managers ƒƒƒƒƒƒƒƒƒ 48

6.04 Poll By default, the EXCEPT set operator selects all the rows from the first result set that are not in the second result set.  True  False False. By default, the EXCEPT Operator eliminates duplicate rows first.

6.04 Poll – Correct Answer By default, the EXCEPT set operator selects all the rows from the first result set that are not in the second result set.  True  False By default, the EXCEPT operator eliminates duplicate rows first. It selects only unique rows from the first result set that are not in the second result set. Type answer here

Chapter 6: Set Operators 5 Chapter 6: Set Operators 6.1: Introduction to Set Operators 6.2: The EXCEPT Operator 6.3: The INTERSECT Operator 6.4: The UNION Operator 6.5: The OUTER UNION Operator

Objectives Describe the SQL process when using the INTERSECT set operator and keywords. Use the INTERSECT set operator.

INTERSECT Common unique rows from both result sets are selected.

Business Scenario Orion Star frequently hires experienced Sales staff at higher levels on the assumption that they will be more productive than inexperienced personnel. Create a report that displays the employee identification number of current Level III and Level IV Sales staff hired in 2004, who made at least one sale by the end of 2005. Considerations: The orion.Order_fact table contains information on all sales. The orion.Sales table contains information about current Sales employees, including job titles and hire dates.

The INTERSECT Operator You need a query that returns information from rows that exist in both orion.Sales and orion.Order_fact. The INTERSECT operator could be useful. orion.Sales sales by Sales staff orion.Order_fact

Flow Diagram: INTERSECT Operator CORR Yes Remove nonmatching columns. No ALL No Yes Remove duplicate rows. Save matching rows. End

The INTERSECT Operator Display the unique rows common to Table ONE and Table TWO. Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w select * from one intersect from two; s106d06 ...

The INTERSECT Operator The SQL processor removes duplicate rows within the tables. Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w select * from one intersect from two; s106d06 ...

The INTERSECT Operator The SQL processor creates an intermediate result set by returning the rows that are found in both tables. Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w Intermediate Results   3 v select * from one intersect from two; s106d06 ...

The INTERSECT Operator The column names are determined by Table ONE in the final result set. Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w Final Results X A 3 v select * from one intersect from two; s106d06

6.05 Quiz Submit the program s106a03 and review the results. Add the ALL keyword to the second PROC SQL step and re-submit. Will the addition of the ALL keyword have any effect on the output? Table AAA X A 1 a b 2 c 3 v 4 e 6 g Table BBB X B 1 a 2 z 3 v 5 w select * from aaa intersect all from bbb; Yes. There are duplicate rows common to both tables. The ALL keyword will include the duplicate rows. s106a03

6.05 Quiz – Correct Answer Will the addition of the ALL keyword have any effect on the output? Yes. There are duplicate rows common to both tables. The ALL keyword will include the duplicate rows. Table BBB X B 1 a 2 z 3 v 5 w Final Results X A 1 a 3 v Table AAA X A 1 a b 2 c 3 v 4 e 6 g select * from aaa intersect all from bbb; Type answer here s106a03

The INTERSECT Operator with CORR Display the unique rows common to Table ONE and Table TWO, based on the same-named columns. Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w select * from one intersect corr from two; s106d07 ...

The INTERSECT Operator with CORR The SQL processor eliminates any columns not found in both tables. Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w select * from one intersect corr from two; s106d07 ...

The INTERSECT Operator with CORR The SQL processor eliminates duplicate rows and rows that are not common to Table ONE and Table TWO. Final Results X 1 2 3 Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w select * from one intersect corr from two; s106d07

Business Scenario (Review) Create a report that displays the employee identification number of current Level III and Level IV Sales staff hired in 2004, who made at least one sale by the end of 2005. orion.Sales proc sql; select Employee_ID from orion.Sales where year(Hire_date)=2004 and scan(Job_Title,-1) in ("III","IV") intersect all select distinct Employee_ID from orion.Order_fact where year(Order_date) le 2005; quit; sales by Sales staff s106d08 orion.Order_fact s106d08

The INTERSECT Operator PROC SQL Output Employee ID ƒƒƒƒƒƒƒƒƒƒƒƒ 120179

Exercise This exercise reinforces the concepts discussed previously.

Chapter 6: Set Operators 5 Chapter 6: Set Operators 6.1: Introduction to Set Operators 6.2: The EXCEPT Operator 6.3: The INTERSECT Operator 6.4: The UNION Operator 6.5: The OUTER UNION Operator

Objectives Describe the SQL process when you use the UNION set operator and keywords. Use the UNION set operator.

UNION Both result sets are combined, and then unique rows are selected with columns overlaid.

Payroll Report for Level I, II, and III Employees Business Scenario The management team needs a payroll report for Level I, II, and III Orion Star employees. The UNION operator could be useful here. Below is a sketch of the desired report: Payroll Report for Level I, II, and III Employees _______________________________________ Total Paid to ALL Level I Staff 1,234,567 Total Paid to ALL Level II Staff 1,456,789 Total Paid to ALL Level III Staff 2,123,456

Flow Diagram: UNION Operator CORR Yes Remove nonmatching columns. No Concatenate tables. ALL No Yes Remove duplicate rows. End

The UNION Operator Display all of the unique rows from both Table ONE and Table TWO. Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w select * from one union from two; s106d09 ...

The UNION Operator The SQL processor creates an intermediate result set by concatenating and sorting Table ONE and Table TWO. Intermediate Results   1 a b x 2 c y 3 v z 4 e 5 w 6 g Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w select * from one union from two; s106d09 ...

The UNION Operator The SQL processor removes duplicate rows from the intermediate result. Intermediate Results   1 a b x 2 c y 3 v z 4 e 5 w 6 g Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w select * from one union from two; s106d09 ...

The UNION Operator Final Result Set Table ONE Table TWO Final Results X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w Final Results X A 1 a b x 2 c y 3 v z 4 e 5 w 6 g select * from one union from two; s106d09

The UNION Operator with CORR Display all of the unique rows of same-named columns in Table ONE and Table TWO. Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w select * from one union corr from two; s106d10 ...

The UNION Operator with CORR The SQL processor creates an intermediate result set by concatenating and sorting data from same-named columns. Intermediate Results X 1 2 3 4 5 6 Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w select * from one union corr from two; s106d10 ...

The UNION Operator with CORR The SQL processor eliminates duplicate rows. Intermediate Results X 1 2 3 4 5 6 Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w select * from one union corr from two; s106d10 ...

The UNION Operator with CORR Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w Final Results X 1 2 3 4 5 6 select * from one union corr from two; s106d10

Business Scenario (Review) The management team needs a payroll report for the Level I, II, and III Orion Star employees. The orion.Staff table contains the job title and salary information for all Orion Star employees. Use the UNION set operator to combine the results from each query that calculates the total paid to all Level I, II, and III employees. Payroll Report for Level I, II, and III Employees _______________________________________ Total Paid to ALL Level I Staff 1,234,567 Total Paid to ALL Level II Staff 1,456,789 Total Paid to ALL Level III Staff 2,123,456

The UNION Operator proc sql; select 'Total Paid to ALL Level I Staff', sum(Salary) format=comma12. from orion.Staff where scan(Job_Title,-1, ' ')='I' union select 'Total Paid to ALL Level II Staff', where scan(Job_Title,-1,' ')='II' select 'Total Paid to ALL Level III Staff', where scan(Job_Title,-1,' ')='III'; quit; s106d11 s106d11

The UNION Operator PROC SQL Output Payroll Report for Level I, II, and III Employees ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ Total Paid to ALL Level I Staff 3,582,630 Total Paid to ALL Level II Staff 3,569,580 Total Paid to ALL Level III Staff 2,296,425

Set Operators and Keywords: Flow Diagrams CORR ALL Remove duplicate rows. Remove matching rows. EXCEPT Remove nonmatching columns. No Yes End INTERSECT CORR ALL Remove duplicate rows. Save matching rows. Remove nonmatching columns. No Yes End UNION CORR ALL Remove duplicate rows. No Yes End Concatenate tables. Remove nonmatching columns.

6.06 Poll Is it more or less efficient to use the ALL keyword in a set operation?  More efficient  Less efficient More efficient, because SQL doesn't take a second pass through the data to eliminate duplicates.

6.06 Poll – Correct Answer Is it more or less efficient to use the ALL keyword in a set operation?  More efficient  Less efficient No de-duplication is required. Type answer here

Chapter 6: Set Operators 5 Chapter 6: Set Operators 6.1: Introduction to Set Operators 6.2: The EXCEPT Operator 6.3: The INTERSECT Operator 6.4: The UNION Operator 6.5: The OUTER UNION Operator

Objectives Describe SQL OUTER UNION set operators and keywords. Use the OUTER UNION set operators. Compare the SQL set operators to traditional SAS programming tools.

Business Scenario Write a query to display the employee ID numbers, job titles, and salaries for all Administrative staff. The data that you need is in four separate data sets with identical structures. The OUTER UNION operator could be useful here.

OUTER UNION All rows from both result sets, unique as well as non-unique, are selected. Columns are not overlaid.

The OUTER UNION Operator Display all data values from Table ONE and Table TWO. Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w Final Results X A B 1 a . b 2 c 3 v 4 e 6 g x y z 5 w select * from one outer union from two; s106d12

The OUTER UNION Operator with CORR Display all data values from Table ONE and Table TWO, but overlay common columns. Table ONE X A 1 a b 2 c 3 v 4 e 6 g Table TWO X B 1 x 2 y 3 z v 5 w Final Results X A B 1 a b 2 c 3 v 4 e 6 g x y z 5 w select * from one outer union corr from two; s106d13

Business Scenario (Review) Write a query to display the employee ID numbers, job titles, and salaries for all Administrative staff. Considerations: The data that you need is in four separate data sets with identical structures: work.Admin_I work.Admin_II work.Admin_III work.Admin_IV There are no data sets that contain specific job titles for the Admin employees. Therefore, there is a step in the s106d14 program that creates the four WORK tables that are needed for this example.

The OUTER UNION Operator with CORR The OUTER UNION operator with the CORR keyword might be useful here. proc sql; select * from work.Admin_I outer union corr from work.Admin_II from work.Admin_III from work.Admin_IV; There are no data sets that contain specific job titles for the Admin employees. Therefore, there is a step in the s106d14 program that creates the four WORK tables that are needed for this example. s106d14

The OUTER UNION Operator Results PROC SQL Output Employee Annual Employee ID Employee Job Title Salary ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ 120105 Secretary I $27,110 120992 Office Assistant I $26,940 120993 Office Assistant I $26,260 120994 Office Administrator I $31,645 120106 Office Assistant II $26,960 120662 Secretary II $27,045 120749 Office Assistant II $26,545 120995 Office Administrator II $34,850 121147 Secretary II $29,145 120107 Office Assistant III $30,475 120267 Secretary III $28,585 120667 Office Assistant III $29,980 120799 Office Assistant III $29,070 121146 Secretary III $29,320 120266 Secretary IV $31,750 120996 Office Assistant IV $32,745

SQL versus Traditional SAS Programming The following programs produce the same report: data three; set one two; run; proc print data=three noobs; proc sql; select * from one outer union corr select * from two; quit; s105d15 proc append base=one data=two; run; proc print data=one noobs; s105d15

6.07 Multiple Choice Poll What DATA step statement yields the same results as OUTER UNION CORR? MERGE APPEND SET STACK c. SET

6.07 Multiple Choice Poll – Correct Answer What DATA step statement yields the same results as OUTER UNION CORR? MERGE APPEND SET STACK Type answer here

SQL Set Operators versus the DATA Step Key Points SQL DATA Step Number of tables processed simultaneously Limited to two tables Not limited by SAS; limited only by system resources. Column handling Depends on the SET operator and keywords All columns from all data sets are included in output data set(s), unless specified using data set options or program logic. Duplicate row handling All rows are output unless specified using data set options or program logic. Use the techniques discussed in the "Additional PROC SQL Features" chapter to benchmark different techniques.  Also consider PROC APPEND for vertical table combination.

Exercise This exercise reinforces the concepts discussed previously.

Chapter Review How many rows will this query produce? Table 1 Table 2 ID Var 1 Abc 2 Def 3 Ghi Table 2 ID Var 1 Abc 2 Zxy 3 Ghi proc sql; select * from table1 INTERSECT from table2 ; quit;

Chapter Review Answers 1. How many rows will this query produce? Two Table 1 ID Var 1 Abc 2 Def 3 Ghi Table 2 ID Var 1 Abc 2 Zxy 3 Ghi proc sql; select * from table1 INTERSECT from table2 ; quit; ID Var ƒƒƒƒƒƒƒƒƒ 1 Abc 3 Ghi PROC SQL Output

Chapter Review 2. How many rows will this query produce? Table 1 ID Var 1 Abc 2 Def 3 Ghi Table 2 ID Var 1 Abc 2 Zxy 3 Ghi proc sql; select * from table1 EXCEPT from table2 ; quit;

Chapter Review Answers 2. How many rows will this query produce? One Table 1 ID Var 1 Abc 2 Def 3 Ghi Table 2 ID Var 1 Abc 2 Zxy 3 Ghi proc sql; select * from table1 EXCEPT from table2 ; quit; ID Var ƒƒƒƒƒƒƒƒƒ 2 Def PROC SQL Output

Chapter Review 3. Will the addition of the CORR and ALL keywords change the number of rows that this query produces? Table 1 ID Var 1 Abc 2 Def 3 Ghi Table 2 ID Var 1 Abc 2 Zxy 3 Ghi proc sql; select * from table1 EXCEPT CORR ALL from table2 ; quit;

Chapter Review Answers 3. Will the addition of the CORR and ALL keywords change the number of rows that this query produces? No Table 1 ID Var 1 Abc 2 Def 3 Ghi Table 2 ID Var 1 Abc 2 Zxy 3 Ghi proc sql; select * from table1 EXCEPT CORR ALL from table2 ; quit; ID Var ƒƒƒƒƒƒƒƒ 2 Def PROC SQL Output

Chapter Review 4. How many columns will this query produce? Table 1 ID Var1 1 Abc 2 Def 3 Ghi Table 2 ID Var2 1 Abc 2 Zxy 3 Ghi proc sql; select * from table1 OUTER UNION CORR from table2 ; quit;

Chapter Review Answers 4. How many columns will this query produce? Three Table 1 ID Var1 1 Abc 2 Def 3 Ghi Table 2 ID Var2 1 Abc 2 Zxy 3 Ghi proc sql; select * from table1 OUTER UNION CORR from table2 ; quit; ID Var Var2 ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ 1 Abc 2 Def 3 Ghi 1 Abc 2 Xyz 3 Ghi PROC SQL Output