SlideShare une entreprise Scribd logo
1  sur  26
Télécharger pour lire hors ligne
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.

Contenu connexe

Tendances

Tendances (20)

database language ppt.pptx
database language ppt.pptxdatabase language ppt.pptx
database language ppt.pptx
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
Mysql
MysqlMysql
Mysql
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
4. plsql
4. plsql4. plsql
4. plsql
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
 
Sql and Sql commands
Sql and Sql commandsSql and Sql commands
Sql and Sql commands
 
Integrity constraints in dbms
Integrity constraints in dbmsIntegrity constraints in dbms
Integrity constraints in dbms
 
Sql commands
Sql commandsSql commands
Sql commands
 
SQL select clause
SQL select clauseSQL select clause
SQL select clause
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
 
SQL commands
SQL commandsSQL commands
SQL commands
 
Triggers in SQL | Edureka
Triggers in SQL | EdurekaTriggers in SQL | Edureka
Triggers in SQL | Edureka
 
Sql Basics | Edureka
Sql Basics | EdurekaSql Basics | Edureka
Sql Basics | Edureka
 
DATABASE CONSTRAINTS
DATABASE CONSTRAINTSDATABASE CONSTRAINTS
DATABASE CONSTRAINTS
 
SQL subquery
SQL subquerySQL subquery
SQL subquery
 
Relational algebra in dbms
Relational algebra in dbmsRelational algebra in dbms
Relational algebra in dbms
 
Create table
Create tableCreate table
Create table
 

Similaire à 5. Group Functions

Similaire à 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
 
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
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 

Dernier

Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Dernier (20)

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 

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.