Performing Queries Using PROC SQL Chapter 1 1 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Slides:



Advertisements
Similar presentations
Sorting Rows. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Construct a query to sort a results set in ascending.
Advertisements

Chapter 4 Joining 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.
Basic SQL Introduction Presented by: Madhuri Bhogadi.
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.
Introduction to Structured Query Language (SQL)
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Introduction to Structured Query Language (SQL)
Structured Query Language Part I Chapter Three CIS 218.
Introduction to SQL Session 1 Retrieving Data From a Single Table.
Introduction to Structured Query Language (SQL)
Microsoft Access 2010 Chapter 7 Using SQL.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Computer Science 101 Web Access to Databases SQL – Extended Form.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
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.
You can use a query to view a subset of your data or to answer questions about your data. For example, if you want to view a list of student names and.
Introduction to Databases Chapter 7: Data Access and Manipulation.
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:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
©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.
SQL Chapter Two. Overview Basic Structure Verifying Statements Specifying Columns Specifying Rows.
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.
Information Building and Retrieval Using MySQL Track 3 : Basic Course in Database.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
Chapter 22: Using Best Practices 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
1 DBS201: More on SQL Lecture 3. 2 Agenda How to use SQL to update table definitions How to update data in a table How to join tables together.
Performing Advanced Queries Using PROC SQL Chapter 2 1.
Chapter 13 Views Oracle 10g: SQL. Oracle 10g: SQL2 Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE VIEW command Employ the.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Aliya Farheen October 29,2015.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
Chapter 19: Introduction to Efficient SAS Programming 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Session 9 Accessing Data from a Database. RDBMS and Data Management/ Session 9/2 of 34 Session Objectives Describe the SELECT statement, its syntax and.
Chapter 4: Combining Tables Vertically using PROC SQL 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
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 21: Controlling Data Storage Space 1 STAT 541 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
IFS180 Intro. to Data Management Chapter 10 - Unions.
SAS and Other Packages SAS can interact with other packages in a variety of different ways. We will briefly discuss SPSSX (PASW) SUDAAN IML SQL will be.
ORDER BY Clause The result of a query can be sorted in ascending or descending order using the optional ORDER BY clause. The simplest form of.
Session 1 Retrieving Data From a Single Table
Chapter 5 Introduction to SQL.
Connect to SQL Server and run select statements
Basic select statement
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
PROC SQL, Overview.
Chapter 24 (4th ed.): Creating Functions
Displaying Queries 2 Display a query’s results in a specified order.
Chapter 4 Summary Query.
Access: SQL Participation Project
Database Design and Development
Writing Basic SQL SELECT Statements
Introduction To Structured Query Language (SQL)
Chapter 24: Querying Data Efficiently
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

Performing Queries Using PROC SQL Chapter 1 1 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina

PROC SQL Basics PROC SQL is the SAS implementation of Structured Query Language (SQL) PROC SQL is the SAS implementation of Structured Query Language (SQL) SQL is a standardized language and is widely used to retrieve and update data in tables SQL is a standardized language and is widely used to retrieve and update data in tables PROC SQL can often be used as an alternative to other SAS procedures or the data step PROC SQL can often be used as an alternative to other SAS procedures or the data step 2

PROC SQL Basics The PROC SQL statement does not need to be repeated with each query The PROC SQL statement does not need to be repeated with each query PROC SQL does not require a run statement PROC SQL does not require a run statement To end PROC SQL, submit another procedure, a data step, or a quit statement. To end PROC SQL, submit another procedure, a data step, or a quit statement. 3

Uses for SQL Retrieve data from tables Retrieve data from tables Add or modify data in a table Add or modify data in a table Add, modify, or drop columns in a table Add, modify, or drop columns in a table Create tables and views Create tables and views Join tables Join tables Create reports Create reports 4

Basic PROC SQL Step proc sql; select column1, column2,…, columnn select column1, column2,…, columnn from table1 (or view1) from table1 (or view1) ; ; 5

SELECT Statement The SELECT statement follows the PROC SQL statement The SELECT statement follows the PROC SQL statement The SELECT statement retrieves and displays data The SELECT statement retrieves and displays data The SELECT statement is composed of clauses (e.g., SELECT, FROM, WHERE) The SELECT statement is composed of clauses (e.g., SELECT, FROM, WHERE) The SELECT and FROM clauses are required. The remaining clauses are optional. The SELECT and FROM clauses are required. The remaining clauses are optional. 6

The Order of Clauses Within the PROC SQL SELECT Statement Matters SELECT (So) SELECT (So) FROM (Few) FROM (Few) WHERE (Workers) WHERE (Workers) GROUP BY (Go) GROUP BY (Go) HAVING (Home) HAVING (Home) ORDER BY (On-time) ORDER BY (On-time) 7

SELECT Clause The first clause in the SELECT statement The first clause in the SELECT statement Specifies the column(s) to be selected from the table Specifies the column(s) to be selected from the table Each column listed must be separated by a comma Each column listed must be separated by a comma A label can be specified for a column in the SELECT clause A label can be specified for a column in the SELECT clause 8

SELECT Clause New columns (i.e., columns that are not in the table) can be created with the SELECT clause New columns (i.e., columns that are not in the table) can be created with the SELECT clause The new columns may contain text or a calculation The new columns may contain text or a calculation New columns will appear in the output but will not be kept unless a table or view is created New columns will appear in the output but will not be kept unless a table or view is created 9

FROM Clause The FROM clause specifies which table(s) or view(s) to be queried The FROM clause specifies which table(s) or view(s) to be queried 10

2011 SC Baseball Stats (bbstats) PlayerAt BatsHitsBB Christian Walker Scott Wingo Brady Thomas Evan Marzilli Robert Beary Adrian Morales Peter Mooney Jake Williams Jackie Bradley Jr

Example – Select and From Clauses proc sql; select player, atbats select player, atbats from bbstats; from bbstats;quit; playeratbats Christian Walker271 Scott Wingo240 Brady Thomas231 Evan Marzilli220 Robert Beary211 Adrian Morales249 Peter Mooney254 Jake Williams209 Jackie Bradley Jr

Example – Select and From Clauses proc sql; select player, hits/atbats as avg select player, hits/atbats as avg from bbstats; from bbstats;quit; playeravg Christian Walker.358 Scott Wingo.338 Brady Thomas.316 Evan Marzilli.291 Robert Beary.289 Adrian Morales.281 Peter Mooney.280 Jake Williams.268 Jackie Bradley Jr

WHERE Clause The WHERE clause subsets the data from the table(s) based on one or more conditions The WHERE clause subsets the data from the table(s) based on one or more conditions The WHERE clause can be any valid SAS expression The WHERE clause can be any valid SAS expression The columns specified in the WHERE clause do not have to appear in the SELECT clause The columns specified in the WHERE clause do not have to appear in the SELECT clause 14

Example – Where Clause proc sql; select player, atbats select player, atbats from bbstats from bbstats where atbats > 230; where atbats > 230;quit; playeratbats Christian Walker271 Scott Wingo240 Brady Thomas231 Adrian Morales249 Peter Mooney254 15

HAVING Clause Works with the GROUP BY clause to restrict groups that are displayed in the output, based on one or more conditions Works with the GROUP BY clause to restrict groups that are displayed in the output, based on one or more conditions Discussed in more detail in Chapter 2 Discussed in more detail in Chapter 2 16

ORDER BY Clause ORDER BY sorts rows by the values of a specified column or specified columns. Each column must be separated by a comma ORDER BY sorts rows by the values of a specified column or specified columns. Each column must be separated by a comma Descending sort – specify the keyword DESC following the column name Descending sort – specify the keyword DESC following the column name In the ORDER BY clause, you can reference the column by its name or the position in the SELECT clause. Use an integer to indicate the column’s position. In the ORDER BY clause, you can reference the column by its name or the position in the SELECT clause. Use an integer to indicate the column’s position. 17

Example – Order By Clause proc sql; select player, atbats select player, atbats from bbstats from bbstats order by atbats desc; order by atbats desc;quit; playeratbats Christian Walker271 Peter Mooney254 Adrian Morales249 Scott Wingo240 Brady Thomas231 Evan Marzilli220 Robert Beary211 Jake Williams209 Jackie Bradley Jr

Example – Order By Clause proc sql; select player, atbats select player, atbats from bbstats from bbstats order by 2 desc; order by 2 desc;quit; This example uses the column’s position in the SELECT clause in the ORDER by clause playeratbats Christian Walker271 Peter Mooney254 Adrian Morales249 Scott Wingo240 Brady Thomas231 Evan Marzilli220 Robert Beary211 Jake Williams209 Jackie Bradley Jr

CREATE TABLE Statement Use the CREATE TABLE statement to create a new table from the results of a query Use the CREATE TABLE statement to create a new table from the results of a query 20

PROC SQL Step for Creating a Table from a Query Result proc sql; create table table-name as create table table-name as select column1, column2,…, columnn select column1, column2,…, columnn from table1 (or view1) from table1 (or view1) ; ; 21