54
Banner and the SQL Select Statement: Part Three (Joins) Mark Holliday Department of Mathematics and Computer Science Western Carolina University 4 November 2005 and 11 November 2005 (updated: 11 November 2005)

Banner and the SQL Select Statement: Part Three (Joins)

  • Upload
    presta

  • View
    67

  • Download
    0

Embed Size (px)

DESCRIPTION

Banner and the SQL Select Statement: Part Three (Joins). Mark Holliday Department of Mathematics and Computer Science Western Carolina University 4 November 2005 and 11 November 2005 (updated: 11 November 2005). Outline. The Goal The Concepts A First Example Single Table Selects - PowerPoint PPT Presentation

Citation preview

Page 1: Banner and the SQL Select Statement:  Part Three (Joins)

Banner and the SQL Select Statement: Part Three (Joins)

Mark HollidayDepartment of Mathematics and

Computer ScienceWestern Carolina University

4 November 2005 and 11 November 2005(updated: 11 November 2005)

Page 2: Banner and the SQL Select Statement:  Part Three (Joins)

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 Three (Joins)

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 Three (Joins)

Single Table Selects

Outline WHERE clause: single condition, multiple

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

Page 5: Banner and the SQL Select Statement:  Part Three (Joins)

Joins

Outline Why Multiple Tables? Inner Joins Lab 6: Outer joins Lab 7:

Page 6: Banner and the SQL Select Statement:  Part Three (Joins)

Why Multiple Tables? (franz)Why Multiple Tables? (franz)

One Table database: keeps track of all purchases at our store (known as the ‘flat file’)

Page 7: Banner and the SQL Select Statement:  Part Three (Joins)

Every time a new row is inserted into the table, all columns will be updated. This results in unnecessary "redundant data". For example, every time Wolfgang Schultz purchases something, the following rows will be inserted into the table:

Why Multiple Tables? Why Multiple Tables? (franz)(franz)

Page 8: Banner and the SQL Select Statement:  Part Three (Joins)

What happens if we ONE DAY find out Wolfgang Schultz’s last name is really spelled “S-h-o-o-l-t-z-e” instead of “S-c-h-u-l-t-z”? (franz)He has purchased LOTS of stuff from us.

And…. He has been making purchases at our store for over five years. In fact, he is our very BEST customer. His purchases provide for most of our gross sales.

But now… He says that he will take his business elsewhere if we don’t get this problem corrected!

And YOU have to get it corrected NOW!!!

Why Multiple Tables?Why Multiple Tables?

Page 9: Banner and the SQL Select Statement:  Part Three (Joins)

Hold that thought !Hold that thought !

This helps to exemplify WHY relational database table structures are so nice ...

Why Multiple Tables? Why Multiple Tables? (franz)(franz)

Page 10: Banner and the SQL Select Statement:  Part Three (Joins)

Original ‘flat file’ table:

Why Multiple Tables? Why Multiple Tables? (franz)(franz)

Page 11: Banner and the SQL Select Statement:  Part Three (Joins)

For this example, an ideal database would have two tables:

One for keeping track of customer information (customer_info)

And the other to keep track of what they purchase (purchases).

customer_info table:

purchases table:

Why Multiple Tables? Why Multiple Tables? (franz)(franz)

Page 12: Banner and the SQL Select Statement:  Part Three (Joins)

Our new tables: Our new tables: (franz)(franz)

The ‘id’ numberis the commonelement thatties the tablestogether.

Page 13: Banner and the SQL Select Statement:  Part Three (Joins)

After redesigning our data structure into two tables,

whenever a change for the name or address needs to be made to our repeating customer,

we update the “customer_info” table only.

Only the second table, “purchases”, needs to be updated when purchases are made.

Why Multiple Tables? (franz)Why Multiple Tables? (franz)

Page 14: Banner and the SQL Select Statement:  Part Three (Joins)

We've just eliminated the space-taking, time-consuming, useless redundant data of our flat file.

That is, we've just NORMALIZED this database!

This is the basis for relational database structure.

Why Multiple Tables? (franz)Why Multiple Tables? (franz)

Page 15: Banner and the SQL Select Statement:  Part Three (Joins)

Unfortunately, having multiple tables introduces a problem.

Problem: Information you need for a query is often in more than one table.

Example: Find the zip code of the person whose first name is “Ab” and last name is “Mazlan”. need the spraddr table for the zip code and need the spriden table for the name

Joins: IntroductionJoins: Introduction

Page 16: Banner and the SQL Select Statement:  Part Three (Joins)

Solution: Need a way to temporarily (just in the query) connect a row in one table with the row (or rows) in another table that “match” it.

Refined Solution: Use some type of JOIN operation

Joins: IntroductionJoins: Introduction

Page 17: Banner and the SQL Select Statement:  Part Three (Joins)

Problem: How do we find the “matching” rows in the other table?

In other words, how do we do a join?

Solution: 1. Conceptually, match each row in the left

table with every row in the right table.• take the Cartesian Product of the two tables

2. Only keep the row pairs that “match”.

Joins: IntroductionJoins: Introduction

Page 18: Banner and the SQL Select Statement:  Part Three (Joins)

Joins: Introduction (Step One of Join)Joins: Introduction (Step One of Join)

pidm last_name

1 “smith”2 “jones”

pidm zip1 201113 20311

pidm1 Last_name pidm2 zip1 “smith” 1 201111 “smith” 3 203112 “jones” 1 201112 “jones” 3 20311

Cartesian Product

--

Page 19: Banner and the SQL Select Statement:  Part Three (Joins)

Joins: Introduction (Step Two of Join)Joins: Introduction (Step Two of Join)

pidm1 Last_name pidm2 zip1 “smith” 1 201111 “smith” 3 203112 “jones” 1 201112 “jones” 3 20311

only keep rows that have pidm1 = pidm2pidm1 Last_name pidm2 zip1 “smith” 1 20111

Page 20: Banner and the SQL Select Statement:  Part Three (Joins)

Questions:1) When does a row from the left table

“match” a row from the right table?2) What to do with a row in one table

that does not match a row in the other table?

Joins: IntroductionJoins: Introduction

Page 21: Banner and the SQL Select Statement:  Part Three (Joins)

Question One: When does a row from the left table “match” a row from the right table?

Answer: The two rows match if the condition expression is TRUE.

The expression is a sequence of conditions connected by AND.

Each condition is a comparison of a left table column and a right table column

The comparison operator is equality.

Joins: IntroductionJoins: Introduction

Page 22: Banner and the SQL Select Statement:  Part Three (Joins)

Example condition expression just one condition

spriden.spriden_pidm = spraddr.spraddr_pidm

Joins: IntroductionJoins: Introduction

Page 23: Banner and the SQL Select Statement:  Part Three (Joins)

Good relational database design

=> the only common column in two tables is the key for each table

=> use the key of each table to get from a left table row to the matching right table row

• they have the same key value

Joins: IntroductionJoins: Introduction

Page 24: Banner and the SQL Select Statement:  Part Three (Joins)

Question Two: What to do with a row in one table that does not match a row in the other table?

ignore them: inner join don’t ignore them: outer join This section covers inner joins

Joins: IntroductionJoins: Introduction

Page 25: Banner and the SQL Select Statement:  Part Three (Joins)

An example using reserved words to indicate a join:

SELECT spriden_last_name, spriden_first_name, spraddr_city, spraddr_stat_code, spraddr_zip

FROM spriden INNER JOIN spraddrON spriden_pidm = spraddr_pidm

Implicitly, 1) take cartesian product of spriden and spraddr2) only keep the rows that meet the condition

INNER Join (franz)

Page 26: Banner and the SQL Select Statement:  Part Three (Joins)

Syntax for a join using reserved words

SELECT "list-of-columns“FROM table1 [“type” JOIN] table2ON “field matching”WHERE "search-condition(s)"

INNER Join (franz)

Page 27: Banner and the SQL Select Statement:  Part Three (Joins)

Table ‘spriden’:

Table ‘spraddr’:

INNER Join (franz)

Page 28: Banner and the SQL Select Statement:  Part Three (Joins)

SELECT spriden_last_name, spriden_first_name, spraddr_city, spraddr_stat_code, spraddr_zipFROM spriden INNER JOIN spraddrON spriden_pidm = spraddr_pidm

INNER Join (franz)

Page 29: Banner and the SQL Select Statement:  Part Three (Joins)

Task: Select from ‘spriden’ for the active record Task: Select from ‘spriden’ for the active record (spriden_change_ind is null). Pull the permanent mailing (spriden_change_ind is null). Pull the permanent mailing address types (‘MA’) in the US (spraddr_natn_code is null).address types (‘MA’) in the US (spraddr_natn_code is null).

This will pull a large amount of data.

Let’s create a smaller subset by picking zip codes on the northern East Coast (zip codes starting with ‘0’).

Let’s also order our output by the last name then first name.

INNER Join (franz)

Page 30: Banner and the SQL Select Statement:  Part Three (Joins)

To accomplish this we would take the previous SQL and make these additions:

SELECT spriden_last_name, spriden_first_name, spraddr_city, spraddr_stat_code,spraddr_zip

FROM spriden INNER JOIN spraddrON spriden_pidm = spraddr_pidmWHERE spriden_change_ind is nulland spraddr_atyp_code = 'MA'and spraddr_zip between '000%' and '1%'and spraddr_natn_code is nullORDER BY spriden_last_name, spriden_first_name

INNER Join (franz)

Page 31: Banner and the SQL Select Statement:  Part Three (Joins)

SELECT substr(spriden_last_name,1,12) || ', ' || substr(spriden_first_name,1,12), rtrim(spraddr_city) || ', ' || spraddr_stat_code || ' ' || spraddr_zip

FROM spriden INNER JOIN spraddrON spriden_pidm = spraddr_pidmWHERE spriden_change_ind is nulland spraddr_atyp_code = 'MA'and spraddr_zip between '000%' and '1%'and spraddr_natn_code is nullORDER BY spriden_last_name, spriden_first_name

INNER Join (franz)

Page 32: Banner and the SQL Select Statement:  Part Three (Joins)

Part of the Resultant Subset Would Be:

INNER Join (franz)

Page 33: Banner and the SQL Select Statement:  Part Three (Joins)

If we had left out the check for a null change indicator, we would have received duplicate rows of data. (We would get the Gary Abbot record with a null indicator, and also the one with an “I” indicator.)

SELECT substr(spriden_last_name,1,12) || ', ' || substr(spriden_first_name,1,12), rtrim(spraddr_city) || ', ' || spraddr_stat_code || ' ' || spraddr_zip

FROM spriden INNER JOIN spraddrON spriden_pidm = spraddr_pidmWHERE spraddr_atyp_code = 'MA'and spraddr_zip between '000%' and '1%'and spraddr_natn_code is null……spriden_change_ind is nullspriden_change_ind is nullORDER BY spriden_last_name, spriden_first_name

INNER Join (franz)

Page 34: Banner and the SQL Select Statement:  Part Three (Joins)

INNER Join (franz)

Page 35: Banner and the SQL Select Statement:  Part Three (Joins)

SELF JoinSELF JoinThe left table and the right table in an

inner join might be the same table.

This is called a SELF Join.

When would this be useful?

Page 36: Banner and the SQL Select Statement:  Part Three (Joins)

SELF Join (franz)SELF Join (franz)‘spiffy’ table:

Syntax:SELECT e.first_name EMPLOYEE_FIRST, e.last_name EMPLOYEE_LAST, m.first_name MANAGER_FIRST, m.last_name MANAGER_LASTFROM spiffy e INNER JOIN spiffy mON e.manager_id = m.employee_id

Page 37: Banner and the SQL Select Statement:  Part Three (Joins)

SELF JoinSELF Join

Previous Inner Join examples: instances of two different tables joining on the

same column in both tables (e.g. pidm)The Self Inner Join example: two instances of the same table joining on

different columns of the same table (e.g. manager_id and employee_id)

SELECT e.first_name EMPLOYEE_FIRST, e.last_name EMPLOYEE_LAST, m.first_name MANAGER_FIRST, m.last_name MANAGER_LASTFROM spiffy e INNER JOIN spiffy mON e.manager_id = m.employee_id

Page 38: Banner and the SQL Select Statement:  Part Three (Joins)

SELF Join (franz)SELF Join (franz)Result:

Page 39: Banner and the SQL Select Statement:  Part Three (Joins)

Laboratory Six Objectives:

Develop competence with inner joins

Steps: First Query

Page 40: Banner and the SQL Select Statement:  Part Three (Joins)

Laboratory Six Problem: Find the area code and phone

number for every one whose last name is “Holliday.”

Hint: Use the spriden and sprtele tables.

Page 41: Banner and the SQL Select Statement:  Part Three (Joins)

Solution One:SELECT sprtele_phone_area, sprtele_phone_number

FROM spriden INNER JOIN sprteleON spriden_pidm = sprtele_pidmWHERE spriden_last_name = ‘Holliday’

Solution Two:SELECT sprtele_phone_area, sprtele_phone_number FROM spriden, sprteleWHERE spriden_pidm = sprtele_pidm and

spriden_last_name = ‘Holliday’

Laboratory Six

Page 42: Banner and the SQL Select Statement:  Part Three (Joins)

OUTER Join (franz)OUTER Join (franz)

Notice that ‘spriden’ has records for Bagnall, Baker and Barksdale.

‘Spbpers’ has records for Bagnall and Barksdale, but not Baker.

Page 43: Banner and the SQL Select Statement:  Part Three (Joins)

OUTER Join (franz)OUTER Join (franz)

If we join the tables by an inner join on pidm, we would only receive rows for Bagnall and Barksdale.

To pick up all the records in ‘spriden’, and join it with the data in ‘spbpers’ we would use an outer join.

Page 44: Banner and the SQL Select Statement:  Part Three (Joins)

OUTER Join (franz)OUTER Join (franz)

OUTER Joins can be split into three types:OUTER Joins can be split into three types: LEFT outer join RIGHT outer join FULL outer join

“Left” or “Right” designates what your base table is for the join.

“Full” returns all rows in both tables.

Page 45: Banner and the SQL Select Statement:  Part Three (Joins)

SELECT spriden_last_name, spriden_first_name, spbpers_sex, spbpers_birth_date

FROM spriden LEFT OUTER JOIN spbpersON spriden_pidm = spbpers_pidmWHERE spriden_change_ind is nulland spriden_entity_ind = 'P'ORDER BY spriden_last_name, spriden_first_name

LEFT OUTER Join (franz)LEFT OUTER Join (franz)

Page 46: Banner and the SQL Select Statement:  Part Three (Joins)

LEFT OUTER Join (franz)LEFT OUTER Join (franz)

Result:

Page 47: Banner and the SQL Select Statement:  Part Three (Joins)

SELECT spriden_last_name, spriden_first_name, spbpers_sex, spbpers_birth_date

FROM spriden RIGHT OUTER JOIN spbpersON spriden_pidm = spbpers_pidmWHERE spriden_change_ind is nulland spriden_entity_ind = 'P'ORDER BY spriden_last_name, spriden_first_name

RIGHT OUTER Join (franz)RIGHT OUTER Join (franz)

Page 48: Banner and the SQL Select Statement:  Part Three (Joins)

Result:

RIGHT OUTER Join (franz)RIGHT OUTER Join (franz)

Page 49: Banner and the SQL Select Statement:  Part Three (Joins)

SELECT spriden_last_name, spriden_first_name, spbpers_sex, spbpers_birth_date

FROM spriden FULL OUTER JOIN spbpersON spriden_pidm = spbpers_pidmWHERE spriden_change_ind is nulland spriden_entity_ind = 'P'ORDER BY spriden_last_name, spriden_first_name

FULL OUTER Join (franz)FULL OUTER Join (franz)

Page 50: Banner and the SQL Select Statement:  Part Three (Joins)

Result: PIDM added to indicate which rows were selected.

FULL OUTER Join (franz)FULL OUTER Join (franz)

Page 51: Banner and the SQL Select Statement:  Part Three (Joins)

Laboratory Seven Objectives:

Develop competence with outer joins

Steps: Outer Join Query

Page 52: Banner and the SQL Select Statement:  Part Three (Joins)

JOIN Query (franz)JOIN Query (franz)Write a JOIN. Use either an “INNER”, “OUTER” or “FULL”.

Use tables SPRIDEN and SPRADDR.

Include both PIDMs, last name, first name, first address line, address type.

Join on PIDMSelect the Active name recordSelect entity indicator for a PersonSelect PIDMs less than 70,000Select last names that start with “Bea”

Order the result set by SPRIDEN PIDM, then SPRADDR PIDM

Page 53: Banner and the SQL Select Statement:  Part Three (Joins)

Answer (franz)Answer (franz)SELECT spriden_pidm, spraddr_pidm,

spriden_last_name, spriden_first_name, spraddr_atyp_code, spraddr_street_line1

FROM spriden LEFT OUTER JOIN spraddrON spriden_pidm = spraddr_pidmWHERE spriden_change_ind is nullAND spriden_entity_ind = 'P'AND spriden_pidm < 70000AND spriden_last_name like 'Bea%'ORDER BY spriden_pidm, spraddr_pidm

Page 54: Banner and the SQL Select Statement:  Part Three (Joins)

Multiple Connected Select Statements

Outline Union, intersection, minus 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