Uploaded by tastethekream

Week 5 - Module 4 Lecture Notes

advertisement
A Guide to S Q L, Tenth Edition
Chapter 4
Single-Table Queries
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Objectives (1 of 2)
•
•
•
•
Retrieve data from a database using S Q L commands
Use simple and compound conditions in queries
Use the BETWEEN, LIKE, and IN operators in queries
Use computed columns in queries
• Sort data using the ORDER BY clause
• Sort data using multiple keys and in ascending and
descending order
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Objectives (2 of 2)
• Use aggregate functions in a query
• Use subqueries
• Group data using the GROUP BY clause
• Select individual groups of data using the HAVING clause
• Retrieve columns with null values
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Constructing Simple Queries (1 of 2)
• What is a query?
• Question represented in a way that the DBMS can understand
• How do you implement a query in S Q L?
• Use SELECT command
• Are there any special formatting rules?
• No
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Constructing Simple Queries (2 of 2)
• SELECT-FROM-WHERE statement
• Basic form of SELECT command
• SELECT clause
• Portion of the command that follows the word SELECT
• Columns to be included in the query results
• FROM clause
• Portion of the command that follows the word SELECT
• Name of the table containing data to be queried
• WHERE clause
• Conditions to be applied to data retrieved
• Optional
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Retrieving Certain Columns and Rows
• Listing number, first name, last name, and balance for all
customers
• List columns to be included in SELECT clause
• List name of the table in FROM clause
• No WHERE clause needed
• All customers are requested
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-1: SELECT Command to Select Certain
Columns from the Customer Table
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Retrieving All Columns and Rows
• Listing a complete table
• Use an asterisk (*) to indicate all columns in the SELECT clause
• The result will list all columns in the order used when table was
created
• List specific columns in SELECT clause to present columns in a
different order
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-2: SELECT Command to Select All
Columns from the ITEM Table
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Using a WHERE Clause
• WHERE clause
• Used to retrieve rows that satisfy some condition
• Simple condition
• Column name and comparison operator followed by either a
column name or a value
• Can compare columns
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-3: SELECT Command to Find the LAST_NAME
of CUST_ID 125 in the CUSTOMER Table
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-4: Comparison Operators Used in S Q L
Commands
Comparison Operator
Description
=
Equal to
>
Less than
<
Greater than
<=
Less than or equal to
>=
Greater than or equal to
<>
Not equal to
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-6: SELECT Command to Find Specific Information for Customers
with a BALANCE That Exceeds Their CREDIT_LIMIT
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Using Compound Conditions
• Compound conditions
• Connect two or more simple conditions with AND, OR, and NOT
operators
• AND operator: All simple conditions must be true
• OR operator: One simple condition must be true
• NOT operator: Reverses the truth of the original condition
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-7: SELECT Command with an AND
Condition on Separate Lines
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-9: SELECT Command with an OR
Condition Instead of the AND Condition
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-10: SELECT Command with a NOT
Condition
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Using BETWEEN Operators
• BETWEEN operator
•
•
•
•
•
Helps specify a range of values in a condition
Used instead of AND operator
Used when searching a range of values
Makes SELECT commands simpler to construct
Not an essential feature of S Q L
• Helps select a value equal to either value in the condition
and in the range of the values
• BETWEEN 125 AND 250
• Values of 125 through 250 would make the condition true
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-12: SELECT Command Using the
BETWEEN Operator
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Using Computed Columns
• Computed column
• Does not exist in the database but can be computed using data in
existing columns
• Arithmetic operators
•
•
•
•
+ for addition
− for subtraction
* for multiplication
/ for division
• AS clause
• Used to assign a name, or alias, to a computed column by
following the computation with the word AS and the desired name
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-14: SELECT Command with a Computed Column
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-15: SELECT Command with a Named Column
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Using the LIKE Operator
• Used for pattern matching
• Used along with a wildcard symbol
• Example: LIKE %Rock% will retrieve data with those characters
• Underscore (_) represents any single character
• Example: “T_m” for Tim or Tom or T3m
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-17: SELECT Command with a LIKE
Operator and Wildcard Characters
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Using the IN Operator
• Provides a concise way of phrasing certain conditions
• Example
• List the customer ID, first name, last name, and credit limit for each
customer with a credit limit of $500, $750, or $1,000
• IN clause contains a collection of the values 500, 750, and 1000
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-18: SELECT Command with an IN Clause
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Sorting
• When querying a relational database, there is no defined
order in which results are displayed
• Rows may or may not be displayed in the order in which the data
was originally entered
• Use ORDER BY clause to list data in a specific order
• Sort key or key
• Column on which data is to be sorted
• The default sort order is ascending
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-19: SELECT Command to Sort Rows
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Additional Sorting Options
• Sorting data on two columns
• Major sort key (primary sort key): Column that is more important
• Minor sort key (secondary sort key): Column that is less
important
• To sort on multiple keys, list sort keys in order of
importance in the ORDER BY clause
• For descending order sort, use D E S C
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-20: SELECT Command to Sort Data Using
Multiple Sort Keys
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Using Functions
• Aggregate functions
• Used to calculate sums, averages, counts, maximum
values, and minimum values
• Apply to groups of rows
Figure 4-21: S Q L Aggregate Functions
FUNCTION
DESCRIPTION
AVG
Calculates the average value in a column
COUNT
Determines the number of rows in a table
MAX
Determines the maximum value in a column
MIN
Determines the minimum value in a column
SUM
Calculates the total of the values in a column
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Using the COUNT Function
• Helps to count the number of rows in a table
• Allows the use of asterisk (*) to represent any column
Figure 4-22: SELECT Command to Count Rows
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Using the SUM Function
• Used to calculate totals of columns
• Column must be specified and must be numeric
Figure 4-24: SELECT Command to Count Rows and Calculate a Total
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Using the A V G, MAX, and MIN Functions
• Similar to using SUM, except that different statistics are
calculated
• A V G calculates the average value in a numeric range
• MAX calculates the maximum value in a numeric range
• MIN calculates the minimum value in a numeric range
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-25: SELECT Command with Multiple Functions
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Using the DISTINCT Operator
• Eliminates duplicate values
• Used with COUNT function
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-26: SELECT Command Listing the
Customer ID for Each Invoice
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-27: SELECT Command Listing the Customer
ID for All Invoices with Duplicates Removed
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Nesting Queries
• Help obtain results that require two or more steps
• Subquery: An inner query placed inside another query
• Outer query uses subquery results
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-30: SELECT Command to List All Item ID
Values in the Category HOR
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-31: SELECT Command Using Results
from Figure 4-30
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-32: SELECT Command Using the IN
Operator and a Subquery
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-34: SELECT Command Using an Operator
and a Subquery
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Grouping
• Creates groups of rows that share common
characteristics
• When grouping rows, any calculations indicated in the
SELECT command are performed for the entire group
• Allows grouping of data on a particular column and calculation of
statistics
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-35: SELECT Command Grouping Records
in a Column
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Using a HAVING Clause
• Used to restrict groups that will be included in query
results
• HAVING versus WHERE
• WHERE: Limits rows
• HAVING: Limits groups
• Can be used together if condition involves both rows and groups
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-36: SELECT Command Restricting the
Groups to Include in the Results
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Figure 4-37: SELECT Command Counting the
Number of Rows in Each Group
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Nulls
• Used in conditions involving columns that can accept
null values
• IS NULL: Used when column contains null value
• IS NOT NULL: Used when column does not contain null
value
Figure 4-41: Selecting Rows Containing Null Values in the ADDRESS Column
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Summary (1 of 3)
• Basic form of the S Q L SELECT command is SELECTFROM-WHERE
• Simple conditions are written in a specific form using
comparison operators
• AND, OR, and NOT help form compound conditions by
combining simple conditions
• BETWEEN is used to indicate a range of values in a
condition
• Computed columns in S Q L commands involve using
arithmetic operators and writing the computation in place
of a column name
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Summary (2 of 3)
• LIKE is used to check for a value in a character column
that is similar to a particular string of characters
• IN is used to determine whether a column contains a
value in a set of values
• ORDER BY is used to sort data
• S Q L processes the aggregate functions COUNT, SUM,
A V G, MAX, and MIN
• DISTINCT helps avoid duplicates in a query that uses an
aggregate function
• Subquery is an S Q L query that is placed inside another
query
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Summary (3 of 3)
• GROUP BY is used to group data
• HAVING is used to restrict the output to certain groups
• IS NULL and IS NOT NULL are used in WHERE clauses
Mark Shellman, Hassan Afyouni, Philip J Pratt & Mary Z Last, A Guide to S Q L, 10th Edition. © 2021 Cengage. All Rights
Reserved. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part.
Download