SlideShare a Scribd company logo
1 of 26
Download to read offline
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.
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.
 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

What's hot (20)

SQL BASIC QUERIES
SQL  BASIC QUERIES SQL  BASIC QUERIES
SQL BASIC QUERIES
 
Asp objects
Asp objectsAsp objects
Asp objects
 
UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES
UNIT II 	LINEAR DATA STRUCTURES – STACKS, QUEUES	UNIT II 	LINEAR DATA STRUCTURES – STACKS, QUEUES
UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES
 
Fundamentals of Database ppt ch03
Fundamentals of Database ppt ch03Fundamentals of Database ppt ch03
Fundamentals of Database ppt ch03
 
Normalization
NormalizationNormalization
Normalization
 
Database Management System
Database Management SystemDatabase Management System
Database Management System
 
Database Keys & Relationship
Database Keys & RelationshipDatabase Keys & Relationship
Database Keys & Relationship
 
Computer Science-Data Structures :Abstract DataType (ADT)
Computer Science-Data Structures :Abstract DataType (ADT)Computer Science-Data Structures :Abstract DataType (ADT)
Computer Science-Data Structures :Abstract DataType (ADT)
 
Heap tree
Heap treeHeap tree
Heap tree
 
Dbms
DbmsDbms
Dbms
 
Data structures
Data structuresData structures
Data structures
 
Fundamentals of DBMS
Fundamentals of DBMSFundamentals of DBMS
Fundamentals of DBMS
 
relational algebra Tuple Relational Calculus - database management system
relational algebra Tuple Relational Calculus - database management systemrelational algebra Tuple Relational Calculus - database management system
relational algebra Tuple Relational Calculus - database management system
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
 
Join
JoinJoin
Join
 
R data types
R   data typesR   data types
R data types
 
06.01 sql select distinct
06.01 sql select distinct06.01 sql select distinct
06.01 sql select distinct
 

Similar to 5. Group Functions

Similar to 5. Group Functions (20)

Introduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek SharmaIntroduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
 
Group by clause mod
Group by clause modGroup by clause mod
Group by clause mod
 
Les05
Les05Les05
Les05
 
Writing Group Functions - DBMS
Writing Group Functions - DBMSWriting Group Functions - DBMS
Writing Group Functions - DBMS
 
ADVANCED MODELLING.pptx
ADVANCED MODELLING.pptxADVANCED MODELLING.pptx
ADVANCED MODELLING.pptx
 
Les04
Les04Les04
Les04
 
Les04
Les04Les04
Les04
 
Les17
Les17Les17
Les17
 
Les04
Les04Les04
Les04
 
Lab3 aggregating data
Lab3   aggregating dataLab3   aggregating data
Lab3 aggregating data
 
Bt0075 rdbms with mysql 2
Bt0075 rdbms with mysql 2Bt0075 rdbms with mysql 2
Bt0075 rdbms with mysql 2
 
Sql query [select, sub] 4
Sql query [select, sub] 4Sql query [select, sub] 4
Sql query [select, sub] 4
 
Module03
Module03Module03
Module03
 
Chapter9 more on database and sql
Chapter9 more on database and sqlChapter9 more on database and sql
Chapter9 more on database and sql
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 
Les06
Les06Les06
Les06
 
Les18
Les18Les18
Les18
 
Oracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic FunctionsOracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic Functions
 
Oracle_Analytical_function.pdf
Oracle_Analytical_function.pdfOracle_Analytical_function.pdf
Oracle_Analytical_function.pdf
 

Recently uploaded

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 

Recently uploaded (20)

Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 

5. Group Functions

  • 1.
  • 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.
  • 8.
  • 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.
  • 16.
  • 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.