Chapter 4: Combining Tables Vertically using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Slides:



Advertisements
Similar presentations
Using the Set Operators
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.
Relational Database Operators
Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Managing Processing Using PROC SQL Chapter 8 1 Imelda Go, John Grego, Jennifer Lasecki, 2011.
Correlated Subqueries Chapter 2 Supplement 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Performing Queries Using PROC SQL Chapter 1 1 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
1 Combining (with SQL) HRP223 – 2010 October 27, 2009 Copyright © Leland Stanford Junior University. All rights reserved. Warning: This presentation.
Introduction to SQL Session 2 Retrieving Data From Multiple Tables.
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
The Relational Database Model. 2 Objectives How relational database model takes a logical view of data Understand how the relational model’s basic components.
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.
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
PROC SQL – Select Codes To Master For Power Programming Codes and Examples from SAS.com Nethra Sambamoorthi, PhD Northwestern University Master of Science.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Chapter 18: Modifying SAS Data Sets and Tracking Changes 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
Chapter 3: Combining Tables Horizontally using PROC SQL 1 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Chapter 2 Basic SQL SELECT Statements
Chapter 10:Processing Macro Variables at Execution Time 1 STAT 541 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Chapter 2 Basic SQL SELECT Statements Oracle 10g: SQL.
McGraw-Hill Technology Education © 2004 by the McGraw-Hill Companies, Inc. All rights reserved. Office Access 2003 Lab 3 Analyzing Data and Creating Reports.
Chapter 15: Combining Data Horizontally 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE)
3 1 Chapter 3 The Relational Database Model Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 16: Using Lookup Tables to Match Data 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Chapter 22: Using Best Practices 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
15 Copyright © Oracle Corporation, All rights reserved. Using SET Operators.
Copyright © 2004, Oracle. All rights reserved. Using the Set Operators.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
3 1 Database Systems The Relational Database Model.
1 Chapter 2 Basic SQL SELECT Statements. 2 Chapter Objectives Distinguish between an RDBMS and an ORDBMS Identify keywords, mandatory clauses, and optional.
DAY 18: ACCESS CHAPTER 3 Tazin Afrin October 22,
Chapter 23: Selecting Efficient Sorting Strategies 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Chapter 21: Controlling Data Storage Space 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Chapter 6: Modifying and Combining Data Sets  The SET statement is a powerful statement in the DATA step DATA newdatasetname; SET olddatasetname;.. run;
Chapter 14: Combining Data Vertically 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
1 SQL Chapter 9 – 8 th edition With help from Chapter 2 – 10 th edition.
Writing Basic SQL SELECT Statements Lecture
 CONACT UC:  Magnific training   
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Chapter 6: Set Operators
The Relational Database Model
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Using the Set Operators
Writing Basic SQL SELECT Statements
Chapter 18: Modifying SAS Data Sets and Tracking Changes
David M. Kroenke and David J
Using the Set Operators
Former Chapter 23: Selecting Efficient Sorting Strategies
Chapter 24 (4th ed.): Creating Functions
Combining (with SQL) HRP223 – 2013 October 30, 2013
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.
The INTERSECT Operator
3 Specifying Rows.
Chapter 8 Advanced SQL.
SQL set operators and modifiers.
UNION Operator keywords Displays all rows from both the tables
Using the Set Operators
Relational Database Operators
Presentation transcript:

Chapter 4: Combining Tables Vertically using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina

Outline  Set Operations  Combining Columns  Keywords (CORR and ALL)  Except  Intersect  Union  Outer Union 2

Sample Tables Google Grego NameResidence JohnColumbia, SC LauraCambridge, MA MelissaLos Angeles, CA MichaelOrlando, FL JosephLondon, England MelissaLos Angeles, CA MarkNapierville, IL JohnWashington, DC Bing Grego NameCity MarkNapierville, IL JosephLondon, England MichaelOrlando, FL MelissaLos Angeles, CA VGYerevan, Armenia 3

Combining Tables Vertically  Standard syntax: proc sql; select * from google (except/intersect/union/outer union) (corr/all) select * from bing; 4

Combining Tables Vertically  Multiple SELECT queries processed separately, then combined using set operator  Set operators select unique rows by default  Set operators overlay columns by default 5

Combining Tables Vertically  Sequential columns in each query should be the same type  For three or more queries, set operators are evaluated sequentially  Keywords ALL and CORR modify the set operators 6

EXCEPT  Selects unique rows from first table that do not occur in the second table  Columns are simply overlaid (even if names are different)  Columns inherit names from the first table 7

EXCEPT proc sql; select * from google except select * from bing; quit; NameResidence JohnColumbia, SC JohnWashington, DC LauraCambridge, MA 8

EXCEPT  In first sweep, the second occurrence of Melissa in google would be removed  In second sweep, Mark, Joseph, Michael, and the first occurrence of Melissa would be removed  The second column is called Residence rather than City  Data is sorted 9

EXCEPT 10

EXCEPT ALL  Use the keyword ALL after EXCEPT so that duplicate rows in the first table that do not occur in the second table are not eliminated.  The second Melissa does not have a match.  Data is sorted NameResidence JohnColumbia, SC JohnWashington, DC LauraCambridge, MA MelissaLos Angeles, CA 11

EXCEPT ALL  We would have different results if either John from SC, Laura from MA or John from DC were listed more than once.  Or….if Melissa from CA was not listed in the second table 12

EXCEPT CORR  The keyword CORR displays only columns with the same name in both tables  Columns are selected first  Unique rows are then extracted from the first table that do not appear in the second table  This ordering may generate sparser output than expected 13

EXCEPT CORR proc sql; select * from google except corr select * from bing; quit; Name John Laura 14

EXCEPT CORR and ALL  If both keywords ALL and CORR are used with EXCEPT –Unique and duplicate rows from the first table will be saved, unless they have matches in the second table –Only columns with the same name in both tables will be displayed 15

EXCEPT CORR and ALL  All occurrences of John are retained  The unmatched duplicate occurrence of Melissa is retained  The unmatched occurrence of Laura is retained  Data is sorted 16 Name John Laura Melissa

INTERSECT  Selects unique rows common to both tables  Column labels are ignored proc sql; select * from google intersect select * from bing; quit; 17

INTERSECT 18

INTERSECT  Melissa only appears once  The second column is labeled based on the first data set  Data is sorted NameResidence JosephLondon England MarkNapierville, IL MelissaLos Angeles, CA MichaelOrlando, FL 19

INTERSECT ALL  Selects unique and duplicate rows common to both tables  Column labels are ignored  Data is sorted 20

INTERSECT ALL proc sql; select * from google intersect all select * from bing; quit; NameResidence JosephLondon England MarkNapierville, IL MelissaLos Angeles, CA MichaelOrlando, FL 21

INTERSECT CORR  Selects unique rows common to both tables for matching column names proc sql; select * from google intersect corr select * from bing; quit; Name Joseph Mark Melissa Michael 22

INTERSECT CORR and ALL proc sql; select * from google intersect all corr select * from bing; run;  Unique and duplicate rows that appear in both tables will be saved  Only columns with the same name in both tables will be displayed 23 Name Joseph Mark Melissa Michael

UNION  Selects unique rows in either table  Column labels are ignored  Rows are sorted proc sql; select * from google union select * from bing; quit; 24

UNION 25 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina

UNION 26 NameResidence JohnColumbia, SC JohnWashington, DC JosephLondon, England LauraCambridge, MA MarkNapierville, IL MelissaLos Angeles, CA MichaelOrlando, FL VGArmenia

UNION and ALL  Selects all rows in either table  Column labels are ignored  Not sorted proc sql; select * from google union all select * from bing; quit; 27

UNION and CORR  Selects all unique rows in either table based on matching column names proc sql; select * from google union corr select * from bing; run; 28

UNION and CORR 29 Name John Joseph Laura Mark Melissa Michael VG

UNION CORR and ALL  Unique and duplicate rows that appear in either table will be saved  Only columns with the same name in both tables will be displayed  Output is unsorted proc sql; select * from google union all corr select * from bing; quit; 30

OUTER UNION  Selects all rows in both tables, but does not overlay any of the information  Output will have r 1 +r 2 rows and c 1 +c 2 columns proc sql; select * from google outer union select * from bing; quit; 31

OUTER UNION and CORR  Using CORR overlays columns with the same name  More useful than OUTER UNION, but still does not merge data proc sql; select * from google outer union corr select * from bing; quit; 32

OUTER UNION with CORR 33 NameResidenceCity JohnColumbia, SC LauraCambridge, MA MelissaLos Angeles, CA MichaelOrlando, FL JosephLondon, England MelissaLos Angeles, CA MarkNapierville, IL JohnWashington, DC MarkNapierville, IL JosephLondon, England MelissaOrlando, FL MichaelLos Angeles, CA VGYerevan, Armenia