Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6: Set Operators

Similar presentations


Presentation on theme: "Chapter 6: Set Operators"— Presentation transcript:

1 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

2 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

3 Objectives Describe SQL set operators and modifiers.

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

5 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

6 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>

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

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

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

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

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

12 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.

13 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.

14

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

16 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

17

18 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

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

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

21 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.

22 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

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

24 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 ...

25 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 ...

26 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 ...

27 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 ...

28 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 ...

29 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 ...

30 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 ...

31 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 ...

32 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 ...

33 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 ...

34 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 ...

35 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

36

37 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

38 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

39 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; ...

40 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

41 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

42 Non-Sales Staff Employees
The EXCEPT Operator Partial PROC SQL Output (10 out of 259) Non-Sales Staff Employees Employee_ID Job_Title ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ Director Administration Manager Secretary I Office Assistant II Office Assistant III Warehouse Assistant II Warehouse Assistant I Warehouse Assistant III Security Guard II Security Guard I

43

44 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

45 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

46 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

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

48 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

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

50

51 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.

52 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

53

54 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

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

56 INTERSECT Common unique rows from both result sets are selected.

57 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.

58 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

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

60 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 ...

61 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 ...

62 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 ...

63 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

64

65 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

66 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

67 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 ...

68 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 ...

69 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

70 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

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

72

73 Exercise This exercise reinforces the concepts discussed previously.

74 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

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

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

77 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 ,234, Total Paid to ALL Level II Staff ,456, Total Paid to ALL Level III Staff 2,123,456

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

79 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 ...

80 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 ...

81 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 ...

82 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

83 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 ...

84 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 ...

85 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 ...

86 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

87 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 ,234, Total Paid to ALL Level II Staff ,456, Total Paid to ALL Level III Staff 2,123,456

88 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

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

90 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.

91

92 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.

93 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

94

95 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

96 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.

97 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.

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

99 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

100 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

101 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.

102 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

103 The OUTER UNION Operator Results
PROC SQL Output Employee Annual Employee ID Employee Job Title Salary ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ Secretary I $27,110 Office Assistant I $26,940 Office Assistant I $26, Office Administrator I $31, Office Assistant II $26, Secretary II $27, Office Assistant II $26, Office Administrator II $34, Secretary II $29, Office Assistant III $30, Secretary III $28, Office Assistant III $29, Office Assistant III $29, Secretary III $29, Secretary IV $31, Office Assistant IV $32,745

104 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

105

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

107 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

108 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.

109 Exercise This exercise reinforces the concepts discussed previously.

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

111 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

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

113 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 ƒƒƒƒƒƒƒƒƒ Def PROC SQL Output

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

115 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

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

117 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 Abc Xyz Ghi PROC SQL Output


Download ppt "Chapter 6: Set Operators"

Similar presentations


Ads by Google