You are on page 1of 68

David 

M. Kroenke
David M. Kroenke and David J. Auer
and David J. Auer
Database Processing:
Fundamentals, Design, and Implementation
d l d l

Chapter Two:
Introduction to   
Structured Query
Structured Query 
Language
g g
Modified & translated by Walter Chen
Dept. of Civil Engineering
National Taipei University of Technology
For classroom teaching purpose
For classroom teaching purpose

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-1


© 2012 Pearson Prentice Hall
Chapter Objectives
• To understand the use of extracted data sets in business
intelligence (BI) systems
• To
T understand
d t d th the use off ad-hoc
d h queries
i iin b
business
i
intelligence (BI) systems
• To understand the history and significance of Structured
Query Language (SQL)
• To understand the SQL SELECT/FROM/WHERE
framework as the basis for database queries
• To create SQL queries to retrieve data from a single
table

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-2


© 2012 Pearson Prentice Hall
Chapter Objectives
• To create SQL queries that use the SQL SELECT,
FROM, WHERE, ORDER BY, GROUP BY, and HAVING
clauses
• To create SQL queries that use the SQL DISTINCT,
AND OR
AND, OR, NOT
NOT, BETWEEN
BETWEEN, LIKE
LIKE, and IN keywords
• To create SQL queries that use the SQL built-in
functions
u c o so of SUM,
SU , COUNT,
COU , MIN,, MAX,, and
a d AVG G with
and without the use of a GROUP BY clause

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-3


© 2012 Pearson Prentice Hall
Chapter Objectives
• To create SQL queries that retrieve data from a single
table but restrict the data based upon data in another
table (subquery)
• To create SQL queries that retrieve data from multiple
tables using an SQL join operation

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-4


© 2012 Pearson Prentice Hall
Business Intelligence (BI) Systems
• Business intelligence (BI) systems are
y
information systems that assist managers
g
and other professionals:
– Assessment
– Analysis
– Planning
– Control

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-5


© 2012 Pearson Prentice Hall
Ad Hoc Queries
Ad-Hoc
• Ad-hoc queries:
–QQuestions that can be answered usingg
database data
– Example: “How
How many customers in Portland
Portland,
Oregon, bought our green baseball cap?”
– Created by the user as needed
needed, instead of
programmed into an application
– Common
C iin b
business
i

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-6


© 2012 Pearson Prentice Hall
Components of a Data Warehouse

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-7


© 2012 Pearson Prentice Hall
Structured Query Language
• Structured Query Language (SQL) was
developed by the IBM Corporation in the late
1970’s.
• SQL was endorsed as a U.S. national standard
by the American National Standards Institute
(ANSI) in 1992 [SQL-92].
• Newer versions exist, and they incorporate XML
and some object-oriented
object oriented concepts.

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-8


© 2012 Pearson Prentice Hall
SQL As a Data Sublanguage
• SQL iis nott a ffullll ffeatured
t d programming
i
language.
– C, C#, Java
• SQL is a data sublanguage for creating
and processing database data and
metadata.
• SQL is ubiquitous in enterprise-class
DBMS products
products.
• SQL programming is a critical skill.
KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-9
© 2012 Pearson Prentice Hall
SQL DDL,
DDL DML,
DML and SQL/PSM
• SQL statements
t t t can b
be di
divided
id d iinto
t th
three
categories:
– Data definition language (DDL) statements
• Used for creating tables, relationships, and other
structures
• Covered in Chapter 7
– Data
D t manipulation
i l ti language
l (DML)
statements
• Used
U d ffor queries
i and dddata
t modification
difi ti
• Covered in this chapter (Chapter 2)

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-10


© 2012 Pearson Prentice Hall
SQL DDL,
DDL DML,
DML and SQL/PSM
– SQL/Persistent
SQL/P i t t Stored
St d Modules
M d l (SQL/PSM)
statements
• Add procedural
d l programming
i capabilities
biliti
– Variables
– Co
Control-of-flow
t o o o state
statements
e ts
• Covered in Chapters:
– 7 (general introduction)
– 10 (SQL Server 2008 R2)
– 10A (Oracle Database 11g)
– 10B (MySQL 5 5.5)
5)

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-11


© 2012 Pearson Prentice Hall
Cape Codd Outdoor Sports
• C
Cape C Codddd O
Outdoor
td S
Sports t iis a fifictitious
titi
company based on an actual outdoor retail
equipment vendor
vendor.
• Cape Codd Outdoor Sports:
– Has
H 15 retail t il stores
t iin th
the UUnited
it d St States
t and
d
Canada.
– Has
H an online li IInternet
t t store.
t
– Has a (postal) mail order department.
• All retail sales are recorded in an Oracle
database.
KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-12
© 2012 Pearson Prentice Hall
Cape Codd Retail Sales Structure

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-13


© 2012 Pearson Prentice Hall
Cape Codd Retail Sales Data
Extraction
• The Cape Codd marketing department needs an
analysis of in-store sales.
• The entire database is not needed for this, onlyy an
extraction of retail sales data.
• The data is extracted by the IS department from the
operational database into a separate,
separate off-line
off line
database for use by the marketing department.
• Three tables are used: RETAIL_ORDER,
ORDER ITEM and SKU_DATA
ORDER_ITEM, SKU DATA (SKU = Stock Keeping
Unit).
• The extracted data is converted as necessary: y
– Into a different DBMS—Microsoft SQL Server
– Into different columns—OrderDate becomes OrderMonth and
OrderYear.

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-14


© 2012 Pearson Prentice Hall
Extracted
Retail
Sales Data
Format

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-15


© 2012 Pearson Prentice Hall
Retail Sales Extract Tables
[in Microsoft Access 2010]

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-16


© 2012 Pearson Prentice Hall
The SQL SELECT Statement
• The fundamental framework for an SQL
query
q y is the SQL SELECT statement.
– SELECT {ColumnName(s)}
– FROM {TableName(s)}
– WHERE {Condition(s)}
• All SQL statements end with a semi-colon
(;)
(;).

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-17


© 2012 Pearson Prentice Hall
Specific Columns on One Table
SELECT Department, Buyer
FROM SKU DATA;
SKU_DATA;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-18


© 2012 Pearson Prentice Hall
Specifying Column Order
SELECT Buyer, Department
FROM SKU DATA;
SKU_DATA;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-19


© 2012 Pearson Prentice Hall
The DISTINCT Keyword
SELECT DISTINCT Buyer, Department
FROM SKU_DATA;
_

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-20


© 2012 Pearson Prentice Hall
Selecting
g All Columns:
The Asterisk (*) Wildcard Character
SELECT *
FROM SKU DATA;
SKU_DATA;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-21


© 2012 Pearson Prentice Hall
Specific Rows from One Table
SELECT *
FROM SKU_DATA
WHERE D
Department
t t = 'Water
'W t S
Sports';
t '
NOTE: SQL wants a p
plain ASCII single
g q quote: ' NOT ‘ !

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-22


© 2012 Pearson Prentice Hall
Specific Columns and Rows from
One Table
SELECT SKU_Description, Buyer
FROM SKU_DATA
_
WHERE Department = 'Climbing';

Wrong results!

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-23


© 2012 Pearson Prentice Hall
Using Microsoft Access I

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-24


© 2012 Pearson Prentice Hall
Using Microsoft Access II

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-25


© 2012 Pearson Prentice Hall
Using Microsoft Access III

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-26


© 2012 Pearson Prentice Hall
Using Microsoft Access IV

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-27


© 2012 Pearson Prentice Hall
Using Microsoft Access V

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-28


© 2012 Pearson Prentice Hall
Using Microsoft Access
Access—Results
Results

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-29


© 2012 Pearson Prentice Hall
Using Microsoft Access
Saving the Query

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-30


© 2012 Pearson Prentice Hall
Using Microsoft Access
The Named and Saved Query

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-31


© 2012 Pearson Prentice Hall
Using
g Microsoft SQL Server 2008 R2
The Microsoft SQL Server Management Studio I

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-32


© 2012 Pearson Prentice Hall
Using
g Microsoft SQL Server 2008 R2
The Microsoft SQL Server Management Studio II

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-33


© 2012 Pearson Prentice Hall
Using
g Oracle Database 11g
g
SQL Developer I

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-34


© 2012 Pearson Prentice Hall
Using
g Oracle Database 11g
g
SQL Developer II

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-35


© 2012 Pearson Prentice Hall
Using
g MySQL
y 5.5
MySQL Workbench I

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-36


© 2012 Pearson Prentice Hall
Using
g MySQL
y 5.5
MySQL Workbench II

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-37


© 2012 Pearson Prentice Hall
Sorting the Results
Results—ORDER
ORDER BY
SELECT *
FROM ORDER ITEM
ORDER_ITEM
ORDER BY OrderNumber, Price;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-38


© 2012 Pearson Prentice Hall
Sort Order:
Ascending and Descending
SELECT
S C *
FROM ORDER_ITEM
ORDER BY Price DESC, OrderNumber ASC;
NOTE: The default sort order is ASC—does not have to be specified.

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-39


© 2012 Pearson Prentice Hall
WHERE Clause Options
Options—AND
AND
SELECT *
FROM SKU_DATA
_
WHERE Department = 'Water Sports'
AND Buyer = 'Nancy
Nancy Meyers
Meyers';
;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-40


© 2012 Pearson Prentice Hall
WHERE Clause Options
Options—OR
OR
SELECT *
FROM SKU_DATA
_
WHERE Department = 'Camping'
OR Department = 'Climbing';
Climbing ;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-41


© 2012 Pearson Prentice Hall
WHERE Clause Options
Options—IN
IN
SELECT *
FROM SKU_DATA
_
WHERE Buyer IN ('Nancy Meyers',
'Cindy
Cindy Lo'
Lo , 'Jerry
Jerry Martin');
Martin );

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-42


© 2012 Pearson Prentice Hall
WHERE Clause Options
Options—NOT
NOT IN
SELECT *
FROM SKU_DATA
_
WHERE Buyer NOT IN ('Nancy Meyers',
'Cindy
Cindy Lo'
Lo , 'Jerry
Jerry Martin');
Martin );

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-43


© 2012 Pearson Prentice Hall
WHERE Clause Options—
Ranges with BETWEEN
SELECT *
FROM ORDER_ITEM
_
WHERE ExtendedPrice
BETWEEN 100 AND 200;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-44


© 2012 Pearson Prentice Hall
WHERE Clause Options—
Ranges with Math Symbols
SELECT *
FROM ORDER_ITEM
_
WHERE ExtendedPrice >= 100
AND ExtendedPrice <= 200;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-45


© 2012 Pearson Prentice Hall
WHERE Clause Options—
LIKE and Wildcards I
• The SQL keyword LIKE can be combined
y
with wildcard symbols:
– SQL 92 Standard (SQL Server, MySQL, etc.):
• _ = exactly one character
• % = any set of one or more characters
– Microsoft Access (based on MS DOS)
•? = exactly one character
•* = any sett off one or more characters
h t

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-46


© 2012 Pearson Prentice Hall
WHERE Clause Options—
LIKE and Wildcards II
SELECT *
FROM SKU DATA
SKU_DATA
WHERE Buyer LIKE 'Pete%';

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-47


© 2012 Pearson Prentice Hall
WHERE Clause Options—
LIKE and Wildcards III
SELECT *
FROM SKU DATA
SKU_DATA
WHERE Buyer LIKE '%Tent%';

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-48


© 2012 Pearson Prentice Hall
WHERE Clause Options—
LIKE and Wildcards IV
SELECT *
FROM SKU DATA
SKU_DATA
WHERE SKU LIKE '%2__';

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-49


© 2012 Pearson Prentice Hall
SQL Built-In
Built In Functions I
• There are five SQL built-in functions:
– COUNT
– SUM
– AVG
– MIN
– MAX

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-50


© 2012 Pearson Prentice Hall
SQL Built-In
Built In Functions II
SELECT SUM(ExtendedPrice)
AS Order3000Sum
FROM ORDER_ITEM
WHERE OrderNumber = 3000;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-51


© 2012 Pearson Prentice Hall
SQL Built-In
Built In Functions III
SELECT SUM(ExtendedPrice) AS OrderItemSum
OrderItemSum,
AVG(ExtendedPrice) AS OrderItemAvg,
MIN(ExtendedPrice) AS OrderItemMin,
MAX(ExtendedPrice) AS OrderItemMax
FROM ORDER_ITEM;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-52


© 2012 Pearson Prentice Hall
SQL Built-In
Built In Functions IV
SELECT COUNT(*) AS NumberOfRows
FROM ORDER ITEM;
ORDER_ITEM;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-53


© 2012 Pearson Prentice Hall
SQL Built-In
Built In Functions V
SELECT COUNT
(DISTINCT Department)
AS DeptCount
FROM SKU_DATA;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-54


© 2012 Pearson Prentice Hall
Arithmetic in SELECT Statements
SELECT Quantity * Price AS EP,
ExtendedPrice
FROM ORDER_ITEM;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-55


© 2012 Pearson Prentice Hall
String Functions in SELECT
Statements
SELECT DISTINCT RTRIM (Buyer)
+ ' in ' + RTRIM (Department)
AS Sponsor
FROM SKU_DATA;
;

NOTE: This SQL statement uses SQL Server 2008 R2 syntax—other DBMS
products use different concatenation and character string operators
operators.
KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-56
© 2012 Pearson Prentice Hall
The SQL Keyword GROUP BY I
SELECT Department, Buyer,
COUNT(*) AS
Dept_Buyer_SKU_Count
FROM SKU DATA
SKU_DATA
GROUP BY Department, Buyer;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-57


© 2012 Pearson Prentice Hall
The SQL Keyword GROUP BY II
• IIn general,
l place
l WHERE before
b f GROUP BY BY.
Some DBMS products do not require that
placement; but to be safe
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
vary, so to eliminate this ambiguity SQL
always applies WHERE before HAVING.

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-58


© 2012 Pearson Prentice Hall
The SQL Keyword GROUP BY III
SELECT Department, COUNT(*) AS
Dept_SKU_Count
_ _
FROM SKU_DATA
WHERE SKU <> 302000
GROUP BY Department
ORDER BY Dept_SKU_Count;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-59


© 2012 Pearson Prentice Hall
The SQL Keyword GROUP BY IV
S
SELECT
C Department,
t t CO
COUNT(*)
(*) AS
S
Dept_SKU_Count
FROM SKU_DATA
WHERE SKU <> 302000
GROUP BY Department
HAVING COUNT (
(*)) > 1
ORDER BY Dept_SKU_Count;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-60


© 2012 Pearson Prentice Hall
Querying Multiple Tables:
Subqueries I
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.


subquery

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-61


© 2012 Pearson Prentice Hall
Querying Multiple Tables:
Subqueries II
SELECT B
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 = 2011));

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-62


© 2012 Pearson Prentice Hall
Querying Multiple Tables:
Joins I
SELECT Buyer, ExtendedPrice
FROM SKU_DATA, ORDER_ITEM
WHERE SKU_DATA.SKU = ORDER_ITEM.SKU;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-63


© 2012 Pearson Prentice Hall
Querying Multiple Tables:
Joins II
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;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-64


© 2012 Pearson Prentice Hall
Querying Multiple Tables:
Joins III
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;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-65


© 2012 Pearson Prentice Hall
Subqueries versus Joins
• S
Subqueries
b i and d joins
j i both
b th process multiple
lti l
tables.
• A subquery
b can onlyl bbe used d tto retrieve
ti d
data
t
from the top table.
• A join
j i can bbe usedd tto obtain
bt i ddatat ffrom any
number of tables, including the “top table” of the
subquery.
subquery
• In Chapter 7, we will study the correlated
subquery That kind of subquery can do work
subquery.
that is not possible with joins.

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-66


© 2012 Pearson Prentice Hall
David Kroenke and David Auer
Database Processing
Fundamentals, Design, and Implementation
Fundamentals Design and Implementation
(11th Edition)

End of Presentation:
Chapter Two
Chapter Two

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-67


© 2012 Pearson Prentice Hall
All rights reserved. No part of this publication may be reproduced, stored in a
retrieval system, or transmitted, in any form or by any means, electronic,
mechanical, photocopying, recording, or otherwise, without the prior written
permission of the publisher. Printed in the United States of America.

Copyright © 2012 Pearson Education, Inc.


Publishing as Prentice Hall

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2-68


© 2012 Pearson Prentice Hall

You might also like