SlideShare a Scribd company logo
5. Group Functions
REPORTING AGGREGATED DATA, USING GROUP
FUNCTIONS.
OBJECTIVES
At the end of this lesson, you will learn:
 How to identify the available group functions
 How to describe the use of group functions in
select statements
 Grouping data, by using the Group By clause and
 How to include or exclude grouped rows, by
using the having clause.
WHAT ARE GROUP FUNCTIONS?
 Group functions are SQL functions, which operate
on a GROUP of rows, and return a result.
 This group of rows may be an entire column or a
column, split into smaller groups.
EXAMPLE
 This example uses the GROUP function called COUNT, to
count the number of employees that earn a salary
from the company.
AVG & SUM
 The AVG and Sum keyword is used to find the
average and sum –respectively- of a group of
numbers.
EXERCISE
 Query your database for the average salary of all
the employees in the department with where
department_id=90.
5. Group Functions
MIN & MAX
 The MAX and the MIN functions, will display the
maximum and minimum –respectively- of all the
values in the specified group, or column.
EXAMPLE
STDDEV & VARIANCE
 STDDEV and VARIANCE are used to find the standard
deviation and the variance, of the numbers in the
specified column or group.
COUNT
 The COUNT function counts the number of rows in
the stated group or column. It has three different
variations but each performs the same function
of counting the number of rows in the group.
COUNT (COLUMN_NAME)
 This SQL statement COUNTs all the rows in the
stated column as returns their total number.
FORMAT:
SELECT COUNT (column_name)
FROM table_name;
EXAMPLE:
 This SQL statement COUNTs all the rows in the
employees table and returns the number of rows.
EXAMPLE:
 The SQL statement counts the number of rows in
the manager _id column.
COUNT DISTINCT (COLUMN_NAME)
 Where the same value occurs many times in a
column, the Oracle server COUNTS them all as one
value when the SELECT COUNT keyword is used.
5. Group Functions
 The Oracle server would count the different
values in the manager_id column instead of the
number of rows.
 From the result of this query, it is obvious, that all
the 107 employees, share only 18 manager-ids
 The DISTINCT keyword, when used with a group
function, will specify only the different rows
available in that group.
 The opposite of the DISTINCT keyword, is the ALL
keyword, and it operates on all the rows in the
group, including duplicates. Only null values are
exempted.
GROUPING DATA
 Initially we said group functions are functions
that operate on a group of rows.
 We also said these group of rows could be an
entire column.
 When we want to specify the order in which the
output of a GROUP function is processed, we use the
GROUP BY keyword.
EXAMPLE:
 The Oracle server goes to the department_id
column and fetches the distinct departments.
 Afterwards, the Oracle server comes to the salary
column and begins to group the rows in the
salary column by their various departments, and
then finally, for each different department_id, the
Oracle server returns the minimum salary.
 When using the GROUP BY clause, one general rule is
that GROUP BY is always followed by a column
name.
 No column alias can be used with the GROUP BY clause.
EXAMPLE:
NESTED GROUPS
The situation may demand a nested GROUP, also referred
to as a sub GROUP.
EXAMPLE:
 In the event of Nested GROUPS, the inner GROUP function
is SELECT before the outer GROUP function.
INCLUDING & EXCLUDING ROWS
 When restricting rows in a SELECT clause, we use the
WHERE keyword. However, when restricting rows in
a GROUP BY clause, we use the HAVING keyword.
 The GROUP BY clause will only return the rows that
meet the HAVING condition.
EXAMPLE:
 All the rows displayed by the Oracle server, have
a department_id column greater than 30.

More Related Content

PPT
1 - Introduction to PL/SQL
PPTX
Nested queries in database
PPT
Aggregating Data Using Group Functions
PPT
PL/SQL Introduction and Concepts
PPT
Including Constraints -Oracle Data base
PPTX
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
PPT
Aggregate functions
PPTX
SQL JOIN
1 - Introduction to PL/SQL
Nested queries in database
Aggregating Data Using Group Functions
PL/SQL Introduction and Concepts
Including Constraints -Oracle Data base
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
Aggregate functions
SQL JOIN

What's hot (20)

PDF
Exception handling in plsql
PPTX
joins in database
PPT
Sql operators & functions 3
PPT
Abstract class
PPT
Sql ppt
PPTX
basic structure of SQL FINAL.pptx
PPTX
Constructor overloading & method overloading
PPTX
Sql operator
PPTX
Member Function in C++
PPTX
Key and its different types
PPT
Sql group functions
PDF
Introduction to oracle functions
PDF
C++ chapter 1
PPTX
PDF
Relational algebra in dbms
PDF
Exception Handling
PPTX
Method overloading
PDF
Method Overloading In Java
PDF
SQL Functions and Operators
Exception handling in plsql
joins in database
Sql operators & functions 3
Abstract class
Sql ppt
basic structure of SQL FINAL.pptx
Constructor overloading & method overloading
Sql operator
Member Function in C++
Key and its different types
Sql group functions
Introduction to oracle functions
C++ chapter 1
Relational algebra in dbms
Exception Handling
Method overloading
Method Overloading In Java
SQL Functions and Operators
Ad

Similar to 5. Group Functions (20)

PPT
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
PDF
Group by clause mod
PPT
PDF
Writing Group Functions - DBMS
PPTX
ADVANCED MODELLING.pptx
PPT
PPT
PPT
PPT
PPTX
Lab3 aggregating data
PDF
Bt0075 rdbms with mysql 2
PPT
Sql query [select, sub] 4
PPT
Module03
PDF
Chapter9 more on database and sql
PPT
SQL select statement and functions
PPT
PPT
PDF
Oracle Advanced SQL and Analytic Functions
PDF
Oracle_Analytical_function.pdf
PPT
Advanced Sql Training
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
Group by clause mod
Writing Group Functions - DBMS
ADVANCED MODELLING.pptx
Lab3 aggregating data
Bt0075 rdbms with mysql 2
Sql query [select, sub] 4
Module03
Chapter9 more on database and sql
SQL select statement and functions
Oracle Advanced SQL and Analytic Functions
Oracle_Analytical_function.pdf
Advanced Sql Training
Ad

Recently uploaded (20)

PDF
TR - Agricultural Crops Production NC III.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Institutional Correction lecture only . . .
PPTX
Cell Structure & Organelles in detailed.
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
master seminar digital applications in india
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
01-Introduction-to-Information-Management.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Pre independence Education in Inndia.pdf
TR - Agricultural Crops Production NC III.pdf
VCE English Exam - Section C Student Revision Booklet
102 student loan defaulters named and shamed – Is someone you know on the list?
Anesthesia in Laparoscopic Surgery in India
Module 4: Burden of Disease Tutorial Slides S2 2025
PPH.pptx obstetrics and gynecology in nursing
Institutional Correction lecture only . . .
Cell Structure & Organelles in detailed.
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
master seminar digital applications in india
Renaissance Architecture: A Journey from Faith to Humanism
Final Presentation General Medicine 03-08-2024.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Computing-Curriculum for Schools in Ghana
01-Introduction-to-Information-Management.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Pre independence Education in Inndia.pdf

5. Group Functions

  • 2. REPORTING AGGREGATED DATA, USING GROUP FUNCTIONS.
  • 3. OBJECTIVES At the end of this lesson, you will learn:  How to identify the available group functions  How to describe the use of group functions in select statements  Grouping data, by using the Group By clause and  How to include or exclude grouped rows, by using the having clause.
  • 4. WHAT ARE GROUP FUNCTIONS?  Group functions are SQL functions, which operate on a GROUP of rows, and return a result.  This group of rows may be an entire column or a column, split into smaller groups.
  • 6.  This example uses the GROUP function called COUNT, to count the number of employees that earn a salary from the company. AVG & SUM  The AVG and Sum keyword is used to find the average and sum –respectively- of a group of numbers.
  • 7. EXERCISE  Query your database for the average salary of all the employees in the department with where department_id=90.
  • 9. MIN & MAX  The MAX and the MIN functions, will display the maximum and minimum –respectively- of all the values in the specified group, or column. EXAMPLE
  • 10. STDDEV & VARIANCE  STDDEV and VARIANCE are used to find the standard deviation and the variance, of the numbers in the specified column or group. COUNT  The COUNT function counts the number of rows in the stated group or column. It has three different variations but each performs the same function of counting the number of rows in the group.
  • 11. COUNT (COLUMN_NAME)  This SQL statement COUNTs all the rows in the stated column as returns their total number. FORMAT: SELECT COUNT (column_name) FROM table_name;
  • 13.  This SQL statement COUNTs all the rows in the employees table and returns the number of rows.
  • 15.  The SQL statement counts the number of rows in the manager _id column. COUNT DISTINCT (COLUMN_NAME)  Where the same value occurs many times in a column, the Oracle server COUNTS them all as one value when the SELECT COUNT keyword is used.
  • 17.  The Oracle server would count the different values in the manager_id column instead of the number of rows.  From the result of this query, it is obvious, that all the 107 employees, share only 18 manager-ids
  • 18.  The DISTINCT keyword, when used with a group function, will specify only the different rows available in that group.  The opposite of the DISTINCT keyword, is the ALL keyword, and it operates on all the rows in the group, including duplicates. Only null values are exempted.
  • 19. GROUPING DATA  Initially we said group functions are functions that operate on a group of rows.  We also said these group of rows could be an entire column.  When we want to specify the order in which the output of a GROUP function is processed, we use the GROUP BY keyword.
  • 21.  The Oracle server goes to the department_id column and fetches the distinct departments.  Afterwards, the Oracle server comes to the salary column and begins to group the rows in the salary column by their various departments, and then finally, for each different department_id, the Oracle server returns the minimum salary.
  • 22.  When using the GROUP BY clause, one general rule is that GROUP BY is always followed by a column name.  No column alias can be used with the GROUP BY clause. EXAMPLE:
  • 23. NESTED GROUPS The situation may demand a nested GROUP, also referred to as a sub GROUP. EXAMPLE:
  • 24.  In the event of Nested GROUPS, the inner GROUP function is SELECT before the outer GROUP function. INCLUDING & EXCLUDING ROWS  When restricting rows in a SELECT clause, we use the WHERE keyword. However, when restricting rows in a GROUP BY clause, we use the HAVING keyword.  The GROUP BY clause will only return the rows that meet the HAVING condition.
  • 26.  All the rows displayed by the Oracle server, have a department_id column greater than 30.