You are on page 1of 79

David M. Kroenke and David J.

Auer
Database Processing:
Fundamentals, Design, and Implementation

Chapter Two:
Introduction to
Structured Query
ING. HERNN QUITO
Language
UNIVERSIDAD DE CUENCA 2017 - 2017
Chapter Objectives
To understand the use of extracted data sets in business
intelligence (BI) systems
To understand the use of ad-hoc queries in business
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, 13th Edition 2-2


2014 Pearson Education, Inc.
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, NOT, BETWEEN, LIKE, and IN keywords
To create SQL queries that use the SQL built-in
functions of SUM, COUNT, MIN, MAX, and AVG with
and without the use of a GROUP BY clause

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


2014 Pearson Education, Inc.
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, 13th Edition 2-4


2014 Pearson Education, Inc.
Business Intelligence (BI) Systems
Business intelligence (BI) systems are
information systems that assist managers
and other professionals:
Assessment
Analysis
Planning
Control

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


2014 Pearson Education, Inc.
Ad-Hoc Queries
Ad-hoc queries:
Questions that can be answered using
database data
Example: How many customers in Portland,
Oregon, bought our green baseball cap?
Created by the user as needed, instead of
programmed into an application
Common in business

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


2014 Pearson Education, Inc.
Components of a Data Warehouse

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


2014 Pearson Education, Inc.
Structured Query Language
Structured Query Language (SQL) was
developed by the IBM Corporation in the late
1970s.
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 concepts.

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


2014 Pearson Education, Inc.
SQL As a Data Sublanguage
SQL is not a full featured programming
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.
SQL programming is a critical skill.
KROENKE AND AUER - DATABASE PROCESSING, 13th Edition 2-9
2014 Pearson Education, Inc.
SQL DDL, DML, SQL/PSM and
SQL TCL
SQL statements can be divided into three
categories:
Data definition language (DDL) statements
Used for creating tables, relationships, and other
structures

Data manipulation language (DML)


statements
Used for queries and data modification

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


2014 Pearson Education, Inc.
SQL DDL, DML, SQL/PSM and
SQL TCL
SQL/Persistent Stored Modules (SQL/PSM)
statements
Add procedural programming capabilities
Variables
Control-of-flow statements

Transaction control language (TCL)


statements
Used to mark transaction boundaries and control
transaction behavior

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


2014 Pearson Education, Inc.
SQL DCL
Data control language (DCL) statements
Used to grant (or revoke) database permissions to
(from) users and groups

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


2014 Pearson Education, Inc.
Cape Codd Outdoor Sports
Cape Codd Outdoor Sports is a fictitious
company based on an actual outdoor retail
equipment vendor.
Cape Codd Outdoor Sports:
Has 15 retail stores in the United States and
Canada.
Has an online Internet store.
Has a (postal) mail order department.
All retail sales are recorded in an Oracle
Database 11g database.
KROENKE AND AUER - DATABASE PROCESSING, 13th Edition 2-13
2014 Pearson Education, Inc.
Cape Codd Retail Sales Structure

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


2014 Pearson Education, Inc.
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, only an
extraction of retail sales data.
The data is extracted by the IS department from the
operational database into a separate, off-line
database for use by the marketing department.
Three tables are used: RETAIL_ORDER,
ORDER_ITEM, and SKU_DATA (SKU = Stock Keeping
Unit).
The extracted data is converted as necessary:
Into a different DBMSMicrosoft SQL Server
Into different columnsOrderDate becomes OrderMonth and
OrderYear.

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


2014 Pearson Education, Inc.
Extracted
Retail
Sales Data
Format

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


2014 Pearson Education, Inc.
Retail Sales Extract Tables
[in Microsoft Access 2010]

20/03/2017
Tarea:
Reporte de
resumen,
interpretado

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


2014 Pearson Education, Inc.
The SQL SELECT Statement
The fundamental framework for an SQL
query 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, 13th Edition 2-18


2014 Pearson Education, Inc.
Specific Columns on One Table
SELECT Department, Buyer
FROM SKU_DATA;

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


2014 Pearson Education, Inc.
Specifying Column Order
SELECT Buyer, Department
FROM SKU_DATA;

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


2014 Pearson Education, Inc.
The DISTINCT Keyword
SELECT DISTINCT Buyer, Department
FROM SKU_DATA;

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


2014 Pearson Education, Inc.
Selecting All Columns:
The Asterisk (*) Wildcard Character
SELECT *
FROM SKU_DATA;

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


2014 Pearson Education, Inc.
Specific Rows from One Table
SELECT *
FROM SKU_DATA
WHERE Department = 'Water Sports';
NOTE: SQL wants a plain ASCII single quote: ' NOT !

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


2014 Pearson Education, Inc.
Specific Columns and Rows from
One Table
SELECT SKU_Description, Buyer
FROM SKU_DATA
WHERE Department = 'Climbing';

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


2014 Pearson Education, Inc.
Sorting the ResultsORDER BY
SELECT *
FROM ORDER_ITEM
ORDER BY OrderNumber, Price;

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


2014 Pearson Education, Inc.
Sort Order:
Ascending and Descending
SELECT *
FROM ORDER_ITEM
ORDER BY Price DESC, OrderNumber ASC;
NOTE: The default sort order is ASCdoes not have to be specified.

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


2014 Pearson Education, Inc.
WHERE Clause OptionsAND
SELECT *
FROM SKU_DATA
WHERE Department = 'Water Sports'
AND Buyer = 'Nancy Meyers';

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


2014 Pearson Education, Inc.
WHERE Clause OptionsOR
SELECT *
FROM SKU_DATA
WHERE Department = 'Camping'
OR Department = 'Climbing';

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


2014 Pearson Education, Inc.
SQL Comparison Operators

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


2014 Pearson Education, Inc.
WHERE Clause OptionsIN
SELECT *
FROM SKU_DATA
WHERE Buyer IN ('Nancy Meyers',
'Cindy Lo', 'Jerry Martin');

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


2014 Pearson Education, Inc.
WHERE Clause OptionsNOT IN
SELECT *
FROM SKU_DATA
WHERE Buyer NOT IN ('Nancy Meyers',
'Cindy Lo', 'Jerry Martin');

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


2014 Pearson Education, Inc.
WHERE Clause Options
Ranges with BETWEEN
SELECT *
FROM ORDER_ITEM
WHERE ExtendedPrice
BETWEEN 100 AND 200;

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


2014 Pearson Education, Inc.
WHERE Clause Options
Ranges with Math Symbols
SELECT *
FROM ORDER_ITEM
WHERE ExtendedPrice >= 100
AND ExtendedPrice <= 200;

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


2014 Pearson Education, Inc.
WHERE Clause Options
LIKE and Wildcards I
The SQL keyword LIKE can be combined
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 set of one or more characters

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


2014 Pearson Education, Inc.
WHERE Clause Options
LIKE and Wildcards II
SELECT *
FROM SKU_DATA
WHERE Buyer LIKE 'Pete%';

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


2014 Pearson Education, Inc.
WHERE Clause Options
LIKE and Wildcards III
SELECT *
FROM SKU_DATA
WHERE Buyer LIKE '%Tent%';

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


2014 Pearson Education, Inc.
WHERE Clause Options
LIKE and Wildcards IV
SELECT *
FROM SKU_DATA
WHERE SKU LIKE '%2__';

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


2014 Pearson Education, Inc.
Problems
Write an SQL statement to show a unique SKU and
SKU_Description for all products having an SKU description starting
with 'Half-Dome'.
Write an SQL statement to show a unique SKU and
SKU_Description for all products having a description that includes
the word 'Climb'.
Write an SQL statement to show a unique SKU and
SKU_Description for all products having a 'd' in the third position
from the left in SKU_Description.

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


2014 Pearson Education, Inc.
SQL Built-In Functions I
There are five SQL built-in functions:
COUNT
SUM
AVG
MIN
MAX

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


2014 Pearson Education, Inc.
SQL Built-In Functions II
SELECT SUM(ExtendedPrice)
AS Order3000Sum
FROM ORDER_ITEM
WHERE OrderNumber = 3000;

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


2014 Pearson Education, Inc.
SQL Built-In Functions III
SELECT SUM(ExtendedPrice) AS OrderItemSum,
AVG(ExtendedPrice) AS OrderItemAvg,
MIN(ExtendedPrice) AS OrderItemMin,
MAX(ExtendedPrice) AS OrderItemMax
FROM ORDER_ITEM;

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


2014 Pearson Education, Inc.
SQL Built-In Functions IV
SELECT COUNT(*) AS NumberOfRows
FROM ORDER_ITEM;

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


2014 Pearson Education, Inc.
SQL Built-In Functions V
SELECT COUNT
(DISTINCT Department)
AS DeptCount
FROM SKU_DATA;

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


2014 Pearson Education, Inc.
Arithmetic in SELECT Statements
SELECT Quantity * Price AS EP,
ExtendedPrice
FROM ORDER_ITEM;

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


2014 Pearson Education, Inc.
String Functions in SELECT
Statements
SELECT DISTINCT RTRIM (Buyer)
+ ' in ' + RTRIM (Department)
AS Sponsor
FROM SKU_DATA;

NOTE: This SQL statement uses SQL Server 2012 syntaxother DBMS
products use different concatenation and character string operators.
KROENKE AND AUER - DATABASE PROCESSING, 13th Edition 2-45
2014 Pearson Education, Inc.
The SQL Keyword GROUP BY I
SELECT Department, Buyer,
COUNT(*) AS
Dept_Buyer_SKU_Count
FROM SKU_DATA
GROUP BY Department, Buyer;

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


2014 Pearson Education, Inc.
The SQL Keyword GROUP BY II
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.

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


2014 Pearson Education, Inc.
The SQL Keyword GROUP BY III
SELECT Department, COUNT(*) AS
Dept_SKU_Count
FROM SKU_DATA
WHERE SKU <> 302000
ANSI-89 SQL:
GROUP BY Department Group or order by:
count(*)
ORDER BY Dept_SKU_Count;

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


2014 Pearson Education, Inc.
The SQL Keyword GROUP BY IV
SELECT Department, COUNT(*) AS
Dept_SKU_Count
FROM SKU_DATA
WHERE SKU <> 302000
GROUP BY Department ANSI-89 SQL:
Group or order by:
HAVING COUNT (*) > 1 count(*)

ORDER BY Dept_SKU_Count;

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


2014 Pearson Education, Inc.
Problems
1. Write an SQL statement that uses all of the SQL built-in functions on the
QuantityOnHand column. Include meaningful column names in the result.
2. Explain the difference between the SQL built-in functions COUNT and SUM.
3. Write an SQL statement to display the WarehouseID and the sum of
QuantityOnHand, grouped by WarehouseID. Name the sum TotalItemsOnHand and
display the results in descending order of TotalItemsOnHand.
4. Write an SQL statement to display the WarehouseID and the sum of
QuantityOnHand, grouped by WarehouseID. Omit all SKU items that have 3 or more
items on hand from the sum, and name the sum TotalItemsOnHandLT3 and display
the results in descending order of TotalItemsOnHandLT3.
5. Write an SQL statement to display the WarehouseID and the sum of QuantityOnHand
grouped by WarehouseID. Omit all SKU items that have 3 or more items on hand
from the sum, and name the sum TotalItemsOnHandLT3. Show the WarehouseID
only for warehouses having fewer than 2 SKUs in their TotalItemsOnHandLT3.
Display the results in descending order of TotalItemsOnHandLT3.
6. In your answer to Review Question 5, was the WHERE clause or the HAVING clause
applied first? Why?
7. Write an SQL statement to display the SKU, SKU_Description, WarehouseID,
WarehouseCity, and WarehouseState for all items stored in the Atlanta, Bangor, or
Chicago warehouse. Do not use the IN keyword.
KROENKE AND AUER - DATABASE PROCESSING, 13th Edition 2-50
2014 Pearson Education, Inc.
Problems
1. Write an SQL statement to display the SKU, SKU_Description,
WarehouseID, WarehouseCity, and WarehouseState for all items stored in
the Atlanta, Bangor, or Chicago warehouse. Use the IN keyword.
2. Write an SQL statement to display the SKU, SKU_Description,
WarehouseID, WarehouseCity, and WarehouseState of all items not stored
in the Atlanta, Bangor, or Chicago warehouse. Do not use the NOT IN
keyword.
3. Write an SQL statement to display the SKU, SKU_Description,
WarehouseID, WarehouseCity, and WarehouseState of all items not stored
in the Atlanta, Bangor, or Chicago warehouse. Use the NOT IN keyword.
4. Write an SQL statement to produce a single column called ItemLocation
that combines the SKU_Description, the phrase is located in, and
WarehouseCity. Do not be concerned with removing leading or trailing
blanks.

27/03/2017
Ejercicios. Tarea
KROENKE AND AUER - DATABASE PROCESSING, 13th Edition 2-51
2014 Pearson Education, Inc.
Querying Multiple Tables:
Subqueries I
Goal: To determine the water sports items revenue.
1. Get the SKU of water sport items
SELECT SKU
FROM SKU_DATA
WHERE Department = 'Water Sports

2. Get the sum


SELECT SUM(ExtendedPrice) AS WaterSportsRevenue
FROM ORDER_ITEM
WHERE SKU IN (100100, 100200, 101100, 101200);

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


2014 Pearson Education, Inc.
SQL subquery
An SQL subquery is an SQL query statement
used to determine a set of values that are
provided (or returned) to the SQL query (the top
level query) that used the subquery.
A subquery is often described as a nested query
or a query within a query
Still function like a single table query
only the columns of the top level query can be
displayed in the query results

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


2014 Pearson Education, Inc.
Querying Multiple Tables:
Subqueries I
Goal: To determine the water sports items revenue.

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.

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


2014 Pearson Education, Inc.
Querying Multiple Tables:
Subqueries II
SELECT Buyer Goal: a) we want to know the names of the
FROM SKU_DATA buyers who manage any product
purchased in
WHERE SKU IN January 2013
(SELECT SKU b) We want just one time each name
FROM ORDER_ITEM
WHERE OrderNumber IN
(SELECT OrderNumber
FROM RETAIL_ORDER
WHERE OrderMonth = 'January'
AND OrderYear = 2013));

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


2014 Pearson Education, Inc.
Querying Multiple Tables:
Subqueries III
SELECT Buyer, Department, COUNT(SKU) AS
Number_Of_SKU_Sold ANSI-89 SQL:
FROM SKU_DATA Group or order by: count(SKU)
WHERE SKU IN 31/03/2017 Tarea: ensayo IEEE sobre:
(SELECT SKU Herramientas de diseo de bases de
FROM ORDER_ITEM datos
WHERE OrderNumber IN Big Data en las telecomunicaciones
(SELECT OrderNumber
FROM RETAIL_ORDER
WHERE OrderMonth='January'
AND OrderYear=2015))
GROUP BY Buyer, Department
ORDER BY Number_Of_SKU_Sold;

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


2014 Pearson Education, Inc.
Querying Multiple Tables with
Joins
the SQL JOIN operator
is used to combine two or more tables by
concatenating (sticking together) the rows of
one table with the rows of another table.
explicit join
the JOIN operator is used as part of the SQL
statement syntax
implicit join
the JOIN operator itself does not appear in
the SQL statement
KROENKE AND AUER - DATABASE PROCESSING, 13th Edition 2-57
2014 Pearson Education, Inc.
CROSS JOIN
Cross Join
the result is Cartesian product of the rows in the tables, which
means that this statement will just stick every row of one table
together with every row of the second table
SELECT *
FROM RETAIL_ORDER, ORDER_ITEM;

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


2014 Pearson Education, Inc.
INNER JOIN
Inner Join (implicit)
to select only those rows for which the OrderNumber of
RETAIL_ORDER (primary key) matches the OrderNumber in
ORDER_ITEM (foreign key)
When the tables are joined using an inner join with an is equal to
condition, this join is called an equijoin

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


2014 Pearson Education, Inc.
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, 13th Edition 2-60


2014 Pearson Education, Inc.
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 ANSI-89 SQL:
Group or order by:
ORDER BY BuyerRevenue DESC; SUM(ExtendedPrice)

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


2014 Pearson Education, Inc.
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, 13th Edition 2-62


2014 Pearson Education, Inc.
JOIN ON Syntax
SELECT RETAIL_ORDER.OrderNumber, StoreNumber, OrderYear,
ORDER_ITEM.SKU, SKU_Description, Department
FROM RETAIL_ORDER JOIN ORDER_ITEM
ON RETAIL_ORDER.OrderNumber=ORDER_ITEM.OrderNumber
JOIN SKU_DATA
ON ORDER_ITEM.SKU=SKU_DATA.SKU
WHERE OrderYear = 2015
ORDER BY RETAIL_ORDER.OrderNumber, ORDER_ITEM.SKU;

ANSI-89
( entre cada grupo de join )
Usar INNER JOIN

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


2014 Pearson Education, Inc.
JOIN ON Syntax Results

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


2014 Pearson Education, Inc.
OUTER JOINS I

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


2014 Pearson Education, Inc.
OUTER JOINS II

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


2014 Pearson Education, Inc.
OUTER JOINS III

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


2014 Pearson Education, Inc.
OUTER JOINS IV

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


2014 Pearson Education, Inc.
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.
There is the correlated subquery. That kind of
subquery can do work that is not possible with
joins.

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


2014 Pearson Education, Inc.
SQL Set Operators
set theory
describe mathematical operations on sets, where a
set is defined as a group of distinct items

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


2014 Pearson Education, Inc.
SQL Set Operators

SELECT SKU, SKU_Description, Department


FROM CATALOG_SKU_2014
UNION
SELECT SKU, SKU_Description, Department
FROM CATALOG_SKU_2015;

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


2014 Pearson Education, Inc.
SQL Set Operators

SELECT SKU, SKU_Description, Department


FROM CATALOG_SKU_2014 No duplicaded
records
UNION Union All to get it
SELECT SKU, SKU_Description, Department
FROM CATALOG_SKU_2015;

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


2014 Pearson Education, Inc.
SQL Set Operators
SELECT SKU, SKU_Description, Department
FROM CATALOG_SKU_2014 ANSI-89
Inner join
INTERSECT
SELECT SKU, SKU_Description, Department
FROM CATALOG_SKU_2015;

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


2014 Pearson Education, Inc.
SQL Set Operators
SELECT SKU, SKU_Description, Department
FROM CATALOG_SKU_2014 Only ANSI-92
EXCEPT

SELECT SKU, SKU_Description, Department


FROM CATALOG_SKU_2015;

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


2014 Pearson Education, Inc.
Problems
1. Write an SQL statement to show the SKU, SKU_Description, and WarehouseID for
all items stored in a warehouse managed by 'Lucille Smith'. Use a subquery.
2. Write an SQL statement to show the SKU, SKU_Description, and WarehouseID for
all items stored in a warehouse managed by 'Lucille Smith'. Use a join, but do not use
JOIN ON syntax.
3. Write an SQL statement to show the SKU, SKU_Description, and WarehouseID for
all items stored in a warehouse managed by 'Lucille Smith'. Use a join using JOIN ON
syntax.
4. Write an SQL statement to show the WarehouseID and average QuantityOnHand of
all items stored in a warehouse managed by 'Lucille Smith'. Use a subquery.
5. Write an SQL statement to show the WarehouseID and average QuantityOnHand of
all items stored in a warehouse managed by 'Lucille Smith'. Use a join, but do not use
JOIN ON syntax.
6. Write an SQL statement to show the WarehouseID and average QuantityOnHand of
all items stored in a warehouse managed by 'Lucille Smith'. Use a join using JOIN ON
syntax.

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


2014 Pearson Education, Inc.
Problems
8 Write an SQL statement to show the WarehouseID, WarehouseCity,
WarehouseState, Manager, SKU, SKU_Description, and QuantityOnHand of all items
with a Manager of 'Lucille Smith'. Use a join using JOIN ON syntax.
9 Write an SQL statement to display the WarehouseID, the sum of QuantityOnOrder,
and the sum of QuantityOnHand, grouped by WarehouseID and QuantityOnOrder.
Name the sum of QuantityOnOrder as TotalItemsOnOrder and the sum of
QuantityOnHand as TotalItemsOnHand. Use only the INVENTORY table in your SQL
statement.
10 Explain why you cannot use a subquery in your answer to Review Question 8.
11 Explain how subqueries and joins differ.
12 Write an SQL statement to join WAREHOUSE and INVENTORY and include all
rows of WAREHOUSE in your answer, regardless of whether they have any
INVENTORY. Run this statement.

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


2014 Pearson Education, Inc.
Problems
13 Write an SQL statement to display the SKU, SKU_Description, and Department of
all SKUs that appear in either the Cape Codd 2013 catalog (either in the printed
catalog or on the Web site) or the Cape Codd 2014 catalog (either in the printed
catalog or on the Web site) or both.
14 Write an SQL statement to display the SKU, SKU_Description, and Department of
all SKUs that appear in either the Cape Codd 2013 catalog (only in the printed
catalog itelf) or the Cape Codd 2014 catalog (only in the printed catalog itself) or
both.
15 Write an SQL statement to display the SKU, SKU_Description, and Department of
all SKUs that appear in both the Cape Codd 2013 catalog (either in the printed
catalog or on the Web site) and the Cape Codd 2014 catalog (either in the printed
catalog or on the Web site).
16 Write an SQL statement to display the SKU, SKU_Description, and Department of
all SKUs that appear in both the Cape Codd 2013 catalog (only in the printed catalog
itself) and the Cape Codd 2014 catalog (only in the printed catalog itself) or both.
17 Write an SQL statement to display the SKU, SKU_Description, and Department of
all SKUs that appear in only the Cape Codd 2013 catalog (either in the printed
catalog or on the Web site) and not in the Cape Codd 2014 catalog (either in the
printed catalog or on the Web site).

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


2014 Pearson Education, Inc.
David Kroenke and David Auer
Database Processing
Fundamentals, Design, and Implementation
(13th Edition)

End of Presentation:
Chapter Two

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


2014 Pearson Education, Inc.
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.

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


2014 Pearson Education, Inc.

You might also like