SQL Group Functions: A Comprehensive Guide for Exams, Exams of Accounting

Unlike single-row functions, group functions operate on sets of rows to give one result per group. These sets may be the whole table or the.

Typology: Exams

2022/2023

Uploaded on 03/01/2023

lakshmirnarman
lakshmirnarman šŸ‡ŗšŸ‡ø

5

(5)

221 documents

1 / 43

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Aggregating Data
Using Group Functions
Capter 5
Objectives
After completing this lesson, you should be able to do
the following:
• Identify the available group functions
• Describe the use of group functions
• Group data using the GROUP BY clause
• Include or exclude grouped rows by
using the HAVING clause
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b

Partial preview of the text

Download SQL Group Functions: A Comprehensive Guide for Exams and more Exams Accounting in PDF only on Docsity!

Aggregating Data

Using Group Functions

Capter 5

Objectives

After completing this lesson, you should be able to do

the following:

  • Identify the available group functions
  • Describe the use of group functions
  • Group data using the GROUP BY clause
  • Include or exclude grouped rows by

using the HAVING clause

What Are Group Functions?

Group functions operate on sets of rows to give one result per group.

Group Functions

Unlike single-row functions, group functions operate on sets of rows

to give one result per group. These sets may be the whole table or the

table split into groups.

Types of Group Functions

•AVG

•COUNT

•M AX

•MIN

•STDDEV

•SUM

- VARIANCE

Using AVG and SUM Functions

You can use AVG and SUM for numeric data.

SELECT AVG(sal) , SUM(sal)

FROM emp;

AVG(SAL) SUM(SAL)

Group Functions You can use AVG, SUM. MIN, and MAX functions against columns that can store numeric data. The example on the slide displays the average, highest, lowest, and sum of monthly salaries for all salespeople.

Using MIN and MAX Functions

You can use MIN and MAX for any datatype.

Using MIN , MAX for numeric data types:

SELECT min(sal) , max(sal),

FROM emp;

MIN(SAL) MAX(SAL)

Using MIN and MAX Functions

You can use MIN and MAX for char datatype.

Group Functions (continued) The following example displays the employee name that is first and the employee name that is the last in an alphabetized list of all employees.

SELECT MIN(ename), MAX(ename)

FROM emp ;

MIN(ENAME) MAX(ENAME)

ADAMS WARD

Using the COUNT Function

COUNT(*) returns the number of rows in a table.

SELECT COUNT(*)

FROM emp;

COUNT(*)

The COUNT Function The COUNT function has two formats:

• COUNT(*)

  • COUNT( expr )

COUNT(*) returns the number of rows in a table, including duplicate rows and rows containing null values in any of the columns.

If a WHERE clause is included in the SELECT statement COUNT(*) returns the number of rows that satisfies the condition in the WHERE clause.

In contrast. COUNT( expr ) returns the number of nonnull rows in the column identified by expr.

Using the COUNT Function

COUNT( expr ) returns the number of non NULL rows

satisfying expr in a table.

SELECT COUNT(comm)

FROM emp ;

COUNT(COMM)

The COUNT Function (continued) COUNT( expr ) returns the number of nonnull rows in the column identified by expr. The slide example displays the number of employees in departme'nt 30 who can earn a commission. Notice that the result gives the total number of rows to be four because two employees in department 30 cannot earn a commission and contain a null value in the COMM column.

SELECT COUNT(comm)

FROM emp

WHERE deptno = 30 ;

COUNT(COMM)

SELECT COUNT(comm) FROM emp WHERE deptno = 20 ;

COUNT(COMM)

SELECT COUNT(*)

FROM emp WHERE comm IS NULL ;

COUNT(*)

Group Functions and Null Values

Group functions ignore null values in the column.

SELECT AVG(comm)

FROM emp;

AVG(comm) 550

Group Functions and Null Values All group functions except COUNT (*) ignore null values in the column. In the slide example, the average is calculated based only on the rows in the table where a valid value is stored in the COMM column. The average is calculated as total commission being paid to all employees divided by the number of employees receiving commission (4).

Using the NVL Function with Group Functions

The NVL function forces group functions to include null

values.

SELECT AVG(NVL (comm,0))

FROM emp;

AVG(NVL(comm , 0)) 157,

Group Functions and Null Values (continued) The NVL function forces group functions to include null values. In the slide example, the average is calculated based on all rows in the table regardless of whether null values are stored in the COMM column. The average is calculated as total commission being paid to all employees divided by the total number of employees in the company (14).

Creating Groups of Data: GROUP BY Clause

SELECT column, group_function (column) FROM table [WHERE condition] [GROUP BY group by] [ORDER BY column];

Divide rows in a table into smaller groups by using the

GROUP BY clause.

The GROUP BY Clause

You can use the GROUP BY clause to divide the rows in a table into groups.

You can then use the group functions to return summon' information for each group.

In the syntax:

group by expression specifies columns whose values determine the basis for grouping rows

Guidelines

If you include a group function in a SELECT clause, you cannot select individual results as well unless the individual column appears in the GROUP BY clause. You will receive an error-message if you fail to include the column list.

Using a WHERE clause, you can preexclude rows before dividing them into groups.

You must include the columns in the GROUP BY clause.

You cannot use the column alias in the GROUP BY clause.

Using the GROUP BY Clause

All columns in the SELECT list that are not in group functions

must be in the GROUP BY clause

SELECT deptno, AVG(sal) FROM emp GROUP BY deptno ;

DEPTNO AVG(SAL)

The GROUP BY Clause (continued)

When using the GROUP BY clause, make sure that all columns in the SELECT list that are not in the group functions are included in the GROUP BY clause. The example on the slide displays the department number and the average salary for each department. Here is how this SELECT statement, containing a GROUP BY clause, is evaluated:

The SELECT clause specifies the columns to be retrieved:

Department number column in the EMP table

The average of all the salanes in the group you specified in the GROUP BY clause

The FROM clause specifies the tables that the database must access: the EMP table.

The WHERE clause specifies the rows to be retrieved. Since there is no WHERE clause, bv default all rows are retrieved.

Creating Groups of Data

SELECT deptno , AVG(sal)

FROM emp

GROUP BY deptno; DEPTNO AVG(SAL) 30 1 566, 20 2175 10 2916,

Using the GROUP BY Clause

All columns in the SELECT list that are not in group

functions must be in the GROUP BY clause.

SELECT deptno, AVG(sal)

FROM emp

GROUP BY deptno;

DEPTNO AVG(SAL)