TAIR Galveston 2008TAIR Galveston 2008
Ms. Bonnie Hurford, Tarleton State UniversityMs. Bonnie Hurford, Tarleton State UniversityMs. Lauren Morton, Tarleton State UniversityMs. Lauren Morton, Tarleton State University
Wednesday, February 6th, 3:30 p.m.Wednesday, February 6th, 3:30 p.m.Concurrent Sessions FConcurrent Sessions F
Ad Hoc Query Reporting – Ad Hoc Query Reporting – SQL 101 Tips, Tools & SQL 101 Tips, Tools &
TechniquesTechniques
Topics for Discussion
What is SQL?What is SQL? Overview of SQL StatementsOverview of SQL Statements Syntax for SELECTSyntax for SELECT
JoinsJoins Restricting RowsRestricting Rows OperationsOperations FunctionsFunctions CASE StatementCASE Statement
I wish I knew then…I wish I knew then… IR SQL ExampleIR SQL Example Questions & AnswersQuestions & Answers
What is SQL Anyway?What is SQL Anyway?
Structured Query Language (SQL) is the Structured Query Language (SQL) is the industry standard for interacting with industry standard for interacting with relational databasesrelational databases
Processes sets of data as groups and Processes sets of data as groups and navigates the data stored within various navigates the data stored within various tablestables
Create, modify tables, enter & maintain Create, modify tables, enter & maintain data & retrieve datadata & retrieve data
From “Oracle 9i: SQL with an Introduction to PL/SQL” by Morris-Murphy
Basic SQL StatementsBasic SQL Statements
SELECT: retrieve a set of data stored in SELECT: retrieve a set of data stored in the database. the database.
INSERT INTO: add new rows of data INSERT INTO: add new rows of data into a table into a table
UPDATE: changes existing data in your UPDATE: changes existing data in your database database
DELETE: removes rows of data from a DELETE: removes rows of data from a table table
Data Manipulation Language (DML)Data Manipulation Language (DML)
SQL Data Definition Language (DDL) SQL Data Definition Language (DDL)
CREATE TABLE - creates a new CREATE TABLE - creates a new database table database table
ALTER TABLE - changes a table ALTER TABLE - changes a table DROP TABLE - deletes a database table DROP TABLE - deletes a database table CREATE INDEX - creates an index CREATE INDEX - creates an index
(search key) (search key) DROP INDEX - deletes an index DROP INDEX - deletes an index
Basic SQL StatementsBasic SQL Statements
SELECT Statement SyntaxSELECT Statement Syntax
SELECTSELECT [DISTINCT] * [DISTINCT] * oror column1, column1, column2 AS column2 AS aliasalias……
FROMFROM table1, table2… table1, table2…
WHEREWHERE conditions conditions
GROUP BYGROUP BY column1, column2… column1, column2…
HAVINGHAVING group conditions group conditions
ORDER BYORDER BY column1,… ASC | DESC; column1,… ASC | DESC;
Operations within theOperations within theSELECT StatementSELECT Statement
Column AliasColumn Alias – assign meaningful name to a column – assign meaningful name to a column Example: majr_code1 Example: majr_code1 ASAS major major
DISTINCTDISTINCT – eliminates duplicates – eliminates duplicates SELECT DISTINCT id, l_name, f_name…SELECT DISTINCT id, l_name, f_name…
Arithmetic OperationsArithmetic Operations – multiply *, divide /, add +, – multiply *, divide /, add +, subtract –subtract – SELECT (on_campus_hrs + off_campus_hrs) AS SCHSELECT (on_campus_hrs + off_campus_hrs) AS SCH
ConcatenationConcatenation – combine two or more columns – combine two or more columns l_name || ‘, ’ || f_name l_name || ‘, ’ || f_name Smith, JohnSmith, John
Single Row FunctionsSingle Row Functions SUBSTR, TRUNC, UPPER, LOWER, LENGTH, etc.SUBSTR, TRUNC, UPPER, LOWER, LENGTH, etc.
Group FunctionsGroup Functions (multiple row function) (multiple row function) AVG, SUM, COUNT MAX, MINAVG, SUM, COUNT MAX, MIN
CASE StatementCASE Statement – assign more meaningful – assign more meaningful descriptions to a value in a columndescriptions to a value in a column ((CASE CASE
WHEN gender_code = ‘F’ THEN ‘Female’WHEN gender_code = ‘F’ THEN ‘Female’
WHEN gender_code = ‘M’ THEN ‘Male’ WHEN gender_code = ‘M’ THEN ‘Male’
ELSE ‘Not Reported’ELSE ‘Not Reported’
END) AS gender;END) AS gender;
Operations within theOperations within theSELECT StatementSELECT Statement
WHERE Statements WHERE Statements (Restricting Rows) (Restricting Rows)
Mathematical Comparison OperatorsMathematical Comparison Operators = <> < > <= >== <> < > <= >= WHERE year = ‘2008’WHERE year = ‘2008’
Other Comparison OperatorsOther Comparison Operators IN, BETWEEN x AND y, LIKE, IS NULLIN, BETWEEN x AND y, LIKE, IS NULL WHERE year IN (‘2008’, ‘2007’, ‘2006’)WHERE year IN (‘2008’, ‘2007’, ‘2006’)
Logical OperatorsLogical Operators AND, OR, NOTAND, OR, NOT
Joining TablesJoining Tables
Equality/Simple JoinEquality/Simple Join – method used to – method used to combine two or more tables based on a combine two or more tables based on a common column.common column. SELECT a.crn, a.subject, a.crs_num, SELECT a.crn, a.subject, a.crs_num,
b.fac_nameb.fac_nameFROM courses a, faculty bFROM courses a, faculty bWHERE a.fac_id = b.fac_id;WHERE a.fac_id = b.fac_id;
““aa” is known as the ” is known as the column qualifiercolumn qualifier and indicates the from which table the and indicates the from which table the column is being referenced.column is being referenced.
I Wish I Knew Then…I Wish I Knew Then…
Start a library of base SQL codeStart a library of base SQL code Registered students, Admitted studentsRegistered students, Admitted students
Get to know your dataGet to know your data Data Types, Formats, Tables, ViewsData Types, Formats, Tables, Views
Use a SQL software toolUse a SQL software tool PL/SQL DeveloperPL/SQL Developer ToadToad SQL DeveloperSQL Developer SQL*PlusSQL*Plus
Keep your reference materials on handKeep your reference materials on hand SQL 101 BooksSQL 101 Books SQL Code Websites SQL Code Websites
http://www.w3schools.com/sql/http://www.w3schools.com/sql/
SQL ExampleSQL Example
A faculty member would like a list of Fall A faculty member would like a list of Fall 2007 Registered Students that have not 2007 Registered Students that have not declared a major so that he can advise them.declared a major so that he can advise them.
He does not want to include students He does not want to include students attending the Killeen and Gatesville attending the Killeen and Gatesville campuses.campuses.
He would like to include phone number and He would like to include phone number and gender, so he will know to address them as gender, so he will know to address them as Mr. or Ms. when he contacts them.Mr. or Ms. when he contacts them.
Take it step by step…Take it step by step…
Narrow down the data with a Narrow down the data with a WHEREWHERE
Use an AND to add more Use an AND to add more conditionsconditions
Exclude rows with a NOT INExclude rows with a NOT IN
Use a DISTINCT to return a Use a DISTINCT to return a row only oncerow only once
Concatenate fieldsConcatenate fields
Use a CASE statement to Use a CASE statement to make the data easier for usersmake the data easier for users
Finish the requestFinish the request
Ship the data to the userShip the data to the user Most SQL tools will provide a way to Most SQL tools will provide a way to
export the data to other formats like export the data to other formats like Excel or .PDFExcel or .PDF
Questions?Questions?
Presenter InformationPresenter Information
Tarleton State UniversityTarleton State University Office of Planning, Evaluation & Office of Planning, Evaluation &
Institutional ResearchInstitutional Research Box-T 0505Box-T 0505
Stephenville, TX 76402Stephenville, TX 76402 http://www.tarleton.edu/~opeirhttp://www.tarleton.edu/~opeir