TAIR Galveston 2008Ms. Bonnie Hurford, Tarleton State UniversityMs. Lauren Morton, Tarleton State University
Wednesday, February 6th, 3:30 p.m.Concurrent Sessions FAd Hoc Query Reporting SQL 101 Tips, Tools & Techniques
Topics for DiscussionWhat is SQL?Overview of SQL StatementsSyntax for SELECTJoinsRestricting RowsOperationsFunctionsCASE StatementI wish I knew thenIR SQL ExampleQuestions & Answers
What is SQL Anyway?Structured Query Language (SQL) is the industry standard for interacting with relational databasesProcesses sets of data as groups and navigates the data stored within various tablesCreate, modify tables, enter & maintain data & retrieve dataFrom Oracle 9i: SQL with an Introduction to PL/SQL by Morris-Murphy
Basic SQL StatementsSELECT: retrieve a set of data stored in the database. INSERT INTO: add new rows of data into a table UPDATE: changes existing data in your database DELETE: removes rows of data from a table Data Manipulation Language (DML)
SQL Data Definition Language (DDL) CREATE TABLE - creates a new database table ALTER TABLE - changes a table DROP TABLE - deletes a database table CREATE INDEX - creates an index (search key) DROP INDEX- deletes an index Basic SQL Statements
SELECT Statement SyntaxSELECT [DISTINCT] * or column1, column2 AS aliasFROM table1, table2 WHERE conditionsGROUP BY column1, column2HAVING group conditionsORDER BY column1, ASC | DESC;
Operations within theSELECT StatementColumn Alias assign meaningful name to a columnExample: majr_code1 AS majorDISTINCT eliminates duplicatesSELECT DISTINCT id, l_name, f_nameArithmetic Operations multiply *, divide /, add +, subtract SELECT (on_campus_hrs + off_campus_hrs) AS SCHConcatenation combine two or more columns l_name || , || f_name Smith, JohnSingle Row FunctionsSUBSTR, TRUNC, UPPER, LOWER, LENGTH, etc.
Operations within theSELECT StatementGroup Functions (multiple row function)AVG, SUM, COUNT MAX, MINCASE Statement assign more meaningful descriptions to a value in a column(CASE WHEN gender_code = F THEN FemaleWHEN gender_code = M THEN Male ELSE Not ReportedEND) AS gender;
WHERE Statements (Restricting Rows) Mathematical Comparison Operators= < > =WHERE year = 2008Other Comparison Operators IN, BETWEEN x AND y, LIKE, IS NULLWHERE year IN (2008, 2007, 2006)Logical OperatorsAND, OR, NOT
Joining TablesEquality/Simple Join method used to combine two or more tables based on a common column.SELECT a.crn, a.subject, a.crs_num, b.fac_name FROM courses a, faculty b WHERE a.fac_id = b.fac_id;a is known as the column qualifier and indicates the from which table the column is being referenced.
I Wish I Knew ThenStart a library of base SQL codeRegistered students, Admitted studentsGet to know your dataData Types, Formats, Tables, ViewsUse a SQL software tool PL/SQL DeveloperToadSQL DeveloperSQL*PlusKeep your reference materials on handSQL 101 BooksSQL Code Websites
SQL ExampleA faculty member would like a list of Fall 2007 Registered Students that have not declared a major so that he can advise them.He does not want to include students attending the Killeen and Gatesville campuses.He would like to include phone number and gender, so he will know to address them as Mr. or Ms. when he contacts them.
Take it step by step
Narrow down the data with a WHERE
Use an AND to add more conditions
Exclude rows with a NOT IN
Use a DISTINCT to return a row only once
Use a CASE statement to make the data easier for users
Finish the requestShip the data to the userMost SQL tools will provide a way to export the data to other formats like Excel or .PDF
Presenter InformationTarleton State UniversityOffice of Planning, Evaluation & Institutional ResearchBox-T 0505 Stephenville, TX 76402http://www.tarleton.edu/~opeir
Click here to load reader