Correlated Subqueries Chapter 2 Supplement 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Slides:



Advertisements
Similar presentations
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
Advertisements

Chapter 4 Joining Multiple Tables
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.
Chapter 5: Creating and Managing Tables using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
MULTIPLE-TABLE QUERIES
Chapter 9: Introducing Macro Variables 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Managing Processing Using PROC SQL Chapter 8 1 Imelda Go, John Grego, Jennifer Lasecki, 2011.
Performing Queries Using PROC SQL Chapter 1 1 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Introduction to SQL Session 2 Retrieving Data From Multiple Tables.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
CSEN 5314 Quiz What type of join is needed when you wish to include rows that do not have matching values? A. Equi-joinB. Natural join C. Outer.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Database Systems More SQL Database Design -- More SQL1.
PROC SQL – Select Codes To Master For Power Programming Codes and Examples from SAS.com Nethra Sambamoorthi, PhD Northwestern University Master of Science.
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.
Chapter 3: Combining Tables Horizontally using PROC SQL 1 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
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.
©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina Chapter 17 supplement: Review of Formatting Data STAT 541.
Creating and Managing Indexes Using Proc SQL Chapter 6 1.
Chapter 15: Combining Data Horizontally 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
JOI/1 Data Manipulation - Joins Objectives –To learn how to join several tables together to produce output Contents –Extending a Select to retrieve data.
Week 10 Quiz 9 Answers Group 28 Christine Hallstrom Deena Phadnis.
Chapter 4 Multiple-Table Queries
Chapter 4Introduction to Oracle9i: SQL1 Chapter 4 Joining Multiple Tables.
Unit 4 Queries and Joins. Key Concepts Using the SELECT statement Statement clauses Subqueries Multiple table statements Using table pseudonyms Inner.
1 Multiple Table Queries. 2 Objectives  Retrieve data from more than one table by joining tables  Using IN and EXISTS to query multiple tables  Nested.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Programming in R SQL in R. Running SQL in R In this session I will show you how to: Run basic SQL commands within R.
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.
Chapter 12 Subqueries and Merge Statements
Performing Advanced Queries Using PROC SQL Chapter 2 1.
Denormalizing Data with PROC SQL. Demoralizing Data with PROC SQL Is that a real word?? Spell Check…
Chapter 19: Introduction to Efficient SAS Programming 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
Subqueries.
Chapter 4: Combining Tables Vertically using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Chapter 17 Supplement: Alternatives to IF-THEN/ELSE Processing STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South.
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.
11 Chapter 4: Subqueries 4.1: Noncorrelated Subqueries 4.2: Correlated Subqueries (Self-Study)
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
IFS180 Intro. to Data Management Chapter 10 - Unions.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
CpSc 3220 The Language of SQL
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
References: Text Chapters 8 and 9
Database Systems: Design, Implementation, and Management Tenth Edition
Chapter 19: Introduction to Efficient SAS Programming
SQL – Subqueries.
Chapter 18: Modifying SAS Data Sets and Tracking Changes
Former Chapter 23: Selecting Efficient Sorting Strategies
Correlated Subqueries
Chapter 24 (4th ed.): Creating Functions
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Using Macros to Solve the Collation Problem
SQL Subquery.
Combining Data Sets in the DATA step.
Chapter 24: Querying Data Efficiently
Subqueries.
Database Systems: Design, Implementation, and Management Tenth Edition
SQL set operators and modifiers.
2 Types of Subqueries.
Updating Databases With Open SQL
Updating Databases With Open SQL
Presentation transcript:

Correlated Subqueries Chapter 2 Supplement 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina

Example – Correlated Subquery PlayerAtbats Walker271 Wingo240 Thomas231 Marzilli220 Beary211 Morales249 Mooney254 Williams209 Bradley Jr PlayerPosition MarzilliOutfield WilliamsOutfield Bradley Jr.Outfield WingoInfield WalkerInfield ThomasInfield MarzilliInfield MoralesInfield MooneyInfield AtBatsPlayerposition

Correlated Subqueries  In Chapter 2, we saw one example of a correlated subquery: proc sql; select player, atbats from atbats where "Infield"= (select position from playerposition where atbats.player=playerposition.playe r); quit; 3

Example Correlated Subquery Example Correlated Subquery Step 1 – The outer query takes the first row in atbats table and finds the columns player and atbats. Step 1 – The outer query takes the first row in atbats table and finds the columns player and atbats. Step 2 – Match atbats.player (passed from table in outer query) with playerposition.player to find the qualifying row in the playerposition table. Step 2 – Match atbats.player (passed from table in outer query) with playerposition.player to find the qualifying row in the playerposition table. Step 3 – The inner query now passes the position of the selected row in playerposition back to the outer query via the = operator, where the position is matched for the selection in the outer query. Step 3 – The inner query now passes the position of the selected row in playerposition back to the outer query via the = operator, where the position is matched for the selection in the outer query. 4

Example – Correlated Subquery PlayerAtbats Walker271 Wingo240 Thomas231 Beary211 Morales249 Mooney254 5

Example-Correlated Subquery  Correlated subqueries suggest that SAS has the ability to resolve ambiguous references.  Regardless, when creating code, we often get error messages that look like: ERROR: Unresolved reference to table/correlation name varname 6

Example-Correlated Subquery  We can modify the correlated subquery so that  The reference to playerposition.player is less clear  playerposition.player is not even selected in the outer query, but does appear in the referenced data set (atbats) 7