SQL Chapter Two. Overview Basic Structure Verifying Statements Specifying Columns Specifying Rows.

Slides:



Advertisements
Similar presentations
Haas MFE SAS Workshop Lecture 3:
Advertisements

Statistical Methods Lynne Stokes Department of Statistical Science Lecture 7: Introduction to SAS Programming Language.
Performing Queries Using PROC SQL Chapter 1 1 ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
1 Creating a Non-Conditional List A- What are you going to do? You will “list” “all of the records” in a database. (it means you will not use any condition!)
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)
Databases Tutorial 2 Further Select Statements. Objectives for Week Data types Sort retrieved data Formatting output.
Queries and SQL in Access Please use speaker notes for additional information!
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Understanding SAS Data Step Processing Alan C. Elliott stattutorials.com.
Ceng 356-Lab2. Objectives After completing this lesson, you should be able to do the following: Limit the rows that are retrieved by a query Sort the.
Welcome to SAS…Session..!. What is SAS..! A Complete programming language with report formatting with statistical and mathematical capabilities.
“SAS macros are just text substitution!” “ARRRRGGHHH!!!”
IFS Intro. to Data Management Chapter 6 Filtering your data.
Chapter 2 Basic SQL SELECT Statements
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
SAS SQL SAS Seminar Series
Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.
Chapter 3 Single-Table Queries
Introduction to SAS Essentials Mastering SAS for Data Analytics Alan Elliott and Wayne Woodward SAS ESSENTIALS -- Elliott & Woodward1.
11 Chapter 2: Basic Queries 2.1: Overview of the SQL Procedure 2.2: Specifying Columns 2.3: Specifying Rows.
SQL/lesson 2/Slide 1 of 45 Retrieving Result Sets Objectives In this lesson, you will learn to: * Use wildcards * Use the IS NULL and IS NOT NULL keywords.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Creating and Managing Indexes Using Proc SQL Chapter 6 1.
SAS Macro: Some Tips for Debugging Stat St. Paul’s Hospital April 2, 2007.
SAS ® PROC SQL or Vanilla Flavor Cecilia Mauldin January
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
Performing Advanced Queries Using PROC SQL Chapter 2 1.
Chapter 4 concerns various SAS procedures (PROCs). Every PROC operates on: –the most recently created dataset –all the observations –all the appropriate.
Introduction to SAS Essentials Mastering SAS for Data Analytics Alan Elliott and Wayne Woodward SAS Essentials - Elliott & Woodward1.
Computing with SAS Software A SAS program consists of SAS statements. 1. The DATA step consists of SAS statements that define your data and create a SAS.
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.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
9/29/2005From Introduction to Oracle:SQL and PL/SQL, Oracle 1 Restricting and Sorting Data Kroenke, Chapter Two.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
1 SQL Chapter 9 – 8 th edition With help from Chapter 2 – 10 th edition.
Writing Basic SQL SELECT Statements Lecture
1 Checking Data with the PRINT and FREQ Procedures.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
SAS ® 101 Based on Learning SAS by Example: A Programmer’s Guide Chapter 26 By Tasha Chapman, Oregon Health Authority.
 CONACT UC:  Magnific training   
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Copyright 2009 The Little Engine That Could: Using EXCEL LIBNAME Engine Options to Enhance Data Transfers between SAS® and Microsoft® Excel Files William.
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
1 Copyright © 2009, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Chapter 5 Introduction to SQL.
Writing Basic SQL SELECT Statements
Introduction to Oracle9i: SQL
PROC SQL, Overview.
Using the Set Operators
Chapter 4 Summary Query.
Writing Basic SQL SELECT Statements
A new keyword -- calculated
The Selection Structure
Objectives In this lesson, you will learn to:
Restricting and Sorting Data
Presentation transcript:

SQL Chapter Two

Overview Basic Structure Verifying Statements Specifying Columns Specifying Rows

Introduction SQL is a modular language that uses statements and clauses.

Basic structure of PROC SQL: PROC SQL; statement (select) clauses (from, where, group by, having, order by); QUIT; Note: place semicolon at the end of the last clause only.

Statements select - specifies the columns to be selected Select statement has the following features: -selects data that meets certain conditions -groups data -specifies an order for the data -formats data -calculates new variables

Clauses from - specifies the tables to be queried where - subsets the data based on a condition - optional group by - classifies the data into groups - optional having - subsets groups of data based on a group condition order by - sorts row by the values of specific columns Note: the order of the clauses are significant.

Basic Structure Verifying Statements Specifying Columns Specifying Rows Overview

Verifying Statements Two functions that can be used to verify if your statement syntax are: validate - used to check the select statement syntax noexec - checks for invalid syntax in all types of SQL statements

Validate proc sql; validate select timemile, restpulse, maxpulse from project.fitness where timemile gt 7; NOTE: PROC SQL statement has valid syntax. proc sql; validate select timemile, restpulse, maxpulse, from project.fitness where timemile gt 7; Syntax error, expecting one of the following: a quoted string, !, !!, &...

NoExect proc sql noexec; select timemile, restpulse, maxpulse from project.fitness where timemile gt 7; NOTE: Statement not executed due to NOEXEC option.

Contrasting Features of validate: -tests syntax of query without executing the query -checks the validity of column name -prints error messages for invalid queries -is only used for select statements Features of noexec: -Checks for invalid syntax in all types of SQL statements

Overview Basic Structure Verifying Statements Specifying Columns Specifying Rows

Specifying Columns Objectives -Displaying columns directly from a table -Displaying columns calculated from other columns -Calculating columns using a CASE expression

Displaying data from a table To print all of a table columns in the order that they were stored, use an asterisk in the SELECT statement: PROC SQL; SELECT * FROM VITALS; QUIT; PATIENT PULSE TEMP BPS BPD

Printing Specify Columns If you do not want to print out all columns in a table in the order that they were stored, you can specify the columns to be printed in the order that you want them in the SELECT statement or CASE EXPRESSION in the select statement. PROC SQL; CREATE TABLE TESTMED AS SELECT PATIENT, CASE ((PATIENT/2 = INT(PATIENT/2)) + (PATIENT =.)) WHEN 1 THEN 'Med A' WHEN 0 THEN 'Med B' ELSE 'Error' END AS DOSEGRP LENGTH=5 FROM VITALS ORDER BY PATIENT; QUIT; PATIENT DOSEGRP 101 Med B 101 Med B 102 Med A 102 Med A 103 Med B 104 Med A

Calculating Columns We can calculate a new column by using data in an existing column and then naming the new column using the as function. Calculate the proportion of Units form each country CODE: OUTPUT:

Calculated columns using SAS Dates Recall from previous chapters in our SAS book that dates are stored in a different format when run through SAS. We will then use these dates to calculate new columns.

Example: Calculate the range of dates in a Dailyprices dataset. CODE: OUTPUT:

Creating new columns The use of CASE expression can be used to create a new column CODE: OUTPUT:

Creating a table To create and populate a table with the rows from an SQL query, use create table. proc sql; create table states as select state_code, state_name from d2data.state; quit; State_ Obs Code State_Name 99 UT Utah 100 VT Vermont 101 VA Virginia 102 WA Washington 103 WV West Virginia 104 WI Wisconsin 105 WY Wyoming 106 N/A

Overview Basic Structure Verifying Statements Specifying Columns Specifying Rows

Specifying Rows in a table Objectives -Selecting a subset of rows -Removing duplicate rows -Subsetting using where clauses, escape clauses, and calculated values

Selecting a subset of rows proc sql; title 'large orders'; select Product_ID, total_retail_price from d2data.order_item where total_retail_price > 1000; quit; Large orders Total Retail Price Product ID For This Product $1, $1, $1, $1, $1, $1, $1, $1, $1, $1, $1, $1,937.20

Where clause Use a where to specify a condition that data must fulfill before being selected. CODE: OUTPUT: Where clauses uses common comparisons (lt, gt, eq, etc) and logical operators (OR, Not, And, In, Is Null,...).

Removing duplications Use distinct keyword to eliminate duplications. CODE (without DISTINCT): CODE (with DISTINCT): OUTPUT:

Escape Clause The escape clause allows you to designate a single character that will indicate how proc sql will interpret LIKE wildcards when SAS is searching within a character string. CODE: OUTPUT: Example: Select observations from a string variable containing an underscore ('_').

Subsetting calculated values Since the where clause is evaluated before the select, it's possible for an error to show up since the columns used in the where clause must exist in the table or be derived from an existing column. There are two fixes for this, the first would be repeating the calculation in the where clause. The alternative method would be using CALCULATED keyword to refer to an already calculated column in the select.

Subsetting calculated values proc sql; title 'Lack of profit'; select Product_ID, ((total_retail_price/quan tity) - costprice_per_Unit) as profit from d2data.order_item where calculated profit < 3; quit; title; Lack of profit Product ID profit

SummarySummary Basic Structure PROC SQL; statement (select) clauses (from, where, group by, having, order by); QUIT; Verifying Statements validate - used to check the select statement syntax noexec - checks for invalid syntax in all types of SQL statements Specifying Columns Displaying columns directly from a table Displaying columns calculated from other columns Calculating columns using a CASE expression Specifying Rows Selecting a subset of rows Removing duplicate rows Subsetting using where clauses, escape clauses, and calculated values