Professional Documents
Culture Documents
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use. 2
Functions
Reminder: Listing Table Rows
SELECT: Command to list the contents
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
4
SQL Functions
Functions always use a numerical, date, or string
value
Value may be part of a command or may be an
attribute located in a table
Function may appear anywhere in an SQL statement
where a value or an attribute can be used
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
5
SQL Functions
Numeric functions
String functions
Conversion functions
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
6
Aggregate Functions
SQL provides useful aggregate functions that:
Count
Find minimum and maximum values
Calculate averages
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
13
Table 7.8 - Some Basic SQL Aggregate
Functions
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
14
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
15
WRONG:
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
16
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
17
Grouping Data
Frequency distributions created by GROUP BY
clause within SELECT statement
Syntax - SELECT columnlist
FROM tablelist
[WHERE conditionlist]
[GROUP BY columnlist]
[HAVING conditionlist]
[ORDER BY columnlist [ASC | DESC]];
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
18
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
19
HAVING Clause
Extension of GROUP BY feature
Applied to output of GROUP BY operation
Used in conjunction with GROUP BY clause in
second SQL command set
Similar to WHERE clause in SELECT statement
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
20
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a
certain product or service or otherwise on a password-protected website or school-approved learning management system for classroom use.
21
Subqueries
Subqueries and Correlated Queries
Subquery is a query inside another query
Subquery can return:
One single value - One column and one row
A list of values - One column and multiple rows
A virtual table - Multicolumn, multirow set of values
No value - Output of the outer query might result in an
error or a null empty set
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected
website or school-approved learning management system for classroom use.
23
WHERE Subqueries
Uses inner SELECT subquery on the right side of a
WHERE comparison expression
Value generated by the subquery must be of a
comparable data type
If the query returns more than a single value, the
DBMS will generate an error
Can be used in combination with joins
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected
website or school-approved learning management system for classroom use.
24
IN and HAVING Subqueries
IN subqueries
Used to compare a single attribute to a list of values
HAVING subqueries
HAVING clause restricts the output of a GROUP BY
query by applying conditional criteria to the grouped
rows
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected
website or school-approved learning management system for classroom use.
25
Multirow Subquery Operators:
ANY and ALL
ALL operator
Allows comparison of a single value with a list of
values returned by the first subquery
Uses a comparison operator other than equals
ANY operator
Allows comparison of a single value to a list of values
and selects only the rows for which the value is greater
than or less than any value in the list
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected
website or school-approved learning management system for classroom use.
26
FROM and Attribute List Subqueries
FROM clause:
Specifies the tables from which the data will be drawn
Can use SELECT subquery
SELECT statement uses attribute list to indicate what
columns to project in the resulting set
Inline subquery
Subquery expression included in the attribute list that must
return one value
Column alias cannot be used in attribute list computation
if alias is defined in the same attribute list
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected
website or school-approved learning management system for classroom use.
27
Correlated Subqueries
Executes once for each row in the outer query
Inner query references a column of the outer
subquery
Can be used with the EXISTS special operator
©2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected
website or school-approved learning management system for classroom use.
28