Top Banner
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 2-1 David M. Kroenke’s Chapter Two: Introduction to Structured Query Language Part Two Database Processing: Fundamentals, Design, and Implementation
40

dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

Apr 28, 2018

Download

Documents

truongthuan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-1

David M. Kroenke’s

Chapter Two:

Introduction to

Structured Query Language

Part Two

Database Processing:Fundamentals, Design, and Implementation

Page 2: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-2

Using MS Access

Page 3: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-3

Using MS Access (Continued)

Page 4: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-4

Using MS Access (Continued)

Page 5: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-5

Using MS Access - Results

Page 6: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-6

Using MS SQL Server

[SQL Query Analyzer]

Page 7: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-7

Using Oracle

[SQL*Plus]

Page 8: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-8

Using Oracle

[Quest Software’s TOAD]

Page 9: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-9

Using MySQL

[MySQL Command Line Client]

Page 10: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-10

Using MySQL

[MySQL Query Browser]

Page 11: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-11

Sorting the Results: ORDER BY

SELECT *

FROM ORDER_ITEM

ORDER BY OrderNumber, Price;

Page 12: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-12

Sort Order:

Ascending and DescendingSELECT *

FROM ORDER_ITEM

ORDER BY Price DESC, OrderNumber ASC;

NOTE: The default sort order is ASC – does not have to be specified.

Page 13: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-13

WHERE Clause Options: AND

SELECT *

FROM SKU_DATA

WHERE Department = 'Water Sports'

AND Buyer = 'Nancy Meyers';

Page 14: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-14

WHERE Clause Options: OR

SELECT *

FROM SKU_DATA

WHERE Department = 'Camping'

OR Department = 'Climbing';

Page 15: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-15

WHERE Clause Options:- IN

SELECT *

FROM SKU_DATA

WHERE Buyer IN ('Nancy Meyers',

'Cindy Lo', 'Jerry Martin');

Page 16: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-16

WHERE Clause Options: NOT IN

SELECT *

FROM SKU_DATA

WHERE Buyer NOT IN ('Nancy Meyers',

'Cindy Lo', 'Jerry Martin');

Page 17: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-17

WHERE Clause Options:

Ranges with BETWEEN

SELECT *

FROM ORDER_ITEM

WHERE ExtendedPrice

BETWEEN 100 AND 200;

Page 18: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-18

WHERE Clause Options:

Ranges with Math Symbols

SELECT *

FROM ORDER_ITEM

WHERE ExtendedPrice >= 100

AND ExtendedPrice <= 200;

Page 19: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-19

WHERE Clause Options:

LIKE and Wildcards

• The SQL keyword LIKE can be combined with wildcard symbols:

– SQL 92 Standard (SQL Server, Oracle, etc.):

• _ = Exactly one character

• % = Any set of one or more characters

– MS Access (based on MS DOS)

• ? = Exactly one character

• * = Any set of one or more characters

Page 20: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-20

WHERE Clause Options:

LIKE and Wildcards (Continued)

SELECT *

FROM SKU_DATA

WHERE Buyer LIKE 'Pete%';

Page 21: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-21

WHERE Clause Options:

LIKE and Wildcards (Continued)SELECT *

FROM SKU_DATA

WHERE SKU_Description LIKE '%Tent%';

Page 22: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-22

WHERE Clause Options:

LIKE and Wildcards

SELECT *

FROM SKU_DATA

WHERE SKU LIKE '%2__';

Page 23: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-23

SQL Built-in Functions

• There are five SQL Built-in Functions:

– COUNT

– SUM

– AVG

– MIN

– MAX

Page 24: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-24

SQL Built-in Functions (Continued)

SELECT SUM (ExtendedPrice)

AS Order3000Sum

FROM ORDER_ITEM

WHERE OrderNumber = 3000;

Page 25: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-25

SQL Built-in Functions (Continued)

SELECT SUM (ExtendedPrice) AS OrderItemSum,

AVG (ExtendedPrice) AS OrderItemAvg,

MIN (ExtendedPrice) AS OrderItemMin,

MAX (ExtendedPrice) AS OrderItemMax

FROM ORDER_ITEM;

Page 26: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-26

SQL Built-in Functions (Continued)

SELECT COUNT(*) AS NumRows

FROM ORDER_ITEM;

Page 27: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-27

SQL Built-in Functions (Continued)

SELECT COUNT

(DISTINCT Department)

AS DeptCount

FROM SKU_DATA;

Page 28: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-28

Arithmetic in SELECT Statements

SELECT Quantity * Price AS EP,

ExtendedPrice

FROM ORDER_ITEM;

Page 29: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-29

String Functions in SELECT

Statements

SELECT DISTINCT RTRIM (Buyer)

+ ' in ' + RTRIM (Department)

AS Sponsor

FROM SKU_DATA;

Page 30: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-30

The SQL keyword GROUP BY

SELECT Department, Buyer,

COUNT(*) AS

Dept_Buyer_SKU_Count

FROM SKU_DATA

GROUP BY Department, Buyer;

Page 31: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-31

The SQL keyword GROUP BY

(Continued)

• In general, place WHERE before GROUP BY. Some DBMS products do not require that placement, but to be safe, always put WHERE before GROUP BY.

• The HAVING operator restricts the groups that are presented in the result.

• There is an ambiguity in statements that include both WHERE and HAVING clauses. The results can vary, so to eliminate this ambiguity SQL always applies WHERE before HAVING.

Page 32: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-32

The SQL keyword GROUP BY

(Continued)

SELECT Department, COUNT(*) AS

Dept_SKU_Count

FROM SKU_DATA

WHERE SKU <> 302000

GROUP BY Department

ORDER BY Dept_SKU_Count;

Page 33: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-33

The SQL keyword GROUP BY

(Continued)SELECT Department, COUNT(*) AS

Dept_SKU_Count

FROM SKU_DATA

WHERE SKU <> 302000

GROUP BY Department

HAVING COUNT (*) > 1

ORDER BY Dept_SKU_Count;

Page 34: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-34

Querying Multiple Tables: Subqueries

SELECT SUM (ExtendedPrice) AS Revenue

FROM ORDER_ITEM

WHERE SKU IN

(SELECT SKU

FROM SKU_DATA

WHERE Department = 'Water Sports');

Note: The second SELECT statement is a subquery.

Page 35: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-35

Querying Multiple Tables:

Subqueries (Continued)SELECT Buyer

FROM SKU_DATA

WHERE SKU IN

(SELECT SKU

FROM ORDER_ITEM

WHERE OrderNumber IN

(SELECT OrderNumber

FROM RETAIL_ORDER

WHERE OrderMonth = 'January'

AND OrderYear = 2004));

Page 36: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-36

Querying Multiple Tables:

JoinsSELECT Buyer, ExtendedPrice

FROM SKU_DATA, ORDER_ITEM

WHERE SKU_DATA.SKU = ORDER_ITEM.SKU;

Page 37: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-37

Querying Multiple Tables:

Joins (Continued)SELECT Buyer, SUM(ExtendedPrice)

AS BuyerRevenue

FROM SKU_DATA, ORDER_ITEM

WHERE SKU_DATA.SKU = ORDER_ITEM.SKU

GROUP BY Buyer

ORDER BY BuyerRevenue DESC;

Page 38: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-38

Querying Multiple Tables:

Joins (Continued)SELECT Buyer, ExtendedPrice, OrderMonth

FROM SKU_DATA, ORDER_ITEM, RETAIL_ORDER

WHERE SKU_DATA.SKU = ORDER_ITEM.SKU

AND ORDER_ITEM.OrderNumber =

RETAIL_ORDER.OrderNumber;

Page 39: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-39

Subqueries versus Joins

• Subqueries and joins both process multiple tables.

• A subquery can only be used to retrieve data from the top table.

• A join can be used to obtain data from any number of tables, including the “top table” of the subquery.

• In Chapter 7, we will study the correlated subquery. That kind of subquery can do work that is not possible with joins.

Page 40: dbp10 ch02 02 - cis.csuohio.educis.csuohio.edu/~sschung/cis430/dbp10_ch02_02.pdfMicrosoft PowerPoint - dbp10_ch02_02 [Compatibility Mode] Author: Sunnie Created Date: 1/11/2015 4:09:16

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall

2-40

David M. Kroenke’s

Database Processing Fundamentals, Design, and Implementation

(10th Edition)

End of Presentation:Chapter Two Part Two