Lecture 6 New - SQL Data Manipulation Language (Intermediate)
Lecture 6 New - SQL Data Manipulation Language (Intermediate)
2
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Review of SELECT command
3
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Aggregate Processing (1 of 3)
© 2019 Cengage. May not be copied, scanned, or duplicated, 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
for classroom use. 4
Aggregate Processing (2 of 3)
Table 7.7
Some Basic SQL Aggregate
Functions
Function Output
COUNT The number of rows containing non-null values
MIN The minimum attribute value encountered in a given
column
MAX The maximum attribute value encountered in a given
column
SUM The sum of all values for a given column
AVG The arithmetic mean (average) for a specified column
© 2019 Cengage. May not be copied, scanned, or duplicated, 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
for classroom use. 5
SELECT Statement - Aggregates
6
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
SELECT Statement - Aggregates
• Aggregate functions can be used only in SELECT list and in HAVING clause.
• If SELECT list includes an aggregate function and there is no GROUP BY clause,
SELECT list cannot reference a column out with an aggregate function. For
example, the following is illegal:
7
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Use of COUNT(*)
How many properties cost more than £350 per month to rent?
8
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Use of COUNT(DISTINCT)
9
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Use of COUNT and SUM
10
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Use of MIN, MAX, AVG
11
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
GROUP BY and HAVING
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
GROUP BY and HAVING
• HAVING clause
• Operates very much like the WHERE clause in the SELECT statement
• HAVING clause is applied to the output of a GROUP BY operation
• Syntax:
SELECT columnlist
FROM tablelist
[WHERE conditionlist ]
[GROUP BY columnlist ]
[HAVING conditionlist ]
[ORDER BY columnlist [ASC | DESC] ];
© 2019 Cengage. May not be copied, scanned, or duplicated, 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
for classroom use. 13
SELECT Statement - Grouping
14
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Use of GROUP BY
SELECT branchNo,
COUNT(staffNo) AS myCount,
SUM(salary) AS mySum
FROM Staff
GROUP BY branchNo
ORDER BY branchNo;
15
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Restricted Groupings – HAVING clause
• HAVING clause is designed for use with GROUP BY to restrict groups that
appear in final result table.
• Similar to WHERE, but WHERE filters individual rows whereas HAVING filters
groups.
• WHERE would be like telling individuals, "If you're shorter than 5 feet, don't
come to the party."
• HAVING is telling entire groups, "If the average height in your group is less
than 5 feet, don't show up."
• Column names in HAVING clause must also appear in the GROUP BY list or be
contained within an aggregate function.
16
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Use of HAVING
For each branch with more than 1 member of staff, find number of staff in each
branch and sum of their salaries.
SELECT branchNo,
COUNT(staffNo) AS myCount,
SUM(salary) AS mySum
FROM Staff
GROUP BY branchNo
HAVING COUNT(staffNo) > 1
ORDER BY branchNo;
17
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Subqueries
Gabungan beberapa queries
Dia ni ada dua boleh guna in atau join
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Subqueries (1 of 3)
• Key characteristics
• A subquery is a query (SELECT statement) inside another query
• A subquery is normally expressed inside parentheses
• The first query in the SQL statement is known as the outer query
• The query inside the SQL statement is known as the inner query
• The inner query is executed first
• The output of an inner query is used as the input for the outer query
• The entire SQL statement is sometimes referred to as a nested query
• Subquery can return one or more values
• 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)
19
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Subqueries (2 of 3)
• WHERE subqueries
• Most common type of subquery uses an inner SELECT subquery on the right side of a
WHERE comparison expression
• IN subqueries
• IN operator: used to compare a single attribute to a list of values
• IN subquery: values are not known beforehand, but can be derived using a query
• HAVING subqueries
• HAVING clause: used to restrict the output of a GROUP BY query by applying
conditional criteria to the grouped rows
• Multirow subquery operators: ALL and ANY
• ALL operator compares a single value with a list of values returned by the first
subquery using a comparison operator other than equals
• ANY operator compares a single value to a list of values and select only the rows
greater than or less than any value in the list
20
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Subqueries (3 of 3)
• FROM subqueries
• FROM clause specifies the table(s) from which the data will be drawn
• Attribute list subqueries
• Inline subquery: subquery expression
- Example: can be used to list the difference between each product’s price and the average
product price
• Correlated subquery
• Executes once for each row in the outer query
• Inner query is related to the outer query; the inner query references a column of the
outer subquery
• Can also be used with the EXISTS special operator
- Can be used whenever there is a requirement to execute a command based on the result of
another query
- Can be used with uncorrelated subqueries, but it is almost always used with correlated
subqueries
21
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Subqueries
22
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Subquery with Equality
23
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Subquery with Equality
• Inner SELECT finds branch number for branch at ‘163 Main St’ (‘B003’).
• Outer SELECT then retrieves details of all staff who work at this branch.
• Outer SELECT then becomes:
24
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Subquery with Aggregate
List all staff whose salary is greater than the average salary, and
show by how much.
25
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Subquery with Aggregate
26
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Subquery Rules
• By default, column names refer to table name in FROM clause of subquery. Can
refer to a table in FROM using an alias.
27
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Nested subquery: use of IN
28
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
ANY and ALL
• ANY and ALL may be used with subqueries that produce a single column of
numbers.
• With ALL, condition will only be true if it is satisfied by all values produced by
subquery.
• With ANY, condition will be true if it is satisfied by any values produced by
subquery.
• If subquery is empty, ALL returns true, ANY returns false.
• SOME may be used in place of ANY.
29
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Use of ANY/SOME
Find staff whose salary is larger than salary of at least one member of staff at
branch B003.
30
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Use of ANY/SOME
• Inner query produces set {12000, 18000, 24000} and outer query selects those
staff whose salaries are greater than any of the values in this set.
31
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Use of ALL
Find staff whose salary is larger than salary of every member of staff at branch
B003.
SELECT staffNo, fName, lName, position, salary
FROM Staff
WHERE salary > ALL
(SELECT salary
FROM Staff
WHERE branchNo = ‘B003’);
32
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Summary (1 of 2)
• SQL commands can be divided into two overall categories: data definition
language (DDL) commands and data manipulation language (DML) commands
• The ANSI standard data types are supported by all RDBMS vendors in different
ways
• The basic data types are NUMBER, NUMERIC, INTEGER, CHAR, VARCHAR, and DATE
• The SELECT statement is the main data retrieval command in SQL
• The column list represents one or more column names separated by commas
• Operations that join tables can be classified as inner joins and outer joins
• A natural join returns all rows with matching values in the matching columns
and eliminates duplicate columns
• Joins may use keywords such as USING and ON
• The ORDER BY clause is used to sort the output of a SELECT statement
33
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.
Summary (2 of 2)
• The WHERE clause can be used with the SELECT, UPDATE, and DELETE
statements to restrict the rows affected by the DDL command
• Aggregate functions (COUNT, MIN, MAX, and AVG) are special functions that
perform arithmetic computations over a set of rows
• Subqueries and correlated queries are used when it is necessary to process
data based on other processed data
• Most subqueries are executed in a serial fashion
• SQL functions are used to extract or transform data
• SQL provides relational set operators to combine the output of two queries to
generate a new relation
• Crafting effective and efficient SQL queries requires a great deal of skill
34
© 2019 Cengage. May not be copied, scanned, or duplicated, 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 fo
r classroom use.