banner and the sql select statement: part two (single table selects) mark holliday department of...

91
Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University 14 October, 28 October, and 4 November (updated: 4 November 2005)

Upload: august-reeves

Post on 05-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Banner and the SQL Select Statement: Part Two (Single Table Selects)

Mark HollidayDepartment of Mathematics and

Computer ScienceWestern Carolina University

14 October, 28 October, and 4 November (updated: 4 November 2005)

Page 2: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Outline

The Goal The Concepts

A First Example Single Table Selects Joins Multiple Connected Select Statements

Page 3: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

A First Example

Outline The Relational Model: Single Table Lab 1: TOAD, Schema Browser Some Structured Query Language (SQL)

Basics Lab 2: TOAD, SQL Editor

Page 4: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Single Table Selects

Outline WHERE clause: single condition, multiple

conditions Lab 3: Aliases; Order By; Aggregate Functions Lab 4: Group By; Having Lab 5:

Page 5: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

WHERE ClauseWHERE Clause We might want to keep on specific rows from

a table based on a condition => where clause

SELECT "column_name" FROM "table_name" WHERE "condition"

We then project onto the columns of interest => select clause

Page 6: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

WHERE ClauseWHERE Clause (franz) (franz)

This condition can be a simple condition, or it can be a compound condition.

Compound conditions are made up of multiple simple conditions connected by AND or OR.

There is no limit to the number of simple conditions that can be present in a single SQL statement.

SELECT "column_name" FROM "table_name" WHERE "simple condition" {[AND|OR] "simple condition"}+

Page 7: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

WHERE ClauseWHERE Clause

Each simple condition must evaluate to the value True or the value False.

Operators within a simple condition? arithmetic comparison operators is, is not in, between like

Page 8: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Arithmetic Comparison Operators Arithmetic Comparison Operators (franz)(franz)

WHERE age > 18

Page 9: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

English Query: Suppose we were going to offer a new scholarship

from Ben & Jerry’s Ice Cream Company. The eligible students are listed in the spriden table. Eligible student need to have a first name of ‘Arnold’. We want to find the first and last names of the eligible

students.

Solution?

A Single Condition ExampleA Single Condition Example

Page 10: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

SELECT spriden_last_name, spriden_first_nameFROM spridenWHERE spriden_first name = ‘Arnold’

A Single Condition ExampleA Single Condition Example

Page 11: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

How do we remember the column names in the spriden table?

They are in the Columns tab of the Schema Browser

open Schema Brower, make Saturn the owner, select the SPRIDEN table, select the Columns tab (it is the default)

A Single Condition ExampleA Single Condition Example

Page 12: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

How do we remember the column names in the spriden table?

Shortcut: in the sql statement in the SQL editor select

the table name, spriden, right-click to open a menu select the Describe menu item (near the

bottom) the Columns tab from the Schema Browser

for that table appears

A Single Condition ExampleA Single Condition Example

Page 13: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Problem: In spriden the spriden_pidm column is

not a key => need not determine a unique row

Why? PIDM refers to a particular person, but

a person may have several spriden rows.

SPRIDEN_CHANGE_IND is NULLSPRIDEN_CHANGE_IND is NULL

Page 14: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Why? The information about a person

changes (e.g. address) but we want to keep the old information

=> the person will have multiple spriden rows.

SPRIDEN_CHANGE_IND is NULLSPRIDEN_CHANGE_IND is NULL

Page 15: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Question: Of all the spriden rows for a particular person (all have the same PIDM) how do we find the row with the person’s current information?

Answer: That row has the null value in the change indicator field, spriden_change_ind

SPRIDEN_CHANGE_IND is NULLSPRIDEN_CHANGE_IND is NULL

Page 16: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Question: What is the NULL value?

Answer: Every column has a data type (e.g. INTEGER,

VARCHAR) What if the column entry is currently

empty? We can’t use any value in the data type. Solution: Use a special value called NULL.

SPRIDEN_CHANGE_IND is NULLSPRIDEN_CHANGE_IND is NULL

Page 17: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Question: How do you check for the NULL value?

Answer: Use the IS operator or the IS NOT

operator. where spriden_change_ind is null

Can not use the = or <> operators

SPRIDEN_CHANGE_IND is NULLSPRIDEN_CHANGE_IND is NULL

Page 18: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Problem: For a Single Condition Example above

we really need two conditions

spriden_change_ind is nullspriden_first_name = ‘Arnold’

and both conditions must be true

A Two Condition ExampleA Two Condition Example

Page 19: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Truth Table for AND logical operatorTruth Table for AND logical operator

FalseFalseFalse

FalseTrueFalse

FalseFalseTrue

TrueTrueTrue

Opd1 AND Opd2Opd2Opd1

Page 20: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Truth Table for OR logical operatorTruth Table for OR logical operator

FalseFalseFalse

TrueTrueFalse

TrueFalseTrue

TrueTrueTrue

Opd1 OR Opd2Opd2Opd1

Page 21: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

SELECT spriden_last_name, spriden_first_name

FROM spridenWHERE

(spriden_change_ind is null) and (spriden_first name = ‘Arnold’)

A Two Condition ExampleA Two Condition Example

Page 22: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

SELECT spriden_last_name, spriden_first_name

FROM spridenWHERE spriden_change_ind is null

and spriden_first name = ‘Arnold’

A Second Equivalent Two Condition A Second Equivalent Two Condition ExampleExample

Page 23: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Suppose we were going to offer a new scholarship from Ben & Jerry’s Ice Cream Company.

In order to apply eligible students need to have

a first name of ‘Arnold’ or or a first name of ‘Arturo’ and have active records (that is change

indicator must be null)

Solution?

A Three Condition ExampleA Three Condition Example (franz)(franz)

Page 24: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

SELECT spriden_last_name, spriden_first_nameFROM spridenWHERE spriden_change_ind is null and

(spriden_first name = ‘Arnold’ or spriden_first_name = ‘Arturo’)

Note that the first name can be Arnold OR Arturo. But, in either case, the change indicator must be null.

A Three Condition ExampleA Three Condition Example (franz)(franz)

Page 25: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

SELECT spriden_last_name, spriden_first_nameFROM spridenWHERE (spriden_change_ind is null and

spriden_first name = ‘Arnold’) or spriden_first_name = ‘Arturo’

How is this query different?

The Importance of ParenthesesThe Importance of Parentheses

Page 26: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

SELECT spriden_last_name, spriden_first_nameFROM spridenWHERE (spriden_change_ind is null and

spriden_first name = ‘Arnold’) or spriden_first_name = ‘Arturo’

Like the previous query this query includes students who have a first name of ‘Arnold’ and a spriden_change_ind value of null.

However this query includes all students who have a first name of ‘Arturo’ even those who have spriden_change_ind not equal to null.

The Importance of ParenthesesThe Importance of Parentheses

Page 27: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

SELECT spriden_last_name, spriden_first_nameFROM spridenWHERE spriden_change_ind is null and

spriden_first name = ‘Arnold’ or spriden_first_name = ‘Arturo’

To which of the statements before is this one equivalent?

The Importance of ParenthesesThe Importance of Parentheses

Page 28: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

AND is of higher precedence than OR

the query is equivalent to SELECT spriden_last_name, spriden_first_name

FROM spridenWHERE (spriden_change_ind is null and

spriden_first name = ‘Arnold’) or spriden_first_name = ‘Arturo’

Morale: Use parentheses for readability and to avoid

surprises

The Importance of ParenthesesThe Importance of Parentheses

Page 29: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

SELECT spriden_last_name, spriden_first_nameFROM spridenWHERE spriden_first name = ‘Arnold’

or spriden_first_name = ‘Arturo’and spriden_change_ind is null

To which of the statements before is this one equivalent?

The Importance of ParenthesesThe Importance of Parentheses

Page 30: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Answer: None! Because AND is higher precedence than OR, the query is equivalent to:

SELECT spriden_last_name, spriden_first_nameFROM spridenWHERE spriden_first name = ‘Arnold’

or (spriden_first_name = ‘Arturo’and spriden_change_ind is null)

The Importance of ParenthesesThe Importance of Parentheses

Page 31: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

SELECT spriden_last_name, spriden_first_nameFROM spridenWHERE spriden_change_ind is null and

(spriden_first name = ‘Arnold’ or ‘Arturo’)

What will this query do?

A Proposed QueryA Proposed Query

Page 32: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Query will generate an error since one operand to the OR operator is not

a boolean value (that is, True or False)

which one? ‘Arturo’

Morale: SQL is pickier than English sometimes.

A Proposed QueryA Proposed Query

Page 33: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

IN Operator IN Operator (franz)(franz)

SELECT "column_name" FROM "table_name" WHERE "column_name" IN ('value1', 'value2', ...)

In SQL, there are two uses of the IN keyword, and this section introduces the one that is related to the WHERE clause.

When used in this context, we know exactly the value of the returned values we want to see for at least one of the columns.

Page 34: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

IN Operator IN Operator (franz)(franz)

The number of values in the parenthesis can be one or more, with each value separated by comma.

Values can be numerical or characters.

If there is only one value inside the parenthesis, this command is equivalent to:

WHERE "column_name" = 'value1'

Character values should be enclosed in single quotes.

Numeric values would not be enclosed in quotes.

Page 35: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

The example below would select valid states codes and descriptions from the valid state table that are either of the values “NC” or “SC”.

An Example of the IN Operator An Example of the IN Operator (franz) (franz)

Page 36: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

SELECT stvstat_code, stvstat_desc

FROM stvstatWHERE stvstat_code IN (‘NC’, ‘SC’)

An Example of the IN Operator (cont.) An Example of the IN Operator (cont.) (franz)(franz)

Page 37: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

BETWEEN Operator (franz)BETWEEN Operator (franz)

SELECT "column_name" FROM "table_name" WHERE "column_name" BETWEEN 'value1‘ AND 'value2'

Whereas the IN keyword helps people limit the selection criteria to one or more discrete values, the BETWEEN keyword allows for selecting a range.

Page 38: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Perhaps you wanted to know the names of the first 100 names loaded onto the ‘spriden’ table. (As they were loaded, the PIDM was assigned as a sequentially incremented integer.)

This example pulls all pidms, last and first names, and change indicators, from spriden for PIDM’s between 0 and 100.

An Example of the Between OperatorAn Example of the Between Operator (franz)(franz)

Page 39: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

SELECT spriden_pidm, spriden_last_name, spriden_first_name, spriden_change_ind

FROM spriden WHERE spriden_pidm BETWEEN 0 and 100

An Example of the Between OperatorAn Example of the Between Operator (cont.)(franz)(cont.)(franz)

Page 40: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

LIKELIKE

SELECT "column_name" FROM "table_name" WHERE "column_name" LIKE {PATTERN}

LIKE is another keyword that is used in the WHERE clause. We previously saw an example where we pulled name information like ‘Er%’ in the first name field.

Basically, LIKE allows you to do a search based on a pattern, rather than specifying exactly what is desired (as when using the IN syntax) or spell out a range (as in BETWEEN).

Page 41: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

The syntax for LIKE is: The syntax for LIKE is: (franz)(franz)

LIKE {PATTERN} where {PATTERN} often consists of wildcards

Here are some examples:

Page 42: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Examples of the LIKE Operator Examples of the LIKE Operator (franz)(franz)

'A_Z': All strings that starts with 'A', contains one other character, and ends with 'Z'.

For example, 'ABZ' and 'A2Z' would both satisfy the condition, while 'AKKZ' would not (because there are two characters between A and Z instead of one).

'ABC%': All strings that start with 'ABC'. For example, 'ABCD' and 'ABCABC' would both satisfy the

condition. '%XYZ': All strings that end with 'XYZ'.

For example, 'WXYZ' and 'ZZXYZ' would both satisfy the condition.

'%AN%': All string that contain the pattern 'AN' anywhere. For example, 'LOS ANGELES' and 'SAN FRANCISCO' would both

satisfy the condition.

Page 43: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

A first LIKE Example A first LIKE Example (franz)(franz)

SELECT *FROM spridenWHERE spriden_first_name like ‘Er%’

Strings must be in single quotes. Also, please note Banner name fields are CASE sensitive. (Last and first names are mixed case.)

This example would retrieve “Ernie Jones” and “Ernestine Smith” but NOT “ERNIE Jones” or “ERNESTINE Smith” (which are capitalized).

Page 44: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

A Second LIKE Example A Second LIKE Example (franz)(franz)

SELECT *FROM spridenWHERE

spriden_change_ind is null and spriden_last_name like ‘Smith%’

Page 45: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Three

Objectives: Develop competence with the WHERE

clause in single table select statements Steps

First query Second query Third query

Page 46: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Three

First Query: Find the pidms and area codes of all the

people who have area codes that are 608 or 414. Hint: use the sprtele table.

Do in two different ways.

Page 47: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Three

First Query Solution (First Way): select sprtele_pidm, sprtele_area_codefrom sprtelewhere sprtele_area_code in (‘608’, ‘414’)

Page 48: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Three

First Query Solution (Second Way): select sprtele_pidm, sprtele_area_codefrom sprtelewhere (sprtele_area_code = ‘608’

or (sprtele_area_code = ‘414’)

Page 49: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Three

Second Query: Find the pidms and area codes of all the

people who have area codes that are 608 or 414 and the local phone number ends with a ’2’.

Do in two different ways.

Page 50: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Three

Second Query Solution (First Way): select sprtele_pidm, sprtele_area_code,

sprtele_phone_numberfrom sprtelewhere sprtele_area_code in (‘608’, ‘414’)

and sprtele_phone_number like ‘%2’

Page 51: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Three

Second Query Solution (Second Way; First Equivalent):

select sprtele_pidm, sprtele_area_code,

sprtele_phone_numberfrom sprtelewhere ((sprtele_area_code =‘608’)

or (sprtele_area_code = ‘414’))and (sprtele_phone_number like ‘%2’)

Page 52: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Three

Second Query Solution (Second Way; Second Equivalent):

select sprtele_pidm, sprtele_area_code,

sprtele_phone_numberfrom sprtelewhere (sprtele_area_code =‘608’

or sprtele_area_code = ‘414’)and sprtele_phone_number like ‘%2’

Page 53: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Three

Second Query Solution (Second Way; Third Equivalent):

select sprtele_pidm, sprtele_area_code,

sprtele_phone_numberfrom sprtelewhere sprtele_area_code =‘608’

or sprtele_area_code = ‘414’and sprtele_phone_number like ‘%2’

Page 54: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Three

Is this statement equivalent to the second way?

select sprtele_pidm, sprtele_area_code,

sprtele_phone_numberfrom sprtelewhere sprtele_area_code =‘608’

and sprtele_phone_number like ‘%2’or sprtele_area_code = ‘414’

Page 55: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Three

No. It is equivalent to select sprtele_pidm, sprtele_area_code,

sprtele_phone_numberfrom sprtelewhere (sprtele_area_code =‘608’

and sprtele_phone_number like ‘%2’)or sprtele_area_code = ‘414’

Page 56: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Three

Third Query: Find the pidms and area codes of all the

people who have area codes that greater than or equal

to 600 and less than or equal to 700.

Page 57: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Three

Third Query Solution: select sprtele_pidm, sprtele_area_code,

from sprtelewhere sprtele_area_code between ‘600’

and ‘700’

Page 58: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Aliases Aliases (franz)(franz)

There are two types of aliases used most frequently:

Column alias Table alias

Page 59: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

COLUMN Alias COLUMN Alias (franz)(franz)

SUM(SALES)/12 monthly_sales

In this example, the total sales were divided by 12 to derive the monthly sales amount. The alias “monthly_sales” is understandable and could be easily referenced.

Page 60: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

TABLE Alias TABLE Alias (franz)(franz)

The table alias is placed directly after the table name in the FROM clause.

This is convenient when you want to obtain information from two separate tables (the technical term is 'perform joins').

The advantage of using a table alias when performing joins is readily apparent when we talk about joins later.

Page 61: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

TABLE Alias TABLE Alias (franz)(franz)

Before we get into joins, though, let's look at the syntax for both the column and table aliases:

SELECT "table_alias"."column_name1" "column_alias" FROM "table_name" "table_alias“

Page 62: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Using an example from ‘spriden’, if we were creating a quick ad-hoc query that would only run this one time, to select last names starting with “S”, “J”, or “F”, we might save ourselves some typing time by using the alias syntax as follows:

SELECT s.spriden_last_name lnFROM spriden sWHERE ln like ‘S%’ or

ln like ‘J%’ orln like ‘F%’

An Alias ExampleAn Alias Example (franz)(franz)

Page 63: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

ORDER BY Clause ORDER BY Clause (franz)(franz)

SELECT "column_name" FROM "table_name" [WHERE "condition"]ORDER BY "column_name" [ASC, DESC]

The [ ] means that the WHERE statement is optional. However, if a WHERE clause exists, it comes before the ORDER BY clause.

ASC means that the results will be shown in ascending order, and DESC means that the results will be shown in descending order. If neither is specified, the default is ASC.

Page 64: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

ORDER BY Clause ORDER BY Clause (franz)(franz)

SELECT "column_name" FROM "table_name" [WHERE "condition"]ORDER BY "column_name" [ASC, DESC]

It is possible to order by more than one column. In this case, the ORDER BY clause above becomes: ORDER BY "column_name1" [ASC, DESC], "column_name2" [ASC, DESC]

Page 65: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

AGGREGATE FUNCTIONS AGGREGATE FUNCTIONS (franz)(franz)

Aggregate functions allow you to create a single value from the rows in a result set.

Arithmetic functions: AVG COUNT MAX MIN SUM

Page 66: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

An example aggregate function syntax using the COUNT function is:

SELECT COUNT("column_name") FROM "table_name“

COUNT and DISTINCT can be used together in a statement to fetch the number of distinct entries in a table.

AGGREGATE FUNCTIONSAGGREGATE FUNCTIONS (franz)(franz)

Page 67: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

For example, if we want to find out the number of distinct address types for Banner, we'd type:

SELECT COUNT(DISTINCT stvatyp_code) FROM stvatyp

The result would look like this:

AGGREGATE FUNCTIONSAGGREGATE FUNCTIONS (franz)(franz)

Page 68: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Warning!if the SELECT clause uses an aggregate function, then it must use only aggregate functions unless the query has a GROUP BY clause

Why? Aggregate function => single value other operands in a SELECT clause (e.g. columns) may

have multiple values => conflict

AGGREGATE FUNCTIONSAGGREGATE FUNCTIONS

Page 69: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Four

Objectives: Develop competence with aggregate

functions Steps:

First Query Second Query Third Query

Page 70: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Four

First Query: Find the largest pidm in spriden for

someone whose currently active and whose first name is ‘Mark’.

Label that column ‘Largest.’

Page 71: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Four

First Query Solution:

select max(spriden_pidm) as Largestfrom spridenwhere spriden_change_ind is null

and spriden_first_name = ‘Mark’

Page 72: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Four

Second Query: Find how many telephone numbers

have a 919 area code. Label that column ‘919 Numbers.’ Use the sprtele table.

Page 73: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Four

Second Query Solution:

select count(*) as “919 Numbers”from sprtelewhere sprtele_area_code = ‘919’

Page 74: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Four

Third Query: Find how many telephone number have

a 919 area code. Label that column ‘919 numbers.’ In the same query, find the smallest

pidm of a person with a 919 area code. Label that column ‘Smallest.’ Hint: use the sprtele table.

Page 75: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Four

Third Query Solution:

select count(*) as “919 Numbers”,min(sprtele_pidm) Smallest

from sprtelewhere sprtele_area_code = ‘919’

Page 76: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

GROUP BY Clause GROUP BY Clause (franz)(franz)

SELECT "column_name1", SUM("column_name2") FROM "table_name" GROUP BY "column_name1"

The GROUP BY keyword is used when we are selecting multiple columns from a table (or tables) and at least one arithmetic operator appears in the SELECT statement.

When that happens, we need to GROUP BY all the other selected columns -- i.e., all columns except the one(s) operated on by the arithmetic operator.

Page 77: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Task: calculate the number of people in our records – by Task: calculate the number of people in our records – by gender and hair code (franz)gender and hair code (franz)

SELECT spbpers_sex, spbpers_hair_code, COUNT(*)FROM spbpersGROUP BY spbpers_sex, spbpers_hair_code

Page 78: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

The HAVING Clause The HAVING Clause (franz)(franz)

SELECT "column_name1", AGGREGATE_FNC("column_name2") FROM "table_name" WHERE “conditions”

GROUP BY "column_name1" HAVING (arithmetic function condition)

Page 79: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

The HAVING ClauseThe HAVING Clause

This is complicated!How do we keep it all straight?

Answer: SQL is said to be non-procedural, but the order of execution of the clauses is

procedural the order of execution of the expressions

within a clause (e.g. WHERE and HAVING) is non-procedural

Page 80: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

The HAVING ClauseThe HAVING Clause

5) SELECT "column_name1", AGGREGATE_FNC("column_name2") 1) FROM "table_name" 2) WHERE “conditions”

3) GROUP BY "column_name1" 4) HAVING (arithmetic function

condition)

Order of execution of the clauses

Page 81: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

The HAVING ClauseThe HAVING Clause

Step 2)The WHERE clause elimination of rows is done on the individual rows BEFORE the GROUP BY and HAVING clauses.

Step 3) The grouping of the remaining rows into groups by the GROUP BY clause is done before the HAVING clause.

Page 82: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

The HAVING ClauseThe HAVING Clause

Step 4) The HAVING clause eliminates groups that do not meet the HAVING conditions.

Step 5) The projection onto columns and application of aggregate functions in the SELECT clause is done last.

Page 83: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Task: Task: For each state count the records in the address table where For each state count the records in the address table where the address type is “MA” (home/permanent mailing). the address type is “MA” (home/permanent mailing). Display the counts only for the states that have ten or more Display the counts only for the states that have ten or more such addresses.such addresses.

SELECT spraddr_stat_code, COUNT(*)FROM spraddrWHERE spraddr_atyp_code = ‘MA’GROUP BY spraddr_stat_codeHAVING COUNT(*) > 9

A HAVING Example A HAVING Example (franz)(franz)

Page 84: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

A subset of the result would look something like this:

A HAVING Example (cont.) A HAVING Example (cont.) (franz)(franz)

Page 85: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Five

Objectives: Develop competence with the GROUP BY and

HAVING clauses

Steps: First Query Second Query

Page 86: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Five

First Query:

For each area codeFor each area code find the largest phone numberfind the largest phone number

Page 87: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Five

First Query Solution:

select sprtele_phone_area, max(sprtele_phone_number)

from sprtelegroup by sprtele_phone_area

Page 88: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Five

Second Query: Count the number of records in the telephone Count the number of records in the telephone

table for each area code. table for each area code. However, only consider area codes from 600 However, only consider area codes from 600

through 800 and through 800 and Only consider area codes where the number Only consider area codes where the number

of records is at least five.of records is at least five.

Page 89: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Laboratory Five

Second Query Solution:

select sprtele_phone_area, count(*)select sprtele_phone_area, count(*)

from sprtelefrom sprtele

where sprtele_area_code between ‘600’ and where sprtele_area_code between ‘600’ and ‘800’‘800’

group by sprtele_phone_areagroup by sprtele_phone_area

having count(*) > 5having count(*) > 5

Page 90: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Joins

Outline Why multiple tables? Inner Joins Lab 6: Outer Joins Lab 7:

Page 91: Banner and the SQL Select Statement: Part Two (Single Table Selects) Mark Holliday Department of Mathematics and Computer Science Western Carolina University

Multiple Connected Select Statements

Outline Set Operators Lab 8: Subqueries

Use directly: FROM clause Use as a set: new operators Use as a single value: aggregate functions

Lab 9: A Query Development Methodology