0% found this document useful (0 votes)
10 views61 pages

Chapter 12-1 Group Functions - GROUP by

This document discusses group functions in SQL. It covers aggregate functions like COUNT, SUM, AVG, MIN, MAX that operate on sets of rows and return single values. It describes how each function works and provides examples of using them with columns, dates, and NULL values. It also covers the GROUP BY clause for grouping query results and the HAVING clause for filtering groups.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views61 pages

Chapter 12-1 Group Functions - GROUP by

This document discusses group functions in SQL. It covers aggregate functions like COUNT, SUM, AVG, MIN, MAX that operate on sets of rows and return single values. It describes how each function works and provides examples of using them with columns, dates, and NULL values. It also covers the GROUP BY clause for grouping query results and the HAVING clause for filtering groups.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 61

Group Functions

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

• Can only be used with numeric data types

8
AVG Function

9
SUM Function
• Returns a single value containing the sum (total)
for a column in the result set

• Can only be used with numeric data types

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

• With group functions, the COALESCE function is


nested inside the group function

20
COALESCE with Group Functions

21
COALESCE with Group Functions

22
Using DISTINCT with Group Functions
• DISTINCT can be used with all group functions

• When the keyword DISTINCT is used with the


COUNT function, only non-duplicate values are
returned

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

• Used in the SELECT clause


• Cannot be used in the WHERE clause
• Returns one result
• Group functions ignore NULL values except
COUNT(*)

27
Rules for GROUP Functions
• MIN and MAX can be used with any data type

• SUM and AVG can be used only with numeric data


types

• Use DISTINCT to suppress duplicate values

28
Chapter 11

GROUP BY / HAVING
Question
• Write the SQL SELECT statement to return the
highest salary of all employees in the employees
table?

SELECT MAX(salary) FROM employees;

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:

SELECT MAX(salary) FROM employees WHERE dept_id = 50;


SELECT MAX(salary) FROM employees WHERE dept_id = 60;
SELECT MAX(salary) FROM employees WHERE dept_id = 90;

• 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

• Which MAX salary belongs to which 35


GROUP BY Example
• Include the GROUP BY column in the SELECT
column-list
• Cannot use a column alias in the GROUP BY clause

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

• In a query using a GROUP BY and HAVING clause:


– The rows are first grouped
– Group functions are applied
– Only those groups matching the HAVING clause are
returned

44
HAVING Clause

45
WHERE CLAUSE versus HAVING Clause
• WHERE clause restricts rows before the rows are
formed into groups

• HAVING clause restricts groups after 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

– Which method is better in this scenerio?

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?

SELECT job_id, last_name, AVG(salary)


FROM employees
GROUP BY job_id;
56
GROUP BY Rule – No alias
• A column alias cannot be used in the GROUP BY
clause
• What is wrong with this example?

SELECT job_id, AVG(salary) AS AVG_SAL


FROM employees
GROUP BY AVG_SAL;

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

You might also like