Sei sulla pagina 1di 38

SUNGARD SUMMIT 2007 | sungardsummit.

com

Introduction to SQL
Presented by: Jennifer Flagel
George Mason University
March 20, 2007
Course ID 1113

A Community of Learning

Introduction
SQL = Structured Query Language
Communicate the benefits of learning SQL
Provide a basic understanding of relational database
structures
Translate Banner application data to its underlying
Oracle source
Introduce basic SQL commands for querying Banner
data
Use the Microsoft Access SQL editor to perform
simple queries against sample General Person data

Course ID 1113

SUNGARD SUMMIT 2007 | sungardsummit.com

Benefits of SQL

A Community of Learning

Why Learn SQL?


Improves your ability to articulate requirements to
technical staff.
Assists in defining population selection rules in Banner.
Enhances your understanding of how Banner operates.
Provides greater flexibility for more complex queries.

Course ID 1113

SUNGARD SUMMIT 2007 | sungardsummit.com

Relational Database
Structure

A Community of Learning

Relational Data Model


Each table consists of columns and rows that describe a
set of related items (eg., people).
Each row represents a single entity (eg., a person).
Each column represents an attribute of that entity (eg.,
first name, last name, ID number).
Every row in a table must be uniquely identified by the
same attribute or set of attributes, defined as the
primary key (eg, PIDM).
Each table is related to one or more other tables through
a shared attribute, defined as the foreign key (eg, PIDM).

Course ID 1113

Sample Relational Table


Each row is uniquely
identified by the primary key.

Each column represents a specific


attribute of all entities (people).

Each row contains a combination of attributes


that represents a single entity (person).

Course ID 1113

Linking Relational Tables

These tables are related through the shared PIDM attribute.


SPRIDEN
SPRIDEN_PIDM
SPRIDEN_ID
SPRIDEN_LAST_NAME
SPRIDEN_FIRST_NAME
SPRIDEN_CHANGE_IND
SPBPERS
SPBPERS_PIDM
SPBPERS_SSN
SPBPERS_BIRTH_DATE
SPBPERS_SEX

SPRADDR
SPRADDR_PIDM
SPRADDR_STREET_LINE1
SPRADDR_CITY
SPRADDR_STAT_CODE
SPRADDR_ZIP

Course ID 1113

Related General Person Data


The shared PIDM links identification, biographic, and
demographic data from multiple tables with minimal redundancy.

Course ID 1113

SUNGARD SUMMIT 2007 | sungardsummit.com

Translating Between
Banner and Oracle

A Community of Learning

10

Banner Schemas
Banner data is grouped by process area, similar to the
Banner menu structure
Tables are grouped by owner, or schema

GENERAL owns general tables (G%)


SATURN owns student tables (S%)
TAISMGR owns accounts receivable tables (T%)
FAISMGR owns financial aid tables (R%)
FIMSMGR owns finance tables (F%)
PAYROLL owns payroll tables (P%)
POSNCTL owns employee tables (N%)
ADISMGR owns alumni/development tables (A%)
BANSECR owns security tables (subset of G%)
BANINST1 owns Object:Access views

Course ID 1113

11

Banner Table Names

Same convention as form names


First letter describes Banner system
Second letter describes module within that system
Third letter describes type of table
V = validation table
B = base table
R = repeating table

Fourth-Seventh letters describe content

Course ID 1113

12

Translating Banner Forms to Tables


Replace third character with V, R, or B
STVATYP form = STVATYP table
SPAIDEN form = SPRIDEN table
SPAPERS form = SPBPERS table

Multiple form blocks represent multiple tables

SPAIDEN ID = SPRIDEN
SPAIDEN Address = SPRADDR
SPAIDEN Telephone = SPRTELE
SPAIDEN Biographical = SPBPERS

Use Help>Dynamic Help Query to view table and column


name

Course ID 1113

13

Other Naming Conventions


Column name prefaced by table name
SPRIDEN_PIDM
SPRIDEN_LAST_NAME

Validated columns reference validation table and end in


_CODE
SPRADDR_ATYP_CODE = STVATYP_CODE
SPRIDEN_NTYP_CODE = STVNTYP_CODE

Indicator columns end in _IND


SPRIDEN_CHANGE_IND
SPBPERS_DEAD_IND

Repeating rows ordered by _SEQNO column


SPRADDR_SEQNO
SPRTELE_SEQNO

Course ID 1113

14

SUNGARD SUMMIT 2007 | sungardsummit.com

Basic SQL Commands

A Community of Learning

15

SQL Syntax
ANSI approved standard
Proprietary variations
Must support standard keywords
Oracle uses SQL*Plus

Basic command structure:


SELECT column
FROM table
[WHERE condition]
[ORDER BY column];

Sample query to select name and ID for all entities:


SELECT spriden_last_name, spriden_first_name, spriden_id
FROM spriden;

Course ID 1113

16

Common Statements
DESC table
Produces list of columns and properties

SELECT *
Selects all columns from specified table

SELECT DISTINCT column


Displays unique rows for selected columns

Course ID 1113

17

Conditions and Operators


WHERE clause specifies any number of conditions
AND must satisfy all conditions
OR must satisfy at least one condition
( ) used to group and prioritize conditions

Operators are used to compare values

= equal to
<> not equal to
< less than
> greater than

BETWEEN includes start and end values


IN specifies a list of values
LIKE must be used with wildcard
IS NULL absence of any value
NOT negates other operators

Values must conform to data type of column

Character and date values enclosed in single quotes


Character values case sensitive
Date values format sensitive
Default date format set locally (eg: DD-MON-YYYY)

Course ID 1113

18

Practice 1: Basic queries


Describe SPRIDEN
DESC spriden;

Select all columns from SPRIDEN


SELECT *
FROM spriden;

Select ID and name for all rows in SPRIDEN


SELECT spriden_id, spriden_last_name, spriden_first_name
FROM spriden;

Select current ID and name for all persons, ordered by


last name and first name
SELECT spriden_id, spriden_last_name, spriden_first_name
FROM spriden
WHERE spriden_change_ind IS NULL
AND spriden_entity_ind = P
ORDER BY spriden_last_name, spriden_first_name;
Course ID 1113

19

Joins
Joins are used to select data from multiple tables.
Each join connects two related tables.
Inner join (also called natural join) selects only those
rows where the shared attribute exists in both tables.
Outer join selects all rows from one table regardless of
whether the shared attribute exists in the other.
Left outer join selects all rows from first table regardless of
match in second table.
Right outer join selects all rows from second table
regardless of match in first table.

New join syntax for Oracle 9i and higher


SELECT column
FROM table1
JOIN table2
ON table1.column = table2.column;
Course ID 1113

20

Practice 2: Inner Join


Select current name, ID, and birth date for all persons
SELECT spriden_last_name, spriden_first_name,
spriden_id, spbpers_birth_date
FROM spriden
JOIN spbpers
ON spriden_pidm = spbpers_pidm
WHERE spriden_change_ind IS NULL
AND spriden_entity_ind = P;

Course ID 1113

21

Practice 2: Left Join


Select current name, ID, and birth date for all persons
SELECT spriden_last_name, spriden_first_name,
spriden_id, spbpers_birth_date
FROM spriden
LEFT JOIN spbpers
ON spriden_pidm = spbpers_pidm
WHERE spriden_entity_ind = P
AND spriden_change_ind IS NULL;

Course ID 1113

22

Practice 2: Right Join


Select current name, ID, and birth date for all persons
SELECT spriden_last_name, spriden_first_name,
spriden_id, spbpers_birth_date
FROM spriden
RIGHT JOIN spbpers
ON spriden_pidm = spbpers_pidm
WHERE spriden_entity_ind = P
AND spriden_change_ind IS NULL;

Course ID 1113

23

Practice 2: Multiple Joins


Select current name and gender for all persons with
mailing address in Virginia
SELECT spriden_last_name, spriden_first_name, spbpers_sex
FROM spriden
LEFT JOIN spbpers
ON spriden_pidm = spbpers_pidm
JOIN spraddr
ON spriden_pidm = spraddr_pidm
WHERE spriden_change_ind IS NULL
AND spraddr_atyp_code = MA
AND spraddr_stat_code = VA;

Course ID 1113

24

Subqueries
Produce subset of data for reference in another query
Can be used as table in FROM
SELECT column
FROM
(SELECT column
FROM table)
WHERE condition;

Can be used as a condition in WHERE


SELECT column
FROM table
WHERE column operator
(SELECT column
FROM table);

Course ID 1113

25

Practice 3: Subquery in FROM


Select current name and ID for all persons without
confidential indicator
SELECT spriden_last_name, spriden_first_name, spriden_id
FROM spriden
LEFT JOIN
(SELECT spbpers_pidm
FROM spbpers
WHERE spbpers_confid_ind = Y)
ON spriden_pidm = spbpers_pidm
WHERE spriden_change_ind IS NULL
AND spbpers_pidm IS NULL;

Course ID 1113

26

Practice 3: Subquery in WHERE


Select current name and ID for all persons without
mailing address
SELECT spriden_last_name, spriden_first_name,
spriden_id
FROM spriden
WHERE spriden_change_ind IS NULL
AND spriden_pidm NOT IN
(SELECT spraddr_pidm
FROM spraddr
WHERE spraddr_atyp_code = MA);

Course ID 1113

27

Aggregate Functions
Perform mathematical summaries over a group of rows

MIN
MAX
COUNT
AVG
SUM

Results in an aggregate total for each unique


combination of selected columns
Must specify non-aggregate columns in GROUP BY
SELECT column1, column2, SUM(column3)
FROM table
WHERE condition
GROUP BY column1, column2;

Course ID 1113

28

Practice 4: Aggregation
Select count of persons by ethnicity and gender
SELECT spbpers_ethn_code, spbpers_sex,
COUNT(spriden_pidm)
FROM spriden
LEFT JOIN spbpers
ON spriden_pidm = spbpers_pidm
WHERE spriden_entity_ind = P
AND spriden_change_ind IS NULL
GROUP BY spbpers_ethn_code, spbpers_sex;

Course ID 1113

29

Advanced Options

Aliases
Concatenation
Literals
Substitution Variables
Calculations
Set Operators
UNION
INTERSECT
MINUS

Character Strings
Substring
Instring

Course ID 1113

Data Type Conversions


TO_DATE
TO_NUMBER
TO_CHAR

Formats

UPPER
LOWER
NVL
CASE
DECODE
ROUND
TRUNC

30

Practice 5: Aliases and Substitution Variables


Select most recent mailing address for a student.
SELECT s.spriden_id ID, a.*
FROM spriden s
JOIN spraddr a
ON s.spriden_pidm = a.spraddr_pidm
JOIN (select spraddr_pidm, spraddr_atyp_code,
MAX(spraddr_seqno) maxseq
FROM spraddr
GROUP BY spraddr_pidm, spraddr_atyp_code) b
ON a.spraddr_pidm = b.spraddr_pidm
AND a.spraddr_atyp_code = b.spraddr_atyp_code
AND a.spraddr_seqno = b.maxseq
WHERE spraddr_atyp_code = MA
AND spriden_id = :ID
AND spriden_change_ind IS NULL;
Course ID 1113

31

Practice 5: Concatenation and Literals


Select full name for all persons
SELECT spriden_first_name|| ||spriden_mi
|| ||spriden_last_name name
FROM spriden
WHERE spriden_id = :ID
AND spriden_change_ind IS NULL;

Course ID 1113

32

Practice 5: Format Using CASE


Select count of persons by gender description (Male,
Female, or Unknown).
SELECT CASE WHEN spbpers_sex = F
THEN Female
WHEN spbpers_sex = M
THEN Male
ELSE Unknown
END gender,
COUNT(spbpers_pidm) total
FROM spbpers
GROUP BY spbpers_sex;

Course ID 1113

33

Practice 5: Format Using NVL


Select all active addresses.
SELECT *
FROM spraddr
WHERE NVL(spraddr_status_ind, A) = A;

Course ID 1113

34

Exercises
Use Microsoft Access SQL editor to practice writing
queries against the sample tables

Course ID 1113

35

Learning More

Training
SQL*Plus user manuals
Online resources
View generated SQL

Course ID 1113

36

Questions and Answers

Course ID 1113

37

Thank You!
Jennifer Flagel
jflagel@gmu.edu

Please complete the online class evaluation form


Course ID 1113

SunGard, the SunGard logo, Banner, Campus Pipeline, Luminis, PowerCAMPUS, Matrix, and Plus are trademarks or registered
trademarks of SunGard Data Systems Inc. or its subsidiaries in the U.S. and other countries. Third-party names and marks
referenced herein are trademarks or registered trademarks of their respective owners.
2006 SunGard. All rights reserved.

Course ID 1113

38

Potrebbero piacerti anche