0% found this document useful (0 votes)
145 views93 pages

Chapter 2 PPT Part 1 Intro To SQL

Uploaded by

sekharreddy82p
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)
145 views93 pages

Chapter 2 PPT Part 1 Intro To SQL

Uploaded by

sekharreddy82p
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

Database Processing: Fundamentals,

Design, and Implementation


Fifteenth Edition

Chapter 2
Part 1
Introduction to Structured
Query Language

Slides in this presentation contain


hyperlinks. JAWS users should be able to
get a list of links by using INSERT+F7

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Learning Objectives (1 of 2)
2.1 To understand the use of extracted datasets in business intelligence (B I) systems
2.2 To understand the use of ad-hoc queries in business intelligence (B I) systems
2.3 To understand the history and significance of Structured Query Language (S QL)
2.4 To understand the SQL SELECT/FROM/WHERE framework as the basis for database
queries
2.5 To create SQL queries to retrieve data from a single table
2.6 To create SQL queries that use the S QL SELECT, FROM, WHERE, ORDER BY, GROUP
BY, and HAVING clauses
2.7 To create SQL queries that use the S QL DISTINCT, TOP, and TOP PERCENT keywords
2.8 To create SQL queries that use the S QL comparison operators, including BETWEEN,
LIKE, IN, and IS NULL

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Learning Objectives (2 of 2)
2.9 To create SQL queries that use the S QL logical operators, including AND, OR, and
NOT
2.10 To create SQL queries that use the S QL built-in aggregate functions of SUM,
COUNT, MIN, MAX, and A VG with and without the S QL GROUP BY clause
2.11 To create SQL queries that retrieve data from a single table while restricting the data
based upon data in another table (subquery)
2.12 To create SQL queries that retrieve data from multiple tables using the S QL join and
JOIN ON operations
2.13 To create SQL queries on recursive relationships
2.14 To create SQL queries that retrieve data from multiple tables using the S QL OUTER
JOIN operation
2.15 To create SQL queries that retrieve data from multiple tables using S QL set
operators UNION, INTERSECT, and EXCEPT

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Chapter 2 Part 1 Coverage
• This Microsoft PowerPoint Slide Show covers single table
queries and the first ten chapter objectives.

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Business Intelligence (BI) Systems
• Business intelligence (BI) systems are information
systems that assist managers and other professionals:
– Assessment
– Analysis
– Planning
– Control

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
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

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-1
Cape Codd Outdoor Sports

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Cape Codd Outdoor Sports Background
• 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 12c
Release 2 DBMS.

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-2
Cape Codd Retail Sales Data Extraction Process

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-3
Components of a Data Warehouse

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Cape Codd Outdoor Sports
Retail Sales and Catalog Context Data Extraction 1
• The Cape Codd marketing department needs an analysis
of:
– In-store sales
– Catalog content
• The entire database is not needed for this, only an
extraction of retail sales data and catalog content.
• The data is extracted by the IS department from the
operational database into a separate, offline database
for use by the marketing department.

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-4
Extracted Retail Sales Data Database Tables

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-5
Cape Codd Extracted Retail Sales Data Format (1 of 2)

Table Column Data Type


RETAIL_ORDER OrderNumber Integer
RETAIL_ORDER StoreNumber Integer
RETAIL_ORDER StoreZIP Character (9)
RETAIL_ORDER OrderMonth Character (12)
RETAIL_ORDER OrderYear Integer
RETAIL_ORDER OrderTotal Currency
RETAIL_ORDER OrderNumber Integer
ORDER_ITEM SKU Integer
ORDER_ITEM Quantity Integer
ORDER_ITEM Price Currency
ORDER_ITEM ExtendedPrice Currency

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-5
Cape Codd Extracted Retail Sales Data Format (2 of 2)

Table Column Data Type


SKU_DATA SKU Integer
SKU_DATA SKU_Description Character (35)
SKU_DATA Department Character (30)
SKU_DATA Buyer Character (35)
BUYER BuyerName Character (35)
BUYER Department Character (30)
BUYER Position Character (10)
BUYER Supervisor Character (35)
CATALOG_SKU_20## CatalogID Integer
CATALOG_SKU_20## SKU Integer
CATALOG_SKU_20## SKU_Description Character (35)
CATALOG_SKU_20## Department Character (30)
CATALOG_SKU_20## CatalogPage Integer
CATALOG_SKU_20## DateOnWebSite Date

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-6
Extracted Retail Sales Database (1 of 2)
(a) The Linked RETAIL_ORDER, ORDER_ITEM, S KU_DATA, and BUYER Tables

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-6
Extracted Retail Sales Database (2 of 2)
(b) The Non-Linked CATALOG_SKU_2017 and CATALOG_SKU_2018 Tables

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Cape Codd Database Available Online (1 of 2)
• Versions of the complete Cape Codd database are
available in the downloadable Student Files available at:
http://www.pearsonhighered.com/kroenke/
• These include versions for:
– Microsoft Access 2016
– Microsoft SQL Server 2017
– Oracle Database XE
– MySQL 5.7
• We recommend you actually run all material in a live
database!

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Cape Codd Database Available Online (2 of 2)
• To complete setting up the Cape Codd database, set the
referenced materials:
– For Microsoft SQL Server 2014:
▪ See Online Chapter 10A
– For Oracle Database 12c and Oracle Database X E:
▪ See Online Chapter 10B
– For MySQL 5.6
▪ See Online Chapter 10C
• Online chapters 10A, 10B, and 10C are available for download
at:
http://www.pearsonhighered.com/kroenke/

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Structured Query Language
• Structured Query Language (S Q L) was developed by
the I B M Corporation in the late 1970’s.
• S Q L was endorsed as a U.S. national standard by the
American National Standards Institute (ANSI) in 1992 [S Q
L-92].
• Newer versions exist, such as S Q L:2008, S Q L:2011, and
S Q L:2016 and they incorporate new features including X M
L, J S O N, and some object-oriented concepts. Some of
these features are discussed in this book.

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
SQL As a Data Sublanguage
• SQL is not a full-featured programming language.
– C, C#
• 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.

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
SQL Categories
• SQL statements can be divided into five categories:
– Data definition language (DDL)
– Data manipulation language (DML) statements
– SQL/Persistent Stored Modules (SQL/PSM)
statements
– Transaction control language (TCL) statements
– Data control language (DCL) statements

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
S QL DDL
• Data definition language (DDL) statements
– Used for creating tables, relationships, and other
structures
– Covered in Chapter 7

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
S QL DML
• Data manipulation language (DML) statements
– Used for:
▪ Queries – SQL SELECT statement
▪ Inserting data – SQL INSERT statement
▪ Modifying data – SQL UPDATE statement
▪ Deleting data – SQL DELETE statement
– Covered in this chapter (Chapter 2)

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
SQL SQL/PSM
• SQL/Persistent Stored Modules (SQL/PSM) statements
– Add procedural programming capabilities
▪ Variables
▪ Control-of-flow statements
– Covered in Chapters:
▪ 7 (general introduction)
▪ 10A (SQL Server 2014)
▪ 10B (Oracle Database)
▪ 10C (MySQL 5.6)

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
S QL T CL
• Transaction control language (TCL) statements
– Used to mark transaction boundaries and control
transaction behavior
– Covered in Chapters:
▪ 9 (general introduction)
▪ 10A (SQL Server 2017)
▪ 10B (Oracle Database XE)
▪ 10C (MySQL 5.7)

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
S QL DCL
• Data control language (DCL) statements
– Used to grant (or revoke) database permissions to
(from) users and groups
– Covered in Chapters:
▪ 9 (general introduction)
▪ 10A (SQL Server 2014)
▪ 10B (Oracle Database)
▪ 10C (MySQL 5.6)

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
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 semicolon(;).

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Specific Columns from a Single Table
SELECT S K U, S K U_Description, Department,
Buyer
FROM S K U_DATA;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Selecting All Columns:
The SQL Asterisk (*) Wildcard Character
SELECT *
FROM S K U_DATA;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
I
Specifying Column Order 1
SELECT Department, Buyer
FROM S K U_DATA;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
II
Specifying Column Order 2
SELECT Buyer, Department
FROM S K U_DATA;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-7
MS Access 2016 Options Object Designers Page
The SQL Server Compatible Syntax (ANSI 92) options control which version of S QL is
used in a Microsoft Access 2016 database. If you check the This database check box, you
will use SQL-92 syntax in the current database. Or you can check the Default for new
databases check box to make S QL-92 syntax the default for all new databases you create.

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-9
The Create Command Tab

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-10
The Show Table Dialog Box

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-11
The Query Tools Contextual Command Tab

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-12
The Query 1 Window in SQL View

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-13
The SQL Query

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-14
The SQL Query Results

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-15
The Save As Dialog Box

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-16
The Named and Saved Query

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-17
Running Query in SQL Server Mgmt Studio

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-18
Saving a Query as an SQL Script in Mgmt Studio

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-19
Running an SQL Query in Oracle SQL Developer

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-20
Saving an Oracle S QL Query in Oracle S QL
Developer

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-21
Running an SQL Query in MySQL Workbench

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-22
Saving a MySQL Query in MySQL Workbench

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Reading Specified Rows from a Single Table The
SQL DISTINCT Keyword
/* *** SQL-Query-CH02-05 *** */
SELECT DISTINCT Buyer, Department
FROM S K U_DATA;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Controlling the Number of Rows from a Single Table I 1

The SQL TOP {NumberOfRows} Function


/* *** SQL-Query-CH02-06 *** */
SELECT TOP 5 Buyer, Department
FROM S K U_DATA;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Controlling the Number of Rows from a Single Table 2II
The SQL TOP {Percentage} PERCENT Function
/* *** SQL-Query-CH02-07 *** */
SELECT TOP 75 PERCENT Buyer, Department
FROM S K U_DATA;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Controlling Specified Rows from a Single Table
The SQL WHERE Clause: Character Strings
/* *** SQL-Query-CH02-08 *** */
SELECT *
FROM S K U_DATA
WHERE Department = 'Water Sports';

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-23
SQL Comparison Operators
S Q L Comparison Operators
Operator Meaning
= Is equal to
<> Is NOT Equal to
< Is less than
> Is greater than
<= Is less than OR equal to
>= Is greater than OR equal to
IN Is equal to one of a set of values
NOT IN Is NOT equal to one of a set of values
BETWEEN Is within a range of numbers (includes the end points)
NOT BETWEEN Is NOT within a range of numbers (includes the end points)
LIKE Matches a sequence of characters
NOT LIKE Does NOT match a sequence of characters
IS NULL Is equal to NULL
IS NOT NULL Is NOT equal to NULL

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Reading Specified Rows from a Single Table 1I
The SQL WHERE Clause: Dates
/* *** SQL-Query-CH02-09 *** */
SELECT *
FROM CATALOG_S K U_2017
WHERE DateOnWebSite = '01-JAN-2017';

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Reading Specified Rows from a Single Table 2II
The SQL WHERE Clause: Numbers
/* *** SQL-Query-CH02-10 *** */
SELECT *
FROM S K U_DATA
WHERE S K U > 200000;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Reading Specified Columns and Rows from a Single
I Column Names and the SQL WHERE Clause
Table 1
/* *** SQL-Query-CH02-11 *** */
SELECT S K U_Description, Department
FROM S K U_DATA
WHERE Department = 'Climbing';

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Reading Specified Columns and Rows from a Single
Table 2II Column Names and the SQL WHERE Clause
/* *** SQL-Query-CH02-12 *** */
SELECT S K U_Description, Buyer
FROM S K U_DATA
WHERE Department = 'Climbing’;
• SQL does not require that the column used in the WHERE clause also
appear in the SELECT clause column list as seen above.

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
I
Sorting the SQL Query Results 1
The SQL ORDER BY Clause
/* *** SQL-Query-CH02-13 *** */
SELECT *
FROM ORDER_ITEM
ORDER BY OrderNumber;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
II
Sorting the SQL Query Results 2
Ascending and Descending Sort Order
/* *** SQL-Query-CH02-16 *** */
SELECT *
FROM ORDER_ITEM
ORDER BY Price DESC, OrderNumber ASC;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-24
SQL Logical Operators
Operator Meaning
AND Both conditions are TRUE
OR One of the other or both conditions are TRUE
NOT Negates the associated condition

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
II
SQL WHERE Clause Options 2
SQL AND Operator
/* *** SQL-Query-CH02-18 *** */
SELECT *
FROM S K U_DATA
WHERE Department='Water Sports’
AND Buyer='Nancy Meyers';

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
III
SQL WHERE Clause Options 3
SQL OR Operator
/* *** SQL-Query-CH02-19 *** */
SELECT *
FROM S K U_DATA
WHERE Department='Camping’
OR Department='Climbing';

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
SQL WHERE Clause Options IV
4
SQL NOT Operator
/* *** S Q L-Query-CH02-20 *** */
SELECT *
FROM S K U_DATA
WHERE Department='Water Sports’
AND NOT Buyer='Nancy Meyers';

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
SQL WHERE Clause Options 5V
Sets of Values: SQL IN Operator
/* *** S Q L-Query-CH02-21 *** */
SELECT *
FROM S K U_DATA
WHERE Buyer IN ('Nancy Meyers', 'Cindy Lo', 'Jerry
Martin');

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
SQL WHERE Clause Options 6 VI
Sets of Values: SQL NOT IN Operator
/* *** S Q L-Query-CH02-22 *** */
SELECT *
FROM S K U_DATA
WHERE Buyer NOT IN ('Nancy Meyers', 'Cindy Lo',
'Jerry Martin');

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
SQL WHERE Clause Options 7VII
Ranges of Values: Using Math Symbols
/* *** S Q L-Query-CH02-23 *** */
SELECT *
FROM ORDER_ITEM
WHERE ExtendedPrice >= 100
AND ExtendedPrice <= 200
ORDER BY ExtendedPrice;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
SQL WHERE Clause Options 8VIII
Ranges of Values: SQL BETWEEN Operator
/* *** S Q L-Query-CH02-24 *** */
SELECT *
FROM ORDER_ITEM
WHERE ExtendedPrice BETWEEN 100 AND 200 ORDER BY
ExtendedPrice;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
SQL WHERE Clause Options IX
9
Ranges of Values: SQL NOT BETWEEN Operator
/* *** S Q L-Query-CH02-25 *** */
SELECT *
FROM ORDER_ITEM
WHERE ExtendedPrice NOT BETWEEN 100 AND 200
ORDER BY ExtendedPrice;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
SQL WHERE Clause Options X 10
Character String Patterns: SQL Wildcard Characters
• The SQL LIKE operator and SQL NOT LIKE operator can
be combined with wildcard symbols:
– the SQL underscore (_) wildcard character, which
represents a single, unspecified character in a specific
position in the character string
– the SQL percent sign (%) wildcard character, which
represents any sequence of contiguous, unspecified
characters (including spaces) in a specific position in
the character string

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
SQL WHERE Clause Options 11 XI
Character String Patterns: SQL Like Operator 1I
/* *** S Q L-Query-CH02-26 *** */
SELECT *
FROM S K U_DATA
WHERE Buyer LIKE 'Pete%';

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
SQL WHERE Clause Options 12 XII
Character String Patterns: SQL Like Operator 2II
/* *** S Q L-Query-CH02-27 *** */
SELECT *
FROM S K U_DATA
WHERE S K U_Description LIKE '%Tent%';

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
SQL WHERE Clause Options 13 XIII
III
Character String Patterns: SQL Like Operator 3
/* *** S Q L-Query-CH02-29 *** */
SELECT *
FROM S K U_DATA
WHERE S K U LIKE '%2__';

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
SQL WHERE Clause Options 14XIV
Using NULL Values: SQL IS NULL Operator
/* *** S Q L-Query-CH02-30 *** */
SELECT *
FROM CATALOG_S K U_2017
WHERE CatalogPage IS NULL;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
SQL WHERE Clause Options 15XV
Using NULL Values: SQL IS NOT NULL Operator
/* *** SQL-Query-CH02-31 *** */
SELECT *
FROM CATALOG_S K U_2017
WHERE CatalogPage IS NOT NULL;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-25
SQL Built-in Aggregate Functions
Function Meaning
COUNT(*) Count the number of rows in a table
COUNT Count the number of rows in the table where column
({Name}] {Name} IS NOT NULL
SUM Calculate the sum of all values (numeric columns only)
AVG Calculate the average of all values (numeric columns only)
MIN Calculate the minimum value of all values
MAX Calculate the maximum value of all values

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Performing Calculations in SQL Queries 2II
Using SQL Built-in Aggregate Functions: SUM
/* *** SQL-Query-CH02-32 *** */
SELECT SUM(OrderTotal) AS OrderSum
FROM RETAIL_ORDER;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
III
Performing Calculations in S QL Queries 3
Using SQL Built-in Aggregate Functions: SUM, AVG,
MIN AND MAX
/* *** SQL-Query-CH02-35 *** */
SELECT SUM(ExtendedPrice) AS OrderItemSum,
AVG(ExtendedPrice) AS OrderItemAvg,
MIN(ExtendedPrice) AS OrderItemMin,
MAX(ExtendedPrice) AS OrderItemMax
FROM ORDER_ITEM;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
IV
Performing Calculations in S QL Queries 4
Using SQL Built-in Aggregate Functions: COUNT(*)
/* *** SQL-Query-CH02-36 *** */
SELECT COUNT(*) AS NumberOfRows
FROM ORDER_ITEM;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
V
Performing Calculations in S QL Queries 5
Using SQL Built-in Aggregate Functions:
COUNT({Name})
/* *** S Q L-Query-CH02-38 *** */
SELECT COUNT(DISTINCT Department) AS DeptCount
FROM S K U_DATA;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Performing Calculations in SQL Queries 6VI
SQL Expressions 1I
• You should be aware of two limitations to SQL built-in
functions:
– 1) You cannot combine table column names with an S
QL built-in function.
/* *** S Q L-Query-CH02-40 *** */
SELECT Department, COUNT(*)
FROM S K U_DATA;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Performing Calculations in SQL Queries 7
VIIS
QL Expressions 2 II
– 2) You cannot use them (table names) in an S QL
WHERE clause because the SQL WHERE clause
operates on rows (choosing which rows will be
displayed), whereas the aggregate functions operate
on columns.
/* *** SQL-Query-CH02-41 *** */
SELECT *
FROM RETAIL_ORDER
WHERE OrderTotal > AVG(OrderTotal);

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Performing Calculations in SQL Queries 8VIII
Using SQL Expressions 1I
/* *** S Q L-Query-CH02-42 *** */
SELECT Order Number, S K U, (Quantity * Price) AS E P
FROM ORDER_ITEM
ORDER BY Order Number, S K U;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Performing Calculations in SQL Queries 9IX
Using SQL Expressions 2II
/* *** S Q L-Query-CH02-44 *** */
SELECT OrderNumber, S K U
FROM ORDER_ITEM
WHERE Quantity * Price) <> ExtendedPrice
ORDER BY OrderNumber, S K U;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Performing Calculations in S QL Queries X
10
Using SQL Expressions with String Functions:
Concatenation I1
/* *** S Q L-Query-CH02-45 *** */
SELECT S K U, S K U_Description, (Buyer+' in '+Department) AS
Sponsor
FROM S K U_DATA
ORDER BY S K U;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
XI
Performing Calculations in S QL Queries 11
Using SQL Expressions with String Functions:
II
Concatenation 2
This is the Oracle Database syntax:
/* *** S Q L-Query-CH02-45-Oracle-Database *** */
SELECT S K U, S K U_Description, (Buyer||' in '||Department) AS
Sponsor
FROM S K U_DATA
ORDER BY S K U;

This is the My SQL syntax:


/* *** S Q L-Query-CH02-45-My S Q L *** */
SELECT S K U, S K U_Description, CONCAT(Buyer, ‘ in ’,
Department) AS Sponsor
FROM S K U_DATA
ORDER BY S K U;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
XII
Performing Calculations in S QL Queries 12
Using SQL Expressions with String Functions:
III
Concatenation 3
/* *** S Q L-Query-CH02-46 *** */
SELECT S K U, S K U_Description, RTRIM(Buyer)+' in ‘
+RTRIM(Department) AS Sponsor
FROM S K U_DATA
ORDER BY S K U;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-26
Department Groups in the CATALOG_S KU_2017
Table

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Figure 2-27
Grouping Data into Department Groups

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Grouping Rows in SQL Queries 3III
The SQL GROUP BY Clause
/* *** S Q L-Query-CH02-48 *** */
SELECT Department, COUNT(S K U) AS NumberOfCatalogItems
FROM CATALOG_S K U_2017
WHERE CatalogPage IS NOT NULL
GROUP BY Department;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
IV
Grouping Rows in SQL Queries 4
The SQL HAVING Clause
/* *** S Q L-Query-CH02-49 *** */
SELECT Department, COUNT(S K U) AS NumberOfCatalogItems
FROM CATALOG_S K U_2017
WHERE CatalogPage IS NOT NULL
GROUP BY Department
HAVING COUNT(S K U) > 2;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Grouping Rows in SQL Queries 5V
• The SQL WHERE clause specifies which rows will be used to
determine the groups.
• The SQL HAVING clause specifies which groups will be used in
the final result.
• 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.
• 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.

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Grouping Rows in SQL Queries 6VI
Grouping by More Than One Column
/* *** S Q L-Query-CH02-50 *** */
SELECT Department, Buyer, COUNT(S K U) AS Dept_Buyer_S K
U_Count
FROM S K U_DATA
GROUP BY Department, Buyer;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
VII
Grouping Rows in SQL Queries 7
Column Names that Must Appear in GROUP BY
/* *** S Q L-Query-CH02-51 *** */
SELECT Department, S K U, COUNT(S K U) AS Dept_S K
U_Count
FROM S K U_DATA
GROUP BY Department;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
VIII
Grouping Rows in SQL Queries 8
Using the SQL ORDER BY Clause with Groupings
/* *** S Q L-Query-CH02-52 *** */
SELECT Department, COUNT(S K U) AS Dept_S K U_Count
FROM S K U_DATA
WHERE S K U <> 302000
GROUP BY Department
HAVING COUNT(S K U) > 1
ORDER BY Dept_S K U_Count;

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved
Copyright

This work is protected by United States copyright laws and is


provided solely for the use of instructors in teaching their
courses and assessing student learning. Dissemination or sale of
any part of this work (including on the World Wide Web) will
destroy the integrity of the work and is not permitted. The work
and materials from it should never be made available to students
except by instructors using the accompanying text in their
classes. All recipients of this work are expected to abide by these
restrictions and to honor the intended pedagogical purposes and
the needs of other instructors who rely on these materials.

Copyright © 2019, 2016, 2014 Pearson Education, Inc. All Rights Reserved

You might also like