+ All Categories
Home > Documents > Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Date post: 10-Feb-2022
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
26
Basic SQL for Institutional Research Phil Rhodes TAIR 2012 February 22, 2012 Concurrent Session B4
Transcript
Page 1: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Basic SQL forInstitutional Research

Phil Rhodes

TAIR 2012

February 22, 2012

Concurrent Session B4

Page 2: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Topics

• Why SQL?

• What is SQL?

• Basic Queries

• Joining Two Tables

• Simple Summary Reports

Page 3: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Why SQL?

• Institutional Research is all about data

• Data usually ‘locked up’ in relational databases (Oracle, DB2, SQL Server)

• IR offices can:• Depend on IT to give timely, correct extracts

• Extract data themselves 

• There are other tools, but some operations are easier in SQL

Page 4: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

What is SQL?

• “S‐Q‐L” vs “sequel”

• Developed at IBM in early 1970’s

• Based on a 1970 paper by Edgar F. Codd

• Standardized first in 1986

• Many variants• “English”

Page 5: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

What is SQL?

• Queries• Retrieve data 

• Data Manipulation Language (DML)• Add, update, and delete data

• Data Definition Language (DDL)• Manage structure of tables and indices

• Character, numeric, date/date‐time values

• Data Control Language (DCL)• Access control

Page 6: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

What is SQL?

• Focus on Queries

• Examples from SAS and MS Access

Page 7: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Sample Data

Student

Acad_Info Schedule

Page 8: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Sample Data

• Student table• Keys:  ID

• Bio‐Demo data

• Acad_info table• Keys:  ID, TERM

• College, Degree, Major, Classification

• Schedule table• Keys:  ID, TERM, CRN

• Course, Credit Hours

Page 9: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Basic SQL Queries

• Most basic query:select [column-list]from [table]

• Returns values of the columns in [column‐list] for all rows of the table

• An asterisk ‘*’ can be used as an alias for all columns• Useful shorthand, risky in production code

• Example 1 ‐ SAS

Page 10: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Basic SQL Queries

• Restricting the rows retrievedselect [column-list]from [table]where [logical-expression]

• Standard boolean operators AND, OR, NOT• Also IN, LIKE, BETWEEN

• Three‐valued logic – True, False, Null/Unknown• IS NULL

• Example 2 – SAS & Access

Page 11: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Joining Two Tables

• Often require data from more than one table

• Keys• Required to match rows from one table to another

• Primary and secondary keys

• Two types of joins• Inner Joins

• Outer Joins

Page 12: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Joining Two Tables

• Inner Joinsselect [column-list]from [table1] as a

inner join [table2] as bwhere a.key = b.key

• Returns data from rows in both tables where keys match

Page 13: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Joining Two Tables – Inner Join

Table 1ID Gender

1 M

2 M

3 F

4 F

Table 2ID Classification

1 Freshman

3 Sophomore

5 Junior

7 Senior

select a.id, a.gender,b.classification

from table1 as ainner join table2 as b

where a.id = b.id

Page 14: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Joining Two Tables – Inner Join

ID Gender Classification

1 M Freshman

3 F Sophomore

select a.id, a.gender,b.classification

from table1 as ainner join table2 as b

where a.id = b.id

Result

Page 15: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Joining Two Tables – Inner Join 

• Example 3

Page 16: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Joining Two Tables

• Outer Joinsselect [column-list]from [table1] as a

left join [table2] as bon a.key = b.key

• Returns all rows from table1 plus matching rows in table2

Page 17: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Joining Two Tables – Outer Join

Table 1ID Gender

1 M

2 M

3 F

4 F

Table 2ID Classification

1 Freshman

3 Sophomore

5 Junior

7 Senior

select a.id, a.gender,b.classification

from table1 as aleft join table2 as b

on a.id = b.id

Page 18: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Joining Two Tables – Outer Join

ID Gender Classification

1 M Freshman

2 M

3 F Sophomore

4 F

select a.id, a.gender,b.classification

from table1 as aleft join table2 as b

on a.id = b.id

Result

Page 19: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Joining Two Tables – Outer Join 

• Example 4

Page 20: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Joining Two Tables – Issues

• Bad or Missing WHERE statements• Cartesian products 

• Multiplying Observations• Duplicated data if more than one row in a table matches criterion

• Sometimes good, sometimes sign of an error

Page 21: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Basic Reporting

• GROUP BY• Combine rows with common values

• Often used with SQL aggregation functions like SUM()• SUM, MIN, MAX, AVG, COUNT, VAR, STDEV

• WHERE is applied before GROUP BY

• HAVING • Similar to WHERE, but applied after GROUP BY

• Can use aggregation functions

Page 22: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Basic Reporting

• ORDER BY• Order the result data by the values of certain columns

• ASCENDING or DESCENDING

Page 23: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Basic Reporting

• Example 5:  Create a report of the number of students by home state.

• Example 6:  Create a report by classification of students taking more than 18 hours

Page 24: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Resources

• SAS Online Documentation• support.sas.com/onlinedoc/913/docMainpage.jsp

• Base SAS  Base SAS Procedures Guide  SAS SQL Procedure User’s Guide

• MS Access SQL Documentation• office.microsoft.com/en‐us/access‐help/CH010072899.aspx

• SQL Tutorial• www.sqltutorial.org

Page 25: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Resources

• O’Reilly Books (shop.oreilly.com)• Learning SQL, 2nd Edition

• SQL In a Nutshell, 3rd Edition

• SQL Cookbook, 1st Edition 

Page 26: Session B4 - Basic SQL for Institutional Research - TAIR-Texas

Questions?

Phil Rhodes

Houston Baptist University

[email protected]

281‐649‐3417


Recommended