Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Lecture 6 New - SQL Data Manipulation Language (Intermediate)

This document discusses SQL data manipulation language concepts including aggregate functions, GROUP BY, and HAVING clauses. It covers how to use aggregate functions like COUNT, MIN, MAX, SUM, and AVG to summarize data. It also explains how to group data using the GROUP BY clause and filter grouped data using the HAVING clause. Examples are provided for counting rows, finding minimum/maximum values, calculating sums and averages, and grouping and filtering aggregated data.

Uploaded by

adli tamrin
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lecture 6 New - SQL Data Manipulation Language (Intermediate)

This document discusses SQL data manipulation language concepts including aggregate functions, GROUP BY, and HAVING clauses. It covers how to use aggregate functions like COUNT, MIN, MAX, SUM, and AVG to summarize data. It also explains how to group data using the GROUP BY clause and filter grouped data using the HAVING clause. Examples are provided for counting rows, finding minimum/maximum values, calculating sums and averages, and grouping and filtering aggregated data.

Uploaded by

adli tamrin
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Lecture 6

SQL Data Manipulation Language


(Intermediate)
Learning Objectives

• After completing this chapter, you will be able to:


• Use aggregate functions.
• Group data using GROUP BY and HAVING.
• Create subqueries to preprocess data for inclusion in other queries
• Identify and use a variety of SQL functions for string, numeric, and date manipulation

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

• Selecting rows with conditional restrictions


• WHERE clause is used to add conditional restrictions to the SELECT statement that
limit the rows returned by the query
• Syntax:
SELECT columnlist
FROM tablelist
[WHERE conditionlist ]
[ORDER BY columnlist [ASC | DESC] ];

• Using comparison operators on character attributes


• May be used to place restrictions on character-based attributes
• Using comparison operators on dates
• Date procedures are often more software-specific than other SQL procedures

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)

• Takes a collection of rows and reduces it to a single row


• SQL provides useful aggregate functions that count, find minimum and maximum
values, calculate averages, etc.
• Aggregate functions
• Count
• MIN and MAX
• SUM and AVG
• Grouping data
• GROUP BY clause syntax:
SELECT columnlist
FROM tablelist
[WHERE conditionlist ]
[GROUP BY columnlist ]
[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. 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

• Each operates on a single column of a table and returns a single value.


• COUNT, MIN, and MAX apply to numeric and non-numeric fields, but SUM and
AVG may be used on numeric fields only.
• Apart from COUNT(*), each function eliminates nulls first and operates only on
remaining non-null values.
• COUNT(*) counts all rows of a table, regardless of whether nulls or duplicate
values occur.
• Can use DISTINCT before column name to eliminate duplicates.
• DISTINCT has no effect with MIN/MAX, but may have with SUM/AVG.

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:

SELECT staffNo, COUNT(salary)


FROM Staff;

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?

SELECT COUNT(*) AS myCount


FROM PropertyForRent
WHERE rent > 350;

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)

How many different properties viewed in May ‘04?

SELECT COUNT(DISTINCT propertyNo) AS myCount


FROM Viewing
WHERE viewDate BETWEEN ‘1-May-04’
AND ‘31-May-04’;

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

Find number of Managers and sum of their salaries.

SELECT COUNT(staffNo) AS myCount,


SUM(salary) AS mySum
FROM Staff
WHERE position = ‘Manager’;

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

Find minimum, maximum, and average staff salary.

SELECT MIN(salary) AS myMin,


MAX(salary) AS myMax,
AVG(salary) AS myAvg
FROM Staff;

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

• Use GROUP BY clause to get sub-totals.


• SELECT and GROUP BY closely integrated: each item in SELECT list must be
single-valued per group, and SELECT clause may only contain:
• column names
• aggregate functions
• constants
• expression involving combinations of the above.
• All column names in SELECT list must appear in GROUP BY clause unless
name is used only in an aggregate function.
• If WHERE is used with GROUP BY, WHERE is applied first, then groups are
formed from remaining rows satisfying predicate.
• ISO considers two nulls to be equal for purposes of GROUP BY.

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

Find number of staff in each branch and their total salaries.

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

When to Use a Subquery in SQL (youtube.com)

© 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

• Some SQL statements can have a SELECT embedded within them.


• A subselect can be used in WHERE and HAVING clauses of an outer SELECT,
where it is called a subquery or nested query.
• Subselects may also appear in INSERT, UPDATE, and DELETE statements.

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

List staff who work in branch at ‘163 Main St’.

SELECT staffNo, fName, lName, position


FROM Staff
WHERE branchNo =
(SELECT branchNo
FROM Branch
WHERE street = ‘163 Main St’);

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:

SELECT staffNo, fName, lName, position


FROM Staff
WHERE branchNo = ‘B003’;

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.

SELECT staffNo, fName, lName, position,


salary – (SELECT AVG(salary) FROM Staff) As SalDiff
FROM Staff
WHERE salary >
(SELECT AVG(salary)
FROM Staff);

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

• Cannot write ‘WHERE salary > AVG(salary)’


• Instead, use subquery to find average salary (17000), and then use outer
SELECT to find those staff with salary greater than this:

SELECT staffNo, fName, lName, position,


salary – 17000 As salDiff
FROM Staff
WHERE salary > 17000;

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

• ORDER BY clause may not be used in a subquery (although it may be used in


outermost SELECT).

• Subquery SELECT list must consist of a single column name or expression,


except for subqueries that use EXISTS.

• By default, column names refer to table name in FROM clause of subquery. Can
refer to a table in FROM using an alias.

• When subquery is an operand in a comparison, subquery must appear on right-


hand side.

• A subquery may not be used as an operand in an expression.

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

List properties handled by staff at ‘163 Main St’.


SELECT propertyNo, street, city, postcode, type, rooms, rent
FROM PropertyForRent
WHERE staffNo IN
(SELECT staffNo
FROM Staff
WHERE branchNo =
(SELECT branchNo
FROM Branch
WHERE street = ‘163 Main St’));

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.

SELECT staffNo, fName, lName, position, salary


FROM Staff
WHERE salary > SOME
(SELECT salary
FROM Staff
WHERE branchNo = ‘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.

You might also like