+ All Categories
Transcript
Page 1: TAIR Galveston 2008

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

Page 2: TAIR Galveston 2008

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

Page 3: TAIR Galveston 2008

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

Page 4: TAIR Galveston 2008

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)

Page 5: TAIR Galveston 2008

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

Page 6: TAIR Galveston 2008

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;

Page 7: TAIR Galveston 2008

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.

Page 8: TAIR Galveston 2008

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

Page 9: TAIR Galveston 2008

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

Page 10: TAIR Galveston 2008

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.

Page 11: TAIR Galveston 2008

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

Page 12: TAIR Galveston 2008
Page 13: TAIR Galveston 2008
Page 14: TAIR Galveston 2008
Page 15: TAIR Galveston 2008

http://www.w3schools.com/sql/http://www.w3schools.com/sql/

Page 16: TAIR Galveston 2008

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.

Page 17: TAIR Galveston 2008

Take it step by step…Take it step by step…

Page 18: TAIR Galveston 2008

Narrow down the data with a Narrow down the data with a WHEREWHERE

Page 19: TAIR Galveston 2008

Use an AND to add more Use an AND to add more conditionsconditions

Page 20: TAIR Galveston 2008

Exclude rows with a NOT INExclude rows with a NOT IN

Page 21: TAIR Galveston 2008

Use a DISTINCT to return a Use a DISTINCT to return a row only oncerow only once

Page 22: TAIR Galveston 2008

Concatenate fieldsConcatenate fields

Page 23: TAIR Galveston 2008

Use a CASE statement to Use a CASE statement to make the data easier for usersmake the data easier for users

Page 24: TAIR Galveston 2008

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

Page 25: TAIR Galveston 2008

Questions?Questions?

Page 26: TAIR Galveston 2008

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


Top Related