Download as pdf or txt
Download as pdf or txt
You are on page 1of 64

MEEM 3040 Engineering Database and Systems Instructor: Dr. Oscar K.S. HUI (Room Y6626) Email: meoscar.hui@cityu.edu.

hk
KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall 1-1

DavidM.KroenkeandDavidJ.Auer DatabaseProcessing:
Fundamentals,Design,andImplementation

ChapterTwo:

Introductionto StructuredQuery Language


KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall 2-2

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, 12th Edition 2012 Pearson Prentice Hall 2-3

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, 12th Edition 2012 Pearson Prentice Hall

2-4

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 2012 Pearson Prentice Hall

2-5

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, 12th Edition 2012 Pearson Prentice Hall

2-6

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, 12th Edition 2012 Pearson Prentice Hall 2-7

Components of a Data Warehouse

Extract, transform and load (ETL)

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-8

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, 12th Edition 2012 Pearson Prentice Hall

2-9

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, 12th Edition 2012 Pearson Prentice Hall 2-10

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 Covered in Chapter 7

Data manipulation language (DML) statements


Used for queries and data modification Covered in this chapter (Chapter 2)
KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall 2-11

SQL DDL, DML, and SQL/PSM


SQL/Persistent Stored Modules (SQL/PSM) statements
Add procedural programming capabilities
Variables Control-of-flow statements

Covered in Chapters:
7 (general introduction) 10 (SQL Server 2008 R2) 10A (Oralce Database 11g) 10B (MySQL 5.5)

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-12

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.
KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall 2-13

Cape Codd Retail Sales Structure

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-14

Using Microsoft Access I

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-15

Using Microsoft Access II

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-16

Using Microsoft Access III

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-17

Using Microsoft Access IV

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-18

Using Microsoft Access V

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-19

Using Microsoft AccessResults

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-20

Using Microsoft Access


Saving the Query

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-21

Using Microsoft Access


The Named and Saved Query

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-22

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, 12th Edition 2012 Pearson Prentice Hall 2-23

Extracted Retail Sales Data Format

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-24

Retail Sales Extract Tables [in Microsoft Access 2010]

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-25

The SQL SELECT Statement


The fundamental framework for an SQL query is the SQL SELECT statement.
SELECT FROM WHERE {ColumnName(s)} {TableName(s)} {Condition(s)}

All SQL statements end with a semi-colon (;).

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-26

Specific Columns on One Table


SELECT Department, Buyer FROM SKU_DATA;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-27

Specifying Column Order


SELECT Buyer, Department FROM SKU_DATA;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-28

The DISTINCT Keyword


SELECT FROM DISTINCT Buyer, Department SKU_DATA;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-29

Selecting All Columns:


The Asterisk (*) Wildcard Character
SELECT * FROM SKU_DATA;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-30

Specific Rows from One Table


SELECT FROM WHERE * SKU_DATA Department = 'Water Sports';

NOTE: SQL wants a plain ASCII single quote: ' NOT !

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-31

Specific Columns and Rows from One Table


SELECT FROM WHERE SKU_Description, Buyer SKU_DATA Department = 'Climbing';

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-32

Sorting the ResultsORDER BY


SELECT FROM ORDER BY * ORDER_ITEM OrderNumber, Price;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-33

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, 12th Edition 2012 Pearson Prentice Hall

2-34

WHERE Clause OptionsAND


SELECT FROM WHERE AND * SKU_DATA Department = 'Water Sports' Buyer = 'Nancy Meyers';

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-35

WHERE Clause OptionsOR


SELECT FROM WHERE OR * SKU_DATA Department = 'Camping' Department = 'Climbing';

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-36

WHERE Clause OptionsIN


SELECT FROM WHERE * SKU_DATA Buyer IN ('Nancy Meyers', 'Cindy Lo', 'Jerry Martin');

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-37

WHERE Clause OptionsNOT IN


SELECT FROM WHERE * SKU_DATA Buyer NOT IN ('Nancy Meyers', 'Cindy Lo', 'Jerry Martin');

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-38

WHERE Clause Options Ranges with BETWEEN


SELECT FROM WHERE * ORDER_ITEM ExtendedPrice BETWEEN 100 AND 200;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-39

WHERE Clause Options Ranges with Math Symbols


SELECT FROM WHERE AND * ORDER_ITEM ExtendedPrice >= 100 ExtendedPrice <= 200;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-40

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, 12th Edition 2012 Pearson Prentice Hall

2-41

WHERE Clause Options LIKE and Wildcards II


SELECT * FROM SKU_DATA WHERE Buyer LIKE 'Pete%';

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-42

WHERE Clause Options LIKE and Wildcards III


SELECT * FROM SKU_DATA WHERE Buyer LIKE '%Tent%';

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-43

WHERE Clause Options LIKE and Wildcards IV


SELECT * FROM SKU_DATA WHERE SKU LIKE '%2__';

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-44

SQL Built-In Functions I


There are five SQL built-in functions:
COUNT SUM AVG MIN MAX

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-45

SQL Built-In Functions II


SELECT SUM(ExtendedPrice) AS Order3000Sum FROM ORDER_ITEM WHERE OrderNumber = 3000;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-46

SQL Built-In Functions III


SELECT SUM(ExtendedPrice) AVG(ExtendedPrice) MIN(ExtendedPrice) MAX(ExtendedPrice) ORDER_ITEM; AS AS AS AS OrderItemSum, OrderItemAvg, OrderItemMin, OrderItemMax

FROM

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-47

SQL Built-In Functions IV


SELECT COUNT(*) AS NumberOfRows FROM ORDER_ITEM;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-48

SQL Built-In Functions V


SELECT COUNT (DISTINCT Department) AS DeptCount FROM SKU_DATA;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-49

Arithmetic in SELECT Statements


SELECT Quantity * Price AS EP, ExtendedPrice FROM ORDER_ITEM;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-50

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, 12th Edition 2012 Pearson Prentice Hall

2-51

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, 12th Edition 2012 Pearson Prentice Hall 2-52

The SQL Keyword GROUP BY III


SELECT FROM WHERE GROUP BY ORDER BY Department, COUNT(*) AS Dept_SKU_Count SKU_DATA SKU <> 302000 Department Dept_SKU_Count;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-53

The SQL Keyword GROUP BY IV


SELECT FROM WHERE GROUP BY HAVING ORDER BY Department, COUNT(*) AS Dept_SKU_Count SKU_DATA SKU <> 302000 Department COUNT (*) > 1 Dept_SKU_Count;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-54

Querying Multiple Tables: Subqueries I


SELECT FROM WHERE SUM (ExtendedPrice) AS Revenue ORDER_ITEM SKU IN (SELECT SKU FROM SKU_DATA WHERE Department = 'Water Sports');

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-55

Querying Multiple Tables: Subqueries II


SELECT FROM WHERE Buyer SKU_DATA SKU IN (SELECT SKU FROM ORDER_ITEM WHERE OrderNumber (SELECT FROM WHERE AND

IN OrderNumber RETAIL_ORDER OrderMonth = 'January' OrderYear = 2011));

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-56

Querying Multiple Tables: Joins I


SELECT FROM WHERE Buyer, ExtendedPrice SKU_DATA, ORDER_ITEM SKU_DATA.SKU = ORDER_ITEM.SKU;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-57

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 2012 Pearson Prentice Hall

2-58

Querying Multiple Tables: Joins III


SELECT FROM WHERE AND Buyer, ExtendedPrice, OrderMonth SKU_DATA, ORDER_ITEM, RETAIL_ORDER SKU_DATA.SKU = ORDER_ITEM.SKU ORDER_ITEM.OrderNumber = RETAIL_ORDER.OrderNumber;

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-59

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.
KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall 2-60

Review
58) SQL is a ________. A) data sublanguage B) product of IBM research C) national standard D) combination that include a data definition language and a data manipulation language E) All of the above.
KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall 2-61

70) Given a table with the structure: EMPLOYEE (EmpNo, Name, Salary, HireDate), which of the following would find all employees whose name begins with the letter "S" using standard SQL?
A) SELECT * FROM EMPLOYEE WHERE Name IN ['S']; B) SELECT EmpNo FROM EMPLOYEE WHERE Name LIKE 'S';

C) SELECT * FROM Name WHERE EMPLOYEE LIKE 'S*'; D) SELECT * FROM EMPLOYEE WHERE Name LIKE 'S%'; E) None of the above.

KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall

2-62

89) In an SQL query, which SQL keyword is used to specify the column names to be used in a join? A) FROM B) HAVING C) JOIN D) SELECT E) WHERE
KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall 2-63

91) What is SQL? 92) Explain why it is important to learn SQL. 93) Briefly describe subqueries and joins. Explain when each is not an acceptable alternative for the other. 100) Distinguish between the HAVING clause and the WHERE clause.
KROENKE AND AUER - DATABASE PROCESSING, 12th Edition 2012 Pearson Prentice Hall 2-64

You might also like