0% found this document useful (0 votes)
13 views

Chapter 3 Structured Query Language (SQL) (2)

Chapter 3 of 'Database Processing: Fundamentals, Design, and Implementation' covers Structured Query Language (SQL), detailing its history, significance, and various components including DML and DDL. It provides objectives for creating SQL queries using SELECT, FROM, WHERE, and other clauses, as well as built-in functions and managing table structures. The chapter also discusses querying multiple tables through subqueries and joins, using a fictitious company, Cape Codd Outdoor Sports, as a case study.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Chapter 3 Structured Query Language (SQL) (2)

Chapter 3 of 'Database Processing: Fundamentals, Design, and Implementation' covers Structured Query Language (SQL), detailing its history, significance, and various components including DML and DDL. It provides objectives for creating SQL queries using SELECT, FROM, WHERE, and other clauses, as well as built-in functions and managing table structures. The chapter also discusses querying multiple tables through subqueries and joins, using a fictitious company, Cape Codd Outdoor Sports, as a case study.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 67

David M. Kroenke and David J.

Auer
Database Processing:
Fundamentals, Design, and Implementation

Chapter 3
Structured Query
Language
(SQL)
Chapter Objectives (DML)
• 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 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

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


© 2019 Pearson Education, Inc.
Chapter Objectives (DML)
• 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
• 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, 15th Edition 2-3


© 2019 Pearson Education, Inc.
Chapter Objectives (DDL)
• To create and manage table structures using CREATE,
ALTER and DROP
• To create and use SQL constraints
• To manage data using INSERT, UPDATE and DELETE
• To understand several uses for SQL views
• To use SQL statements to create and use views

KROENKE AND AUER - DATABASE PROCESSING, 15th Edition 7-4


© 2019 Pearson Education, Inc.
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 concepts.

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


© 2019 Pearson Education, Inc.
SQL DDL, DML, and SQL/PSM
• 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
– SQL/Persistent Stored Modules (SQL/PSM)
statements
• Add procedural programming capabilities
– Variables
– Control-of-flow statements

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


© 2019 Pearson Education, Inc.
SQL for 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, 15th Edition 2-7


© 2019 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, 15th Edition 2-8
© 2019 Pearson Education, Inc.
Cape Codd Retail Sales Data
Extraction
• The Cape Codd marketing department needs an
analysis of in-store sales.
• 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 DBMS—Microsoft SQL Server
– Into different columns—OrderDate becomes OrderMonth and
OrderYear.

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


© 2019 Pearson Education, Inc.
Extracted
Retail
Sales Data
Format

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


© 2019 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, 15th Edition 2-11


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

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


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

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


© 2019 Pearson Education, Inc.
The DISTINCT Keyword
Distinct keyword is used to eliminate duplicate.

SELECT DISTINCT Buyer, Department


FROM SKU_DATA;

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


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

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


© 2019 Pearson Education, Inc.
Specific Rows from One Table
SELECT *
FROM SKU_DATA
WHERE Department = 'Water Sports';

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


© 2019 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, 15th Edition 2-17


© 2019 Pearson Education, Inc.
Using Oracle Database 11g Release 2
SQL Developer I

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


© 2019 Pearson Education, Inc.
Sorting the Results—ORDER BY
SELECT *
FROM ORDER_ITEM
ORDER BY OrderNumber, Price;

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


© 2019 Pearson Education, Inc.
Sort Order:
Ascending and Descending
SELECT *
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, 15th Edition 2-20


© 2019 Pearson Education, Inc.
WHERE Clause Options—AND
SELECT *
FROM SKU_DATA
WHERE Department = 'Water Sports'
AND Buyer = 'Nancy Meyers';

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


© 2019 Pearson Education, Inc.
WHERE Clause Options—OR
SELECT *
FROM SKU_DATA
WHERE Department = 'Camping'
OR Department = 'Climbing';

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


© 2019 Pearson Education, Inc.
WHERE Clause Options—IN
SELECT *
FROM SKU_DATA
WHERE Buyer IN ('Nancy Meyers',
'Cindy Lo', 'Jerry
Martin');

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


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

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


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

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


© 2019 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, 15th Edition 2-26


© 2019 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, 15th Edition 2-27


© 2019 Pearson Education, Inc.
WHERE Clause Options—
LIKE and Wildcards II
SELECT *
FROM SKU_DATA
WHEREBuyer LIKE 'Pete%';

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


© 2019 Pearson Education, Inc.
WHERE Clause Options—
LIKE and Wildcards III
SELECT *
FROM SKU_DATA
WHERESKU_Description LIKE '%Tent
%';

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


© 2019 Pearson Education, Inc.
WHERE Clause Options—
LIKE and Wildcards IV
SELECT *
FROM SKU_DATA
WHERESKU LIKE '%2_ _';

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


© 2019 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, 15th Edition 2-31


© 2019 Pearson Education, Inc.
SQL Built-In Functions II
SELECT SUM(ExtendedPrice)
AS Order3000Sum
FROM ORDER_ITEM
WHEREOrderNumber = 3000;

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


© 2019 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, 15th Edition 2-33


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

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


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

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


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

table

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


© 2019 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 syntax—other DBMS
products use different concatenation and character string operators.
KROENKE AND AUER - DATABASE PROCESSING, 15th Edition 2-37
© 2019 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;

table

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


© 2019 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, 15th Edition 2-39


© 2019 Pearson Education, Inc.
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, 15th Edition 2-40


© 2019 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
HAVING COUNT (*) > 1
ORDER BY Dept_SKU_Count;

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


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

table

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


© 2019 Pearson Education, Inc.
Querying Multiple Tables:
Subqueries II
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 = 2013));

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


© 2019 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, 15th Edition 2-44


© 2019 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
ORDER BY BuyerRevenue DESC;

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


© 2019 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, 15th Edition 2-46


© 2019 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 = '2012'
ORDER BY RETAIL_ORDER.OrderNumber, ORDER_ITEM.SKU;

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


© 2019 Pearson Education, Inc.
JOIN ON Syntax Results

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


© 2019 Pearson Education, Inc.
OUTER JOINS I

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


© 2019 Pearson Education, Inc.
OUTER JOINS II

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


© 2019 Pearson Education, Inc.
OUTER JOINS III

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


© 2019 Pearson Education, Inc.
OUTER JOINS IV

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


© 2019 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.

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


© 2019 Pearson Education, Inc.
CREATE TABLE
• CREATE TABLE statement is used for creating
relations.
• Each column is described with three parts:
column name, data type, and optional
constraints.
• Example

KROENKE AND AUER - DATABASE PROCESSING, 15th Edition © 7-54


2019 Pearson Education, Inc.
Constraints
• Constraints can be defined within the CREATE
TABLE statement, or they can be added to the
table after it is created using the ALTER table
statement.
• Five types of constraints:
– PRIMARY KEY may not have null values
– UNIQUE may have null values
– NULL/NOT NULL
– FOREIGN KEY
– CHECK

KROENKE AND AUER - DATABASE PROCESSING, 15th Edition 7-55


© 2019 Pearson Education, Inc.
SQL for Constraints

KROENKE AND AUER - DATABASE PROCESSING, 15th Edition 7-56


© 2019 Pearson Education, Inc.
ALTER TABLE Statement
• ALTER TABLE statement changes table
structure, properties, or constraints after it has
been created.
• Example
ALTER TABLE ASSIGNMENT
ADD CONSTRAINT EmployeeFK
FOREIGN KEY (EmployeeNumber)
REFERENCES EMPLOYEE (EmployeeNumber)
ON UPDATE CASCADE
ON DELETE NO ACTION;

KROENKE AND AUER - DATABASE PROCESSING, 15th Edition 7-57


© 2019 Pearson Education, Inc.
Adding and Dropping Columns
• The following statement will add a column
named MyColumn to the CUSTOMER
table:
ALTER TABLE CUSTOMER ADD MyColumn Char(5) NULL;
• You can drop an existing column with the
statement:
ALTER TABLE CUSTOMER DROP COLUMN MyColumn;

KROENKE AND AUER - DATABASE PROCESSING, 15th Edition 7-58


© 2019 Pearson Education, Inc.
Adding and Dropping Constraints
• ALTER TABLE can be used to add a
constraint as follows:
ALTER TABLE CUSTOMER
ADD CONSTRAINT MyConstraint CHECK
(LastName NOT IN ('Robert-No-Pay'));
• ALTER TABLE can be used to drop a
constraint:
ALTER TABLE CUSTOMER
DROP CONSTRAINT MyConstraint;

KROENKE AND AUER - DATABASE PROCESSING, 15th Edition 7-59


© 2019 Pearson Education, Inc.
Removing Tables
• SQL DROP TABLE:
DROP TABLE TRANS;

• If there are constraints:


ALTER TABLE CUSTOMER_ARTIST_INT
DROP CONSTRAINT
Customer_Artist_Int_CustomerFK;
ALTER TABLE TRANS
DROP CONSTRAINT TransactionCustomerFK;
DROP TABLE CUSTOMER;

KROENKE AND AUER - DATABASE PROCESSING, 15th Edition 7-60


© 2019 Pearson Education, Inc.
Removing Data Only
• SQL TRUNCATE TABLE:
TRUNCATE TABLE TRANS;

• Cannot be used with a table that is


referenced by a foreign key
constraint.

KROENKE AND AUER - DATABASE PROCESSING, 15th Edition 7-61


© 2019 Pearson Education, Inc.
SQL DML—INSERT
• SQL INSERT statement:
INSERT INTO ARTIST (LastName, FirstName,
Nationality, DateOfBirth, DateDeceased)
VALUES
('Tamayo', ‘Rufino', 'Mexican', 1899, 1991);
• Bulk INSERT:
INSERT INTO ARTIST
(LastName, FirstName, Nationality, DateOfBirth)
SELECT LastName, FirstName, Nationality,
DateOfBirth
FROM IMPORTED_ARTIST;

KROENKE AND AUER - DATABASE PROCESSING, 15th Edition 7-62


© 2019 Pearson Education, Inc.
SQL DML—UPDATE
• SQL UPDATE statement:
UPDATE CUSTOMER
SET City = 'New York City'
WHERE CustomerID = 1000;
• Bulk UPDATE:
UPDATE CUSTOMER
SET AreaCode = '333'
WHERE City = 'Denver';

KROENKE AND AUER - DATABASE PROCESSING, 15th Edition 7-63


© 2019 Pearson Education, Inc.
SQL DML—DELETE
• SQL DELETE statement:
DELETE FROM CUSTOMER
WHERE CustomerID = 1000;
• If you omit the WHERE clause, you will
delete every row in the table.

KROENKE AND AUER - DATABASE PROCESSING, 15th Edition 7-64


© 2019 Pearson Education, Inc.
Using Aliases
• Use of aliases:
SELECT C.Name, A.Name
FROM CUSTOMER AS C
JOIN
CUSTOMER_ARTIST_INT AS CI
ON C.CustomerID = CI.CustomerID
JOIN ARTIST AS A
ON CI.ArtistID = A.ArtistID;
• DBMS products differ
CUSTOMER AS C versus CUSTOMER C

KROENKE AND AUER - DATABASE PROCESSING, 15th Edition 7-65


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

End of Presentation

KROENKE AND AUER - DATABASE PROCESSING, 15th Edition 7-66


© 2019 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, 15th Edition 7-67


© 2019 Pearson Education, Inc.

You might also like