Chapter 1 Overview of the SQL Procedure



Similar documents
Performing Queries Using PROC SQL (1)

AN INTRODUCTION TO THE SQL PROCEDURE Chris Yindra, C. Y. Associates

Introduction to Proc SQL Steven First, Systems Seminar Consultants, Madison, WI

Using the SQL Procedure

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Katie Minten Ronk, Steve First, David Beam Systems Seminar Consultants, Inc., Madison, WI

9.1 SAS. SQL Query Window. User s Guide

Handling Missing Values in the SQL Procedure

Introduction to Microsoft Jet SQL

Oracle SQL. Course Summary. Duration. Objectives

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Microsoft Access 3: Understanding and Creating Queries

Structured Query Language (SQL)

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Salary. Cumulative Frequency

Introduction to SQL and SQL in R. LISA Short Courses Xinran Hu

Introduction to Proc SQL Katie Minten Ronk, Systems Seminar Consultants, Madison, WI

SQL SELECT Query: Intermediate

Outline. SAS-seminar Proc SQL, the pass-through facility. What is SQL? What is a database? What is Proc SQL? What is SQL and what is a database

Analysis One Code Desc. Transaction Amount. Fiscal Period

Effective Use of SQL in SAS Programming

Financial Data Access with SQL, Excel & VBA

Lecture 4: SQL Joins. Morgan C. Wang Department of Statistics University of Central Florida

Relational Database: Additional Operations on Relations; SQL

Oracle Database: SQL and PL/SQL Fundamentals

SQL. Short introduction

Access Queries (Office 2003)

Unit 10: Microsoft Access Queries

Advanced Subqueries In PROC SQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

Examine the structure of the EMPLOYEES table: EMPLOYEE_ID NUMBER Primary Key FIRST_NAME VARCHAR2(25) LAST_NAME VARCHAR2(25)

Paper An Introduction to SAS PROC SQL Timothy J Harrington, Venturi Partners Consulting, Waukegan, Illinois

Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query

Oracle Database: SQL and PL/SQL Fundamentals

Appendix A Practices and Solutions

CIS 631 Database Management Systems Sample Final Exam

Join Example. Join Example Cart Prod Comprehensive Consulting Solutions, Inc.All rights reserved.

Programming with SQL

PROC SQL for DATA Step Die-Hards Christianna S. Williams, Yale University

SQL Programming. Student Workbook

A basic create statement for a simple student table would look like the following.

Advanced Query for Query Developers

Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

SAS Programming Tips, Tricks, and Techniques

Netezza SQL Class Outline

COMPARISON OF FIXED & VARIABLE RATES (25 YEARS) CHARTERED BANK ADMINISTERED INTEREST RATES - PRIME BUSINESS*

COMPARISON OF FIXED & VARIABLE RATES (25 YEARS) CHARTERED BANK ADMINISTERED INTEREST RATES - PRIME BUSINESS*

Lab 9 Access PreLab Copy the prelab folder, Lab09 PreLab9_Access_intro

DBF Chapter. Note to UNIX and OS/390 Users. Import/Export Facility CHAPTER 7

Paper Merges and Joins Timothy J Harrington, Trilogy Consulting Corporation

Database Query 1: SQL Basics

FileMaker 13. SQL Reference

A Day in the Life of Data Part 2

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Database Administration with MySQL

3.GETTING STARTED WITH ORACLE8i

Query. Training and Participation Guide Financials 9.2

Information Systems SQL. Nikolaj Popov

SQL Nested & Complex Queries. CS 377: Database Systems

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

Five Little Known, But Highly Valuable, PROC SQL Programming Techniques. a presentation by Kirk Paul Lafler

PeopleSoft Query Training

The prerelease version of SQL was called SEQUEL (for Structured English Query Language), and some people still pronounce SQL as sequel.

Advance DBMS. Structured Query Language (SQL)

A Brief Introduction to MySQL

Essentials of SQL. Essential SQL 11/06/2010. NWeHealth, The University of Manchester

AT&T Global Network Client for Windows Product Support Matrix January 29, 2015

Oracle Database: Introduction to SQL

DBMS / Business Intelligence, SQL Server

Using Multiple Operations. Implementing Table Operations Using Structured Query Language (SQL)

Welcome to the topic on queries in SAP Business One.

Foundations & Fundamentals. A PROC SQL Primer. Matt Taylor, Carolina Analytical Consulting, LLC, Charlotte, NC

P_Id LastName FirstName Address City 1 Kumari Mounitha VPura Bangalore 2 Kumar Pranav Yelhanka Bangalore 3 Gubbi Sharan Hebbal Tumkur

Using SQL Queries in Crystal Reports

Oracle Database: Introduction to SQL

Oracle Database 12c: Introduction to SQL Ed 1.1

RDBMS Using Oracle. Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture. kamran.munir@gmail.com. Joining Tables

Oracle 10g PL/SQL Training

Comp 5311 Database Management Systems. 3. Structured Query Language 1

Using AND in a Query: Step 1: Open Query Design

PROC SQL: Tips and Translations for Data Step Users Susan P Marcella, ExxonMobil Biomedical Sciences, Inc. Gail Jorgensen, Palisades Research, Inc.

Aggregating Data Using Group Functions

SQL Basics. Introduction to Standard Query Language

SES Project v 9.0 SES/CAESAR QUERY TOOL. Running and Editing Queries. PS Query

David M. Kroenke and David J. Auer Database Processing: Fundamentals, Design and Implementation

Week 4 & 5: SQL. SQL as a Query Language

Kaseya 2. Quick Start Guide. for VSA 6.3

SQL Server 2008 Core Skills. Gary Young 2011

Introduction to Using PROC SQL Thomas J. Winn Jr., Texas State Comptroller s Office, Austin, Texas

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

Lecture 25: Database Notes

Lab # 5. Retreiving Data from Multiple Tables. Eng. Alaa O Shama

CHAPTER 1 Overview of SAS/ACCESS Interface to Relational Databases

Access 2010: Creating Queries Table of Contents INTRODUCTION TO QUERIES... 2 QUERY JOINS... 2 INNER JOINS... 3 OUTER JOINS...

Oracle Database: Introduction to SQL

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

Creating QBE Queries in Microsoft SQL Server

Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views

B) Mean Function: This function returns the arithmetic mean (average) and ignores the missing value. E.G: Var=MEAN (var1, var2, var3 varn);

Transcription:

Chapter 1 Overview of the SQL Procedure 1.1 Features of PROC SQL...1-3 1.2 Selecting Columns and Rows...1-6 1.3 Presenting and Summarizing Data...1-17 1.4 Joining Tables...1-27

1-2 Chapter 1 Overview of the SQL Procedure

1.1 Features of PROC SQL 1-3 1.1 Features of PROC SQL Objectives Understand SQL procedure syntax. 3 Features of PROC SQL PROC SQL has the following features: is part of Base SAS software follows guidelines set by the American National Standards Institute (ANSI) can query, combine, create, and update SAS and RDBMS (relational database management system) data supports SAS functions such as SUBSTR and MEAN supports data set options such as DROP= and RENAME= 4 You must have the appropriate SAS/ACCESS software to query your RDBMS data. You must have appropriate authority to read, create, or update RDBMS objects.

1-4 Chapter 1 Overview of the SQL Procedure Features of PROC SQL The PROC SQL statement does not need to be repeated with each query. Each statement is processed individually. No PROC PRINT step is needed to view query results. No PROC SORT step is needed to order query results. No RUN statement is needed. Use a QUIT statement to terminate PROC SQL. 5 Features of the SELECT Statement The features of the SELECT statement include the following: selects data that meets certain conditions groups data specifies an order for the data formats the data queries 1 to 32 tables 6 Table names and variable names can be 1 to 32 characters in length and are not case-sensitive. Librefs and filerefs are limited to 8 characters. Starting in SAS 9, format and informat names can be up to 32 characters in length.

1.1 Features of PROC SQL 1-5 SELECT Statement Syntax General form of the SELECT statement: SELECT column-1<, column-2>... FROM table-1 view-1<, table-2 view-2>... <WHERE expression> <GROUP BY column-1<, column-2> > <HAVING expression> <ORDER BY column-1<, column-2> <DESC>>; QUIT; 7 SELECT FROM WHERE GROUP BY HAVING ORDER BY specifies the columns to be selected. specifies the table to be queried. subsets the data based on a condition. classifies the data into groups. subsets groups of data based on a group condition. sorts rows of data by the values of specific columns in ascending order by default.

1-6 Chapter 1 Overview of the SQL Procedure 1.2 Selecting Columns and Rows Objectives Display columns directly from a table. Display columns calculated from other columns in a query. Eliminate duplicate rows in a query. Subset the data displayed in a query. 9 Retrieving Data from a Table If you are familiar with a table, you can specify column names to be printed in the SELECT statement. Example: Print employee IDs, job codes, and salaries. libname airline 'c:\sql'; select EmpID, JobCode, Salary from airline.payrollmaster; quit; 10

1.2 Selecting Columns and Rows 1-7 Employee IDs, Job Codes, and Salaries Partial Output The SAS System Emp Job ID Code Salary ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ 1919 TA2 $48,126 1653 ME2 $49,151 1400 ME1 $41,677 1350 FA3 $46,040 1401 TA3 $54,351 1499 ME3 $60,235 1101 SCP $26,212 1333 PT2 $124,048 1402 TA2 $45,661 11 Retrieving Data from a Table select * from airline.payrollmaster; quit; Partial Output The SAS System If you are not familiar with a table, an asterisk in the SELECT statement prints all columns in their originally stored order. Emp Job ID Gender Code Salary DateOfBirth DateOfHire ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ 1919 M TA2 $48,126 16SEP1958 07JUN1985 1653 F ME2 $49,151 19OCT1962 12AUG1988 1400 M ME1 $41,677 08NOV1965 19OCT1988 1350 F FA3 $46,040 04SEP1963 01AUG1988 1401 M TA3 $54,351 16DEC1948 21NOV1983 1499 M ME3 $60,235 29APR1952 11JUN1978 12

1-8 Chapter 1 Overview of the SQL Procedure Retrieving Data from a Table select * from airline.payrollmaster(drop=gender); quit; Partial Output The SAS System Emp Job ID Code Salary DateOfBirth DateOfHire ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ 1919 TA2 $48,126 16SEP1958 07JUN1985 1653 ME2 $49,151 19OCT1962 12AUG1988 1400 ME1 $41,677 08NOV1965 19OCT1988 1350 FA3 $46,040 04SEP1963 01AUG1988 1401 TA3 $54,351 16DEC1948 21NOV1983 1499 ME3 $60,235 29APR1952 11JUN1978 13 Expressions Calculate new columns from existing columns, and name the new columns using the AS keyword. Example: Calculate employee bonuses and age. select EmpID, JobCode, Salary, Salary *.10 as Bonus, int((today()-dateofbirth)/365.25) as Age from airline.payrollmaster; quit; 14

1.2 Selecting Columns and Rows 1-9 Employee Bonuses and Age Partial Output The SAS System Emp Job ID Code Salary Bonus Age ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ 1919 TA2 $48,126 4812.64 48 1653 ME2 $49,151 4915.12 44 1400 ME1 $41,677 4167.66 41 1350 FA3 $46,040 4604.04 43 1401 TA3 $54,351 5435.08 58 1499 ME3 $60,235 6023.5 55 1101 SCP $26,212 2621.22 47 1333 PT2 $124,048 12404.84 48 1402 TA2 $45,661 4566.1 46 1479 TA3 $54,299 5429.9 40 15 Eliminating Duplicate Rows Use the DISTINCT keyword to eliminate duplicate rows in query results. Example: Determine the international flights that were flown during the month. select distinct FlightNumber, Destination from airline.internationalflights; quit; 16

1-10 Chapter 1 Overview of the SQL Procedure Eliminating Duplicate Rows Output The SAS System FlightNumber Destination ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ 132 YYZ 182 YYZ 219 LHR 271 CDG 387 CPH 622 FRA 821 LHR 17 Subsetting with the WHERE Clause Use a WHERE clause to specify a condition that the data must satisfy before being selected. Example: Display all employees that earn more than $112,000. select EmpID, JobCode, Salary from airline.payrollmaster where Salary > 112000; quit; 18

1.2 Selecting Columns and Rows 1-11 Subsetting with the WHERE Clause Output The SAS System Emp Job ID Code Salary ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ 1333 PT2 $124,048 1404 PT2 $127,926 1118 PT3 $155,931 1410 PT2 $118,559 1777 PT3 $153,482 1106 PT2 $125,485 1442 PT2 $118,350 1478 PT2 $117,884 1890 PT2 $120,254 1107 PT2 $125,968 1830 PT2 $118,259 1928 PT2 $125,801 19 Subsetting with the WHERE Clause You can use all common comparison operators in a WHERE clause. Mnemonic Symbol Definition LT GT EQ LE GE NE < > = <= >= = ^= Less than Greater than Equal to Less than or equal to Greater than or equal to Not equal to (EBCDIC) Not equal to (ASCII) 20

1-12 Chapter 1 Overview of the SQL Procedure Subsetting with the WHERE Clause You can use the IN operator to compare a value to a list of values. If the value matches at least one in the list, the expression is true; otherwise, the expression is false. where JobCategory in ('PT','NA','FA') where DayOfWeek in (2,4,6) 21 Subsetting with the WHERE Clause You can specify multiple expressions in a WHERE clause by using logical operators. Mnemonic OR AND NOT NOT Symbol & ^ Definition or, either and, both not, negation EBCDIC not, negation ASCII 22

1.2 Selecting Columns and Rows 1-13 Subsetting with the WHERE Clause Use either CONTAINS or? to select rows that include the substring specified. where word? 'LAM' (BLAME, LAMENT, and BEDLAM are selected.) Use either IS NULL or IS MISSING to select rows with missing values. where FlightNumber is missing 23 Subsetting with the WHERE Clause Use BETWEEN-AND to select rows containing ranges of values, inclusively. where Date between '01mar2000'd and '07mar2000'd where Salary between 70000 and 80000 24

1-14 Chapter 1 Overview of the SQL Procedure Subsetting with the WHERE Clause Use LIKE to select rows by comparing character values to specified patterns. A % sign replaces any number of characters. where LastName like 'H%' (H plus any characters; for example, HENRY and HAMM.) A single underscore ('_') replaces individual characters. where JobCode like ' 1' (Captures any two characters and 1, for example, 'FA1'.) 25 Subsetting with Calculated Values Example: Display only the flights where the total number of passengers was fewer than 100 people. select FlightNumber,Date,Destination, Boarded + Transferred + Nonrevenue as Total from airline.marchflights where Total < 100; Partial Log ERROR: The following columns were not found in the contributing tables: Total. 26

1.2 Selecting Columns and Rows 1-15 Subsetting with Calculated Values One solution is to repeat the calculation in the WHERE clause. select FlightNumber, Date, Destination, Boarded+Transferred+Nonrevenue as Total from airline.marchflights where Boarded+Transferred+Nonrevenue < 100; 27 Subsetting with Calculated Values A more efficient method is to use the CALCULATED keyword to refer to already calculated columns in the SELECT clause. select FlightNumber, Date, Destination, Boarded + Transferred + Nonrevenue as Total from airline.marchflights where calculated Total < 100; 28

1-16 Chapter 1 Overview of the SQL Procedure Subsetting with Calculated Values Partial Output The SAS System FlightNumber Date Destination Total ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ 982 01MAR2000 DFW 70 416 01MAR2000 WAS 93 829 01MAR2000 WAS 96 416 02MAR2000 WAS 90 302 02MAR2000 WAS 93 29

1.3 Presenting and Summarizing Data 1-17 1.3 Presenting and Summarizing Data Objectives Order the data displayed in a query. Use SAS formats, labels, and titles to enhance query output. Use functions to summarize data in a query. Group data for aggregate functions. 31 Ordering Data The ORDER BY clause has the following functionality: sorts on any column or expression (display or nondisplay) in ascending order by default can sort data in descending order by following the column name with the DESC keyword uses a column name or a number that represents the position of an item in the SELECT list can sort on multiple columns 32

1-18 Chapter 1 Overview of the SQL Procedure Ordering Data select EmpID, JobCode, Salary from airline.payrollmaster where JobCode contains 'NA' order by Salary desc; 33 Ordering Data Output The SAS System Emp Job ID Code Salary ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ 1352 NA2 $75,317 1417 NA2 $73,178 1935 NA2 $71,513 1839 NA1 $60,806 1443 NA1 $59,184 1332 NA1 $59,049 1269 NA1 $58,366 1111 NA1 $56,820 34

1.3 Presenting and Summarizing Data 1-19 Enhancing Query Output You can use SAS titles and footnotes enhance your output. You can also use SAS formats and labels to customize PROC SQL output. After the column name in the SELECT list, you specify the following: LABEL= option to alter the column heading FORMAT= option to alter the appearance of the values in that column. 35 Enhancing Query Output Example: Enhance the report. Display the navigators and their salaries. title 'Navigator Salaries'; select EmpID label='employee Identifier', JobCode label='job Code', Salary label='annual Salary' format=dollar12.2 from airline.payrollmaster where JobCode contains 'NA' order by Salary desc; 36

1-20 Chapter 1 Overview of the SQL Procedure Enhanced Query Output Output Navigator Salaries Employee Job Annual Identifier Code Salary ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ 1352 NA2 $75,317.20 1417 NA2 $73,178.00 1935 NA2 $71,513.40 1839 NA1 $60,806.20 1443 NA1 $59,183.60 1332 NA1 $59,049.20 1269 NA1 $58,366.00 1111 NA1 $56,820.40 37 Summary Functions Example: Find the total number of passengers for each flight in March. select Date, FlightNumber, Boarded, Transferred, Nonrevenue, sum(boarded,transferred,nonrevenue) as Total from airline.marchflights; quit; This calculation is performed across columns for each row. 38

1.3 Presenting and Summarizing Data 1-21 Summary Functions Partial Output The SAS System Date FlightNumber Boarded Transferred Nonrevenue Total ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ 30MAR2000 183 86 5 4 95 30MAR2000 271 164 15 6 185 30MAR2000 921 101 20 4 125 30MAR2000 302 54 14 3 71 30MAR2000 431 159 14 4 177 30MAR2000 308 121 18 6 145 31MAR2000 182 91 7 6 104 31MAR2000 114 183 14 4 201 31MAR2000 202 111 12 3 126 39 Summary Functions If you specify only one column name in a summary function, the statistic is calculated down the column. Example: Determine the average salary for the company. select avg(salary) as MeanSalary from airline.payrollmaster; 40 Output The SAS System MeanSalary ƒƒƒƒƒƒƒƒƒƒ 54079.65

1-22 Chapter 1 Overview of the SQL Procedure Summary Functions The following are selected functions: AVG, MEAN mean or average value COUNT, FREQ, N number of nonmissing values MAX largest value MIN smallest value NMISS number of missing values STD standard deviation SUM sum of values VAR variance 41 Summary Functions Example: Add the JobCode column to the summarized query. select JobCode, avg(salary) as Average from airline.payrollmaster; 42

1.3 Presenting and Summarizing Data 1-23 Summary Functions Partial Output The SAS System Job Code Average ƒƒƒƒƒƒƒƒƒƒƒƒƒƒ TA2 54079.65 ME2 54079.65 ME1 54079.65 FA3 54079.65 TA3 54079.65 ME3 54079.65 SCP 54079.65 PT2 54079.65 TA2 54079.65 TA3 54079.65 ME1 54079.65 43 Grouping Data You can use the GROUP BY clause to classify the data into groups based on the values of one or more columns calculate statistics for each unique value of the grouping columns. 44

1-24 Chapter 1 Overview of the SQL Procedure Grouping Data Example: Display the average salary and number of employees for each job code. select JobCode, avg(salary) as Average format=dollar11.2, count(*) as Employees from airline.payrollmaster group by JobCode; 45 Grouping Data Partial Output The SAS System Job Code Average Employees ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ BCK $36,111.91 9 FA1 $32,255.11 11 FA2 $39,181.63 16 FA3 $46,107.40 7 ME1 $39,900.35 8 ME2 $49,807.60 14 ME3 $59,375.00 7 NA1 $58,845.08 5 NA2 $73,336.20 3 PT1 $95,071.20 8 46

1.3 Presenting and Summarizing Data 1-25 Selecting Groups of Data with the HAVING Clause The WHERE clause selects data based on values for individual rows. To select entire groups of data, use the HAVING clause. Example: Display all job codes with an average salary of more than the company average salary of $54,079. select JobCode, avg(salary) as Average format=dollar11.2 from airline.payrollmaster group by JobCode having avg(salary) > 54079; 47 To avoid using hard-coded constants, use a subquery to return the company average salary. A subquery, or inner query, is a query-expression that is nested as part of another query-expression. The subquery executes first, returning its results to the outer query. select JobCode, avg(salary) as Average format=dollar11.2 from airline.payrollmaster group by JobCode having avg(salary) > (select avg(salary) from airline.payrollmaster);

1-26 Chapter 1 Overview of the SQL Procedure Selecting Groups of Data with the HAVING Clause Output The SAS System Job Code Average ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ ME3 $59,375.00 NA1 $58,845.08 NA2 $73,336.20 PT1 $95,071.20 PT2 $122,253.60 PT3 $154,706.30 49

1.4 Joining Tables 1-27 1.4 Joining Tables Objectives Join two tables. Use a table alias. Create a table from the join query results. 51 Combining Data from Multiple Tables Joins combine tables horizontally (side by side). Table A Table B PROC SQL supports both inner and outer joins. We will look at inner joins. 51

1-28 Chapter 1 Overview of the SQL Procedure Inner Joins Inner joins have the following characteristics: return only matching rows allow a maximum of 32 tables to be joined at the same time 53 Inner Joins An inner join returns only the subset of rows from the first table that matches rows from the second table. You can specify the columns that you want to be compared for matching values in a WHERE clause. General form of an inner join: SELECT column-1, column-2, FROM table-1, table-2, WHERE join-condition(s) <AND other subsetting conditions> <other clauses>; 53 Without a WHERE clause, the join will return a Cartesian product of the tables where each row from the first table is combined with every row from the second table.

1.4 Joining Tables 1-29 Inner Joins Example: Create a table that contains the names, job codes, and ages of all New York employees. Employee names are found in the airline.staffmaster table. Employee job codes and birth dates are found in the airline.payrollmaster table. 55 Inner Joins title 'New York Employees'; select FirstName, LastName, JobCode, int((today()-dateofbirth)/365.25) as Age from airline.payrollmaster as p, airline.staffmaster as s where p.empid=s.empid and State='NY' order by JobCode; quit; 56 An alias is a table nickname. You can assign an alias to a table by following the table name in the FROM clause with the AS keyword and a nickname for the table. Then use the alias in other clauses of the QUERY statement.

1-30 Chapter 1 Overview of the SQL Procedure Inner Joins Partial Output New York Employees Job FirstName LastName Code Age ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ RUSSELL LONG BCK 37 LEVI GORDON BCK 50 JAMES PEARSON BCK 49 NATHAN JONES BCK 42 THOMAS BURNETTE BCK 41 RICHARD VANDEUSEN BCK 48 JOHN MARKS BCK 42 DEBORAH WOOD FA1 38 LESLIE JONES FA1 41 ANNE PARKER FA1 44 DIANA FIELDS FA1 51 CASEY RICHARDS FA1 39 57 Creating a Table From Query Results The CREATE TABLE table-name AS statement stores the results of the query into a table. create table airline.newyork as select FirstName, LastName, JobCode, int((today()-dateofbirth)/365.25) as Age from airline.payrollmaster as p, airline.staffmaster as s where p.empid=s.empid and State='NY' order by JobCode; quit; 58 The CREATE TABLE statement does not generate output.

1.4 Joining Tables 1-31 Creating a Table From Query Results proc print data=airline.newyork noobs; title 'New York Employees'; run; Partial Output New York Employees Job FirstName LastName Code Age 59 RUSSELL LONG BCK 37 LEVI GORDON BCK 50 JAMES PEARSON BCK 49 NATHAN JONES BCK 42 THOMAS BURNETTE BCK 41 RICHARD VANDEUSEN BCK 48 JOHN MARKS BCK 42 DEBORAH WOOD FA1 38 LESLIE JONES FA1 41 Where to Go Next SQL Processing with the SAS System 2 days PROC SQL: Beyond the Basics Using SAS by Kirk Paul Laffler The Essential PROC SQL Handbook for SAS Users by Katherine Prairie SQL Self-Paced elearning: http://www.sas.com/apps/elearning/ 58