26
Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

Embed Size (px)

Citation preview

Page 1: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

Inner Join vs. Outer Join

Information Retrieval from Relational Databases

1

Page 2: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

Query Languages

• Relational Algebra• Three main operators: Select, Project, Join• Provides the conceptual basis for SQL and QBE

• Structured Query Language (SQL)• The user enters commands according to a pre-defined

syntax to retrieve desired data.

• Query By Example (QBE)• The user starts with a sample of the table(s) columns

and marks the fields he or she wants to include in the answer.

• Defaults are available for summarizing and manipulating the data.

2

Page 3: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

SQL (Structured Query Language)

• Each query statement follows the same structure:SELECT attribute name(s)FROM table name(s)WHERE criteria is met;

3

Page 4: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

Relational Algebra

• Select• includes only certain rows from a database

table in its “answer”. • Project

• includes only certain columns from a database table in its “answer”

• Join• combines two or more database tables on the

basis of one or more common attributes

4

Page 5: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

Join Types

• Inner join(default in access)• includes only the records from both tables that

have the exact same values in the fields that are joined

• I.e.,

• Outer join• includes all records from one table, and matches

those records from the other table for which values in the joined fields are equal

• I.e.,

5

Left Outer Join Right Outer Join

Page 6: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

Example Tables

6

DepartmentDepartmentID NameD1 Executive ManagementD2 AccountingD3 Information SystemsD4 Operations

EmployeeEmplD SocialSec# LastName FirstName Street Address Pay rate Telephone DeptIDE1 123345678 Adams Anita 144 Apple St. $10.00 555-1234 D4E2 234456789 Boston Benjamin 255 Banana Rd. $12.00 555-2345 D2E3 345567890 Crabb Charlie 366 Cherry Ave. $14.00 555-3456 D2E4 456678901 Davis Deborah 477 Dip Dr. $32.00 555-4567 D1E5 567789101 Engler Edward 588 Eggplant St. $11.00 555-5678 D4E6 678891012 Folkert Fawn 699 Fruity Ave. $23.00 555-6789 D3

CourseTakenEmplID CourseID DateTaken

E3 AC1 May 1-2E3 AC2 June 24-28E4 AC1 Oct 14-15E6 IS1 June 24-28E6 IS2 July 8-12

TrainingCourse CourseID Description Length

AC1 Accounting Fundamentals 2 daysAC2 Chart of Accounts 5 daysIS1 Basic Information Systems 5 daysIS2 Database Design 5 daysMD100 ERP Systems 10 days

Page 7: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

Relationship View

7

Page 8: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

Query to List ALL Employees and Description of any Training Courses Taken ( Inner Join)

8

Page 9: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

Example Tables

9

DepartmentDepartmentID NameD1 Executive ManagementD2 AccountingD3 Information SystemsD4 Operations

EmployeeEmplD SocialSec# LastName FirstName Street Address Pay rate Telephone DeptIDE1 123345678 Adams Anita 144 Apple St. $10.00 555-1234 D4E2 234456789 Boston Benjamin 255 Banana Rd. $12.00 555-2345 D2E3 345567890 Crabb Charlie 366 Cherry Ave. $14.00 555-3456 D2E4 456678901 Davis Deborah 477 Dip Dr. $32.00 555-4567 D1E5 567789101 Engler Edward 588 Eggplant St. $11.00 555-5678 D4E6 678891012 Folkert Fawn 699 Fruity Ave. $23.00 555-6789 D3

CourseTakenEmplID CourseID DateTaken

E3 AC1 May 1-2E3 AC2 June 24-28E4 AC1 Oct 14-15E6 IS1 June 24-28E6 IS2 July 8-12

TrainingCourse CourseID Description Length

AC1 Accounting Fundamentals 2 daysAC2 Chart of Accounts 5 daysIS1 Basic Information Systems 5 daysIS2 Database Design 5 daysMD100 ERP Systems 10 days

Note: We have 6 employees

Page 10: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

Does this This Give Us What we Need?

10

LastName FirstName CourseID DescriptionCrabb Charlie AC1 Accounting FundamentalsCrabb Charlie AC2 Chart of AccountsFolkert Fawn IS1 Basic Information SystemsFolkert Fawn IS2 Database DesignDavis Deborah AC1 Accounting Fundamentals

The above based upon the default join which is the inner join and will not give us

employees that have not taken training classes

SQL code created by access for QBE on previous slide

Page 11: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

Using Outer Joins

11

Note change

Page 12: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

12

LastName FirstName CourseID DescriptionAdams AnitaBoston BenjaminCrabb Charlie AC1 Accounting FundamentalsCrabb Charlie AC2 Chart of AccountsDavis Deborah AC1 Accounting FundamentalsEngler EdwardFolkert Fawn IS1 Basic Information SystemsFolkert Fawn IS2 Database Design

SQL code created by access for QBE on previous slide

Page 13: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

The Revenue Cycle of M&M

13

M&M sells its agleclaps to customers through a network of company salespeople. Each type of agleclap is bought from a particular vendor and is given an initial list price. Each salesperson services a separate group of customers and is allowed to offer them various discounts from list to induce sales. Each sale can include one or more types of agleclaps and can be paid for in any one of three ways: (1) immediately in cash, (2) on the 15th of the following month, or (3) over the course of six months. When cash is received, a cashier deposits it into a company bank account. Sales are signaled by invoices; cash receipts by remittance advices

Page 14: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

INVENTORY Sale-line-item

EMPLOYEECATEGORY

SALE

CASH

EMPLOYEE

CASHRECEIPT

CUSTOMER

Sale-payment

SALEInsideparty

SALE-OutsideParty

Services

CROutsideParty

Cash-Inflow

CRInsideParty

Kindof

M N

N

N

M

N

N

1

N1 N

N

1

1

N

1

1

(1, 1)

(1, 1)

(1, 1)

(0, N) (1, N)

(0, N)

(0, N)

(0, N) (1, 1) (1, 1) (0, N)

(0, N)

(0, N)

(0, N)

(0, 1)

(0, N)

(1, 1)

AG

LE

CLA

PP

RIM

AR

Y

VE

ND

OR

DE

SC

RIP

TIO

N

QO

H

LIS

T P

RIC

E

QU

AN

TIT

Y-S

OLD

AC

TU

AL-P

RIC

E-O

F-

EA

CH

-AG

LE

CLA

P

INV

OIC

E #

SA

LE

-DA

TE

SA

LE

-AM

OU

NT

BA

NK

-A

CC

OU

NT #

BA

NK

AC

CO

UN

T-

BA

LA

NC

E

AC

CO

UN

T-

TYP

E

RE

MIT

TA

NC

E-

AD

VIC

E #

CA

SH

-RE

CE

IPT-

AM

OU

NT

RE

CE

IPT-D

ATE

EMPLOYEE-CATEGORY

NUMBER-OF-EMPLOYEES

MEDICAL-PLAN

SALESPERSON QUARTERLY SALES

SALESPERSONCOMMISSION

RATEFIDELITY BOND

RATING

EM

PLO

YE

E #

MO

NTH

LY-

PA

Y

NA

ME

HIG

HE

ST-D

EG

RE

E

M & M Revenue CycleREA Model

(0, N)

1

CUSTOMER #

CUSTOMER-NAME

A/R-AMOUNT

AMOUNT-OF-RECEIPT-APPLIED-

TO-SALE

EMPLOYEE

Page 15: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

Relationship View

15

Page 16: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

Example Tables (Incomplete Enterprise Database)from Dunn & McCarthy (2004) working paper

16

Inventory-Sale Stockflow Inventory ItemID

Sale Number

Quantity

Actual Price

A-4 S-1 2 600 A-1 S-1 3 2,000 A-6 S-2 2 5,000 A-1 S-3 1 2,000 A-5 S-3 2 4,000 A-3 S-3 6 1,000 A-6 S-4 2 5,000 A-2 S-5 2 3,000 A-4 S-5 2 300 A-6 S-5 2 5,000 A-2 S-6 10 3,500 A-6 S-7 2 7,000 A-5 S-7 3 3,000

Sale Sale# Amount Date Cust# SalesRep# S-1 7,200 1 July C-1 E-12 S-2 10,000 21 July C-2 E-10 S-3 16,000 22 July C-5 E-10 S-4 10,000 26 July C-2 E-10 S-5 16,600 31 July C-5 E-10 S-6 35,000 15 Aug C-3 E-10 S-7 23,000 21 Aug C-4 E-99

Sale-CashRecDuality Sale# RA# Applied S-2 RA-1 1,666 S-4 RA-2 10,000 S-1 RA-3 7,200 S-3 RA-4 16,000 S-5 RA-4 16,600 S-2 RA-5 1,666

Customer Customer# Name A/R Amt SP# C-1 Bill E-12 C-2 Mick E-10 C-3 Keith E-10 C-4 Charlie E-99 C-5 Ron E-10

Cash Receipt Remittance Advice#

Amount

Bank Account#

Date

Customer Number

Cashier Number

RA-1 1,666 BA-6 25 July C-2 E-39 RA-2 10,000 BA-7 26 July C-2 E-39 RA-3 7,200 BA-7 15 Aug C-1 E-39 RA-4 32,600 BA-7 15 Aug C-5 E-39 RA-5 1,666 BA-6 25 Aug C-2 E-39

Cash Account# Type Bank Balance BA-6 Checking Boston5 BA-7 Checking Shawmut BA-8 Draft Shawmut 75,000 BA-9 Checking MassNat 0

Salesperson Employee Number

Quarterly Sales $

Comm rate

E-12 .12 E-10 .10 E-99 .10 E-78 0 .15

Page 17: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

SQL and Relational Algebra Inner Join vs. Outer Join

• Find all details of all sales and the cash receipt number and amount applied of any cash receipts related to those sales• Data we need for this example is in 2 tables

• Summary sales totals have been stored in sales table• If summary figure not stored, you would need also need the

sale-inventory relationship table to calculate sales totals• Note: there have been no cash receipts related to sales

transaction 6 & 7.

17

Page 18: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

Relational Algebra Inner Join in QBE Details of all sales, related cash receipts

18

Page 19: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

Relational Algebra Inner Join in QBE Details of all sales, related cash receipts

19

Page 20: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

Default join is inner Join

20

If you double click on the join you will see the join properties box.Item 1, the inner join is the default

Page 21: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

Inner Join Results• Sales transactions 6 & 7 are not listed since there

was no cash receipts associated with them.• Does this create a potential problem??

21

SELECT Sale.SaleNumber, Sale.Amount, Sale.Date, Sale.CustomerNumber, [Sale-CashRecDuality].RemittanceAdviceNumber, [Sale-CashRecDuality].Applied FROM Sale INNER JOIN [Sale-CashRecDuality] ON Sale.SaleNumber = [Sale-CashRecDuality].SaleNumber;

Page 22: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

Relational Algebra Outer Join in QBE Details of all sales, related cash receipts

22

Double-click on the join line

Page 23: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

Relational Algebra Outer Join in QBE

Details of all sales, related cash receipts

23

Click OK

Click on appropriate join type

Page 24: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

24

Relational Algebra Outer Join in QBE Details of all sales, related cash receipts

Note change in Join line

Page 25: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

Relational Algebra Outer Join in QBE Details of all sales, related cash receipts

25

Result

Page 26: Inner Join vs. Outer Join Information Retrieval from Relational Databases 1

Joins Compared

26

Outer Join

Inner Join