Chapter 12-1 Group Functions - GROUP by
Chapter 12-1 Group Functions - GROUP by
Chapter 12
Group Functions
• A group or aggregate function that:
– Operates on a set of rows
– Returns a single value
Aggregate functions
Ignore NULL values, except COUNT(*)
2
Group Functions
3
MIN and MAX Functions
• The MIN function returns the minimum or lowest
value in a column from a group of rows
• The MAX function returns the maximum or highest
value in a column from a group of rows.
• Can be used with any data type
• NULL values are ignored
4
MIN Function
5
MAX Function
6
MIN & MAX Functions with Dates
7
AVG Function
• Returns the average value in a column from a
group of rows
8
AVG Function
9
SUM Function
• Returns a single value containing the sum (total)
for a column in the result set
10
SUM Function
11
Types of COUNT Functions
• There are two variations of the COUNT function:
– COUNT(*)
Counts total rows including those rows with a NULL value
– COUNT(column_name)
Counts rows based on a specific column
Does not count rows containing a NULL value
Ignores NULL values
12
COUNT(*) Function
13
COUNT(column_name)
Function
14
Using COUNT & SUM
Functions Together
15
Group Functions & NULLs
• Group functions ignore NULL values, except the
COUNT(*) function
• Be careful when using group functions with columns
that allow a null value
16
Group Functions & NULLs
COUNT(*)
17
Group Functions & NULLs
COUNT(column_name)
18
Group Functions & NULLs
COUNT(*) with IS NOT NULL
19
COALESCE with Group Functions
• When it is necessary to include rows with NULL
values, use the COALESCE function to add a value
to the NULL rows
20
COALESCE with Group Functions
21
COALESCE with Group Functions
22
Using DISTINCT with Group Functions
• DISTINCT can be used with all group functions
23
Using DISTINCT
with COUNT Function
24
More Than One GROUP Function
• Can have more than one group function in the
SELECT clause, on the same or different columns
SELECT DECIMAL( AVG(salary), 7,2) AS avg_salary,
MIN(salary) AS low_salary,
MAX(salary) AS high_salary,
TO_CHAR( MIN(hire_date), 'mm/dd/yyyy' ) AS low_hire_date
FROM employees
WHERE department_id = 50;
25
Group Function Caution
26
Rules for GROUP Functions
27
Rules for GROUP Functions
• MIN and MAX can be used with any data type
28
Chapter 11
GROUP BY / HAVING
Question
• Write the SQL SELECT statement to return the
highest salary of all employees in the employees
table?
30
Question
• Write the SQL statement to return the highest
salary in each department?
31
SELECT Statements
• Until now, you would have to write a number of
different SQL statements to accomplish this:
• And so on
32
GROUP BY and HAVING Clauses
• GROUP BY and HAVING clauses make this easier
33
GROUP BY and HAVING Clauses
• The GROUP BY clause works with group functions to
group data in the result set by columns
• The rows in the result set are grouped together
based on the columns specified in the GROUP BY
clause
34
GROUP BY Example
• Return the highest salary in each department
– Rows are grouped by department_id
– The MAX function is then applied to each group
36
GROUP BY
• The GROUP BY clause divides the rows into smaller
groups by department_id
37
GROUP BY
• The MAX group function is applied to each group,
returning the max (highest) salary for each group
38
GROUP BY
39
GROUP BY Clause on Multiple Columns
• GROUP BY clause may contain multiple columns
40
GROUP BY Clause
with Multiple Columns
41
GROUP BY Clause
with Multiple Columns
42
HAVING Clause
43
HAVING Clause
• WHERE clause – Restricts rows
• HAVING clause – Restricts groups
44
HAVING Clause
45
WHERE CLAUSE versus HAVING Clause
• WHERE clause restricts rows before the rows are
formed into groups
46
WHERE CLAUSE versus HAVING Clause
• Example 12-19
– WHERE clause used with the GROUP BY clause
– Restricts which rows are returned before the rows are
grouped by store code
47
WHERE CLAUSE versus HAVING Clause
48
WHERE CLAUSE versus HAVING Clause
• Example 12-20
– Produces the same results as Example 12-19
– HAVING clause used with the GROUP BY clause
– Restricts groups in the results set
49
WHERE CLAUSE versus HAVING Clause
50
WHERE CLAUSE versus HAVING Clause
• If possible, restrict rows using WHERE clause
before rows are grouped with GROUP BY clause
51
GROUP BY with NULL Values
• When using the GROUP BY clause, NULL values are
grouped as a separate group
52
GROUP BY
with NULL Values
53
GROUP BY using COALESCE
with NULL Character Values
54
GROUP BY using COALESCE
with NULL Numeric Values
55
GROUP BY Rule
• GROUP BY requires that any column listed in the
SELECT column-list that is not part of a group
function (SUM, AVG, MIN, MAX, COUNT) must be
listed in a GROUP BY clause
• What is wrong with this statement?
57
SQL Processing Order of SQL Statement
58
Logical Processing Order of the
SELECT Statement
• The order determines when the objects defined in
one step are made available to the clauses in
subsequent steps
– When the query processor binds to the tables or views
defined in the FROM clause, these objects and their
columns are made available to all subsequent steps
59
Logical Processing Order of the
SELECT Statement
• The SELECT clause is step 5
– Any column aliases or derived columns defined in that
clause cannot be referenced by preceding clauses
– However, they can be referenced by subsequent clauses
such as the ORDER BY clause
60
61