Noncorrelated subquery

Slides:



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

DATABASE PROGRAMMING Sections 5-7. Write a query that shows the average, maximum, and minimum salaries for all employees with jobs in the programming.
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
Introduction to SQL Session 2 Retrieving Data From Multiple Tables.
11 Chapter 3: Displaying Query Results 3.1 Presenting Data 3.2 Summarizing Data.
Introduction to SQL Session 1 Retrieving Data From a Single Table.
PROC SQL – Select Codes To Master For Power Programming Codes and Examples from SAS.com Nethra Sambamoorthi, PhD Northwestern University Master of Science.
Seminar #1 – Refreshing databases concepts and SQL CM036: Advanced Databases1 Seminar 1: Revisiting Databases and SQL Purpose To refresh basic concepts.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 8: Subqueries.
Chapter 8 Producing Summary Reports. Section 8.1 Introduction to Summary Reports.
Chapter 9 Producing Descriptive Statistics PROC MEANS; Summarize descriptive statistics for continuous numeric variables. PROC FREQ; Summarize frequency.
11 Chapter 2: Basic Queries 2.1: Overview of the SQL Procedure 2.2: Specifying Columns 2.3: Specifying Rows.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
1 Agenda – 03/25/2014 Login to SQL Server 2012 Management Studio. Answer questions about HW#7 – display answers. Exam is 4/1/2014. It will be in the lab.
Nested Queries (Sub-Queries). Objectives Learn how to run a query as a nested sub-query Condition on nested query Application of nested query Restriction.
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.
Structured Query Language Introduction. Basic Select SELECT lname, fname, phone FROM employees; Employees Table LNAMEFNAMEPHONE JonesMark SmithSara
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
Controlling Input and Output
Agenda for Class - 03/04/2014 Answer questions about HW#5 and HW#6 Review query syntax. Discuss group functions and summary output with the GROUP BY statement.
11 Chapter 4: Subqueries 4.1: Noncorrelated Subqueries 4.2: Correlated Subqueries (Self-Study)
1 Checking Data with the PRINT and FREQ Procedures.
Using Subqueries to Solve Queries
Session 1 Retrieving Data From a Single Table
Chapter 6: Set Operators
Writing Basic SQL SELECT Statements
Displaying Query Results
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using the Set Operators
Riding the Wave of Innovation
Basic Queries Specifying Columns
PROC SQL, Overview.
Using Subqueries to Solve Queries
An Introduction to SQL.
Using the Set Operators
Correlated Subqueries
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.
Grouping Summary Results
Inner Joins.
Create a subset of DPC data
Program Testing and Performance
Using Subqueries to Solve Queries
SQL Subquery.
Combining Data Sets in the DATA step.
Summarizing Data with Summary Functions
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.
Subqueries Schedule: Timing Topic 25 minutes Lecture
3 Views.
Introduction to Subqueries, An Example
A new keyword -- calculated
Using Subqueries to Solve Queries
Subqueries.
Reporting Aggregated Data Using the Group Functions
Using Subqueries to Solve Queries
UNION Operator keywords Displays all rows from both the tables
Remerging Summary Values
Using the Set Operators
Reporting Aggregated Data Using the Group Functions
2 Types of Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Aggregating Data Using Group Functions
Presentation transcript:

Noncorrelated subquery

Example: Create a report that displays Job_Title for job groups with an average salary greater than the average salary of the company as a whole. proc contents data=orion.staff position;run; Orion staff file contains necessary information

How many unique job titles? proc sql; select count(distinct job_title) from orion.staff ; proc freq data=orion.staff nlevels; table job_title/noprint; run; We can find number of job titles in two ways

The standalone query select avg(Salary) as MeanSalary from orion.Staff

Use the standalone query as a subquery proc sql; select Job_Title, avg(Salary) as MeanSalary from orion.Staff group by Job_Title having avg(Salary) > ( select avg(Salary) as MeanSalary from orion.Staff ); quit; Note MeanSalary appears in two different contexts

Noncorrelated Subqueries proc sql; select Job_Title, avg(Salary) as MeanSalary from orion.Staff group by Job_Title having avg(Salary) > ( select avg(Salary) as MeanSalary from orion.Staff ); quit; Evaluate the subquery first. The noncorrelated subquery is evaluate first. In this case a single number is created

Noncorrelated Subqueries proc sql; select Job_Title, avg(Salary) as MeanSalary from orion.Staff group by Job_Title having avg(Salary) > (38041.51); quit; Then pass the results to the outer query. The number is then passed to the outer query

Example: Create a report listing the names and addresses of employees with February birthdays. In this case, the necessary info isn’t on a single file

names and addresses. Birth dates The two files Note primary key

A stand alone query to get employee_id of all employees born in February select Employee_ID from orion.Employee_Payroll where month(Birth_Date)=2

Embed stand alone query proc sql; select Employee_ID, Employee_Name, City, Country from orion.Employee_Addresses where Employee_ID in (select Employee_ID from orion.Employee_Payroll where month(Birth_Date)=2) order by 1 ; quit;

Example: Create a file with only studies that have both male and female participants

A stand alone query, find study number for those studies having female participants proc sql; select distinct study from dpc.ipd_student where male=0 ; quit;

Embed the stand alone query as a subquery proc freq data=dpc.ipd_student; tables study*male/norow nocol nopercent; proc sql; create table malefem as select * from dpc.ipd_student where study in (select distinct study where male=0) ; quit; proc freq data=malefem; run;

Example Missing values on the test data set, a problem in predictive analytics

The data (Partial) proc contents data=kag.train position;run;

Count observations on train and test sets libname kag "&path\clickthrough"; proc sql; title "Size of training data set"; select count(*) format=comma10. from kag.train ; title "Size of test data set"; from kag.test quit; title;

The dependent variable proc sql; select click,count(*) format= comma10. from kag.train group by click; quit;

An aside, the same thing in PROC FREQ proc freq data=kag.train; tables click; run;

Examine the number of app_ids on train and test data sets. proc sql; select count(distinct app_id) as num_on_train from kag.train ; select count(distinct app_id) as num_on_test from kag.test quit;

proc sql; select count(distinct app_id) as not_on_train from kag.test where app_id not in (select unique app_id from kag.train) ; quit;

proc sql; select count(*) as obs_not_on_train from kag.test where app_id not in (select unique app_id from kag.train) ; quit;

The next few examples uses an airline data base The next few examples uses an airline data base. The airline data base came from SAS and is used in the Advanced Programming Certification Prep Book. Many of the queries used in the following come from that book.

Example: Find jobcodes that have average salary greater then the overall average salary

The payrollmaster table proc contents data=train.payrollmaster;run;

How many jobcodes? proc freq data=train.payrollmaster nlevels; tables jobcode/noprint; run;

Example: Find jobcodes that have average salary greater then the overall average salary /*single-value non correlated subquery*/ proc sql; select jobcode, avg(salary) as AvgSalary format=dollar11.2 from train.payrollmaster group by jobcode having avg(salary) > (select avg(salary) from train.payrollmaster) ; quit;

Example: List the employee id, last name, first name, city and state for all employees born in December

/*multiple value noncorrelated subquery*/ proc sql; select empid,lastname,firstname, city,state from train.staffmaster where empid in (select empid from train.payrollmaster where month(dateofbirth)=12) ; quit;

The ANY keyword with subqueries that return multiple values

List employee id, job code, and date of birth for level 1 or 2 flight attendants who are older than any level 3 flight attendants

A standalone query that selects dates of birth for all Flight Attendant 3s select dateofbirth from train.payrollmaster where jobcode="FA3"

Embed the stand alone query as a subquery /* any keyword*/ proc sql; select empid,jobcode,dateofbirth from train.payrollmaster where jobcode in ("FA1","FA2") and dateofbirth < any (select dateofbirth where jobcode="FA3") ; quit;

The ALL keyword

List employee id, jobcode, and date of birth for level 1 or level 2 flight attendants who are older than all level 3 flight attendants

proc sql; select empid,jobcode,dateofbirth from train.payrollmaster where jobcode in ("FA1","FA2") and dateofbirth < all (select dateofbirth where jobcode="FA3") ; quit;