SlideShare une entreprise Scribd logo
1  sur  22
AGGREGATING DATA
USING GROUP FUNCTIONS
Structured Query Language
Group Functions
What are group 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.
“maximum
salary in
the EMP table”
Types of Group Functions
• AVG
• COUNT
• MAX
• MIN
• SUM
Function Description
AVG(DISTINCT|ALL|n) Average value of n,
ignoring null values
COUNT({*|DISTINCT|ALL|expr}) Number of rows, where
expr evaluates to
something other than null
(Count all rows using *,
including duplicates and
rows with nulls)
MAX(DISTINCT|ALL|expr) Maximum value of expr,
ignoring null values
MIN(DISTINCT|ALL|expr) Minimum value of expr,
ignoring null values
SUM(DISTINCT|ALL|n) Sum values of n, ignoring
null values
Using Group Functions
 Syntax:
SELECT [column, ] group_function(column)
FROM table
[WHERE condition]
[GROUP BY column]
[ORDER BY column];
Using AVG and SUM Functions
 We can use AVG, SUM, MIN, and MAX functions against
columns that can store numeric data. The example displays
the average, highest, lowest, and sum of monthly salaries
for all salespeople.
Using MIN and MAX Functions
 We can use MIN and MAX for any data type.
Using COUNT Function
 COUNT(*) returns the number of rows in a table
 COUNT(expr) returns the number of non-null rows.
Using COUNT Function (continued)
 Display the number of departments in the EMP
table.
 Display the number of distinct departments in the
EMP table.
Creating Groups of Data
 We have used the group functions on the tables as one large
group of information. At times we need to divide the table
into smaller groups. This can be done by using GROUP BY
clause.
EMP
“average
salary in
EMP table
for each
department”
2916.6667
2175
1566.6667
Creating Groups of Data:
GROUP BY Clause
 Divide rows in a table into smaller groups by using th
GROUP BY clause.
SELECT [column, ] group_function(column)
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[ORDER BY column];
 group_by_expression specifies columns whose values
determine the basis for grouping
rows.
Using the GROUP BY Clause
 All columns int the SELECT list that are not in group
functions must by in the GROUP BY clause.
 The GROUP BY column does not have to be in the
SELECT list.
Grouping by More Than One Column
EMP
“sum salaries in
the EMP table
for each job,
grouped by
department”
• Display the total salary being paid to each job title,
within each department.
Using the GROUP BY Clause
on Multiple Columns
• Display the total salary being paid to each job title,
within each department.
Illegal Queries
Using Group Functions
 Any column or expression in the SELECT list that is
not an aggregate function, must be in the GROUP BY
clause.
 Display the number of employees in each
department.
Illegal Queries
Using Group Functions
 We cannot use the WHERE clause to restrict groups.
 We use the HAVING clause to restrict groups.
 Display the average salaries of those departments
that have an average salary greater than $2000.
Excluding Group Results
 In the same way that we use the WHERE clause to restrict
rows that we select, we use the HAVING clause to restrict
groups.
 To find the maximum salary of each department, but show
only the departments that have maximum salary more than
$2900, we need to do the following:
 Find the maximum salary for each department by grouping by
department number.
 Restrict the groups to those departments with a maximum salary
greater than $2900.
Excluding Group Results
EMP
5000
3000
2850
“maximum salary
per department
greater than $2900”
Excluding Group Results:
Having Clause
 Use the HAVING clause to restrict groups
 Rows are grouped.
 The group function is applied.
 Groups matching the HAVING clause are displayed.
SELECT column, group_function
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BY column];
Using the HAVING Clause
 Display department numbers and maximum salary
for those department whose maximum salary is
greater than $2900.
 Display the job title and total monthly salary for each
job title with a total payroll exceeding $5000.
Exclude salespeople and sort the list by the total
monthly salary.
Using the HAVING Clause
Nesting Group Functions
 Display the maximum average salary.
Summary
SELECT column, group_function(column)
FROM table
[WHERE condition]
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BY column];
Order of evaluation of the clauses:
 WHERE clause
 GROUP BY clause
 HAVING clause

Contenu connexe

Tendances (18)

Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
 
SQL subquery
SQL subquerySQL subquery
SQL subquery
 
Subqueries
SubqueriesSubqueries
Subqueries
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
 
Oracle views
Oracle viewsOracle views
Oracle views
 
Difference between group by and order by in sql server
Difference between group by and  order by in sql serverDifference between group by and  order by in sql server
Difference between group by and order by in sql server
 
Functions oracle (pl/sql)
Functions oracle (pl/sql)Functions oracle (pl/sql)
Functions oracle (pl/sql)
 
Sql subquery
Sql subquerySql subquery
Sql subquery
 
ADVANCE SQL-"Sub queries"
ADVANCE SQL-"Sub queries"ADVANCE SQL-"Sub queries"
ADVANCE SQL-"Sub queries"
 
Sub query example with advantage and disadvantages
Sub query example with advantage and disadvantagesSub query example with advantage and disadvantages
Sub query example with advantage and disadvantages
 
Writing Group Functions - DBMS
Writing Group Functions - DBMSWriting Group Functions - DBMS
Writing Group Functions - DBMS
 
Subqueries
SubqueriesSubqueries
Subqueries
 
e computer notes - Subqueries
e computer notes - Subqueriese computer notes - Subqueries
e computer notes - Subqueries
 
Sql insert statement
Sql insert statementSql insert statement
Sql insert statement
 
Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)
 
Les06
Les06Les06
Les06
 

Similaire à Lab3 aggregating data

Similaire à Lab3 aggregating data (20)

Les05
Les05Les05
Les05
 
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
 
Les04
Les04Les04
Les04
 
Les04
Les04Les04
Les04
 
Reporting aggregated data using the group functions
Reporting aggregated data using the group functionsReporting aggregated data using the group functions
Reporting aggregated data using the group functions
 
Sql5
Sql5Sql5
Sql5
 
MySQL-commands.pdf
MySQL-commands.pdfMySQL-commands.pdf
MySQL-commands.pdf
 
e computer notes - Aggregating data using group functions
e computer notes -  Aggregating data using group functionse computer notes -  Aggregating data using group functions
e computer notes - Aggregating data using group functions
 
SQL5.ppt
SQL5.pptSQL5.ppt
SQL5.ppt
 
Clauses in sql server
Clauses in sql serverClauses in sql server
Clauses in sql server
 
SQL.pptx
SQL.pptxSQL.pptx
SQL.pptx
 
Module03
Module03Module03
Module03
 
Sql server ___________session_10(group by clause)
Sql server  ___________session_10(group by clause)Sql server  ___________session_10(group by clause)
Sql server ___________session_10(group by clause)
 
Day-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptxDay-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptx
 
Les04
Les04Les04
Les04
 
Les17
Les17Les17
Les17
 
MYSQL GROUP FUNCTION.pptx
MYSQL GROUP FUNCTION.pptxMYSQL GROUP FUNCTION.pptx
MYSQL GROUP FUNCTION.pptx
 
Sql query [select, sub] 4
Sql query [select, sub] 4Sql query [select, sub] 4
Sql query [select, sub] 4
 
Les04
Les04Les04
Les04
 
Bt0075 rdbms with mysql 2
Bt0075 rdbms with mysql 2Bt0075 rdbms with mysql 2
Bt0075 rdbms with mysql 2
 

Plus de Balqees Al.Mubarak

Plus de Balqees Al.Mubarak (6)

Using scripts
Using scriptsUsing scripts
Using scripts
 
Single row functions
Single row functionsSingle row functions
Single row functions
 
Sequences and indexes
Sequences and indexesSequences and indexes
Sequences and indexes
 
Lab2 ddl commands
Lab2 ddl commandsLab2 ddl commands
Lab2 ddl commands
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
 
Using social network sites
Using social network sites Using social network sites
Using social network sites
 

Dernier

GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxElton John Embodo
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsRommel Regala
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 

Dernier (20)

GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docx
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World Politics
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 

Lab3 aggregating data

  • 1. AGGREGATING DATA USING GROUP FUNCTIONS Structured Query Language
  • 2. Group Functions What are group 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. “maximum salary in the EMP table”
  • 3. Types of Group Functions • AVG • COUNT • MAX • MIN • SUM Function Description AVG(DISTINCT|ALL|n) Average value of n, ignoring null values COUNT({*|DISTINCT|ALL|expr}) Number of rows, where expr evaluates to something other than null (Count all rows using *, including duplicates and rows with nulls) MAX(DISTINCT|ALL|expr) Maximum value of expr, ignoring null values MIN(DISTINCT|ALL|expr) Minimum value of expr, ignoring null values SUM(DISTINCT|ALL|n) Sum values of n, ignoring null values
  • 4. Using Group Functions  Syntax: SELECT [column, ] group_function(column) FROM table [WHERE condition] [GROUP BY column] [ORDER BY column];
  • 5. Using AVG and SUM Functions  We can use AVG, SUM, MIN, and MAX functions against columns that can store numeric data. The example displays the average, highest, lowest, and sum of monthly salaries for all salespeople.
  • 6. Using MIN and MAX Functions  We can use MIN and MAX for any data type.
  • 7. Using COUNT Function  COUNT(*) returns the number of rows in a table  COUNT(expr) returns the number of non-null rows.
  • 8. Using COUNT Function (continued)  Display the number of departments in the EMP table.  Display the number of distinct departments in the EMP table.
  • 9. Creating Groups of Data  We have used the group functions on the tables as one large group of information. At times we need to divide the table into smaller groups. This can be done by using GROUP BY clause. EMP “average salary in EMP table for each department” 2916.6667 2175 1566.6667
  • 10. Creating Groups of Data: GROUP BY Clause  Divide rows in a table into smaller groups by using th GROUP BY clause. SELECT [column, ] group_function(column) FROM table [WHERE condition] [GROUP BY group_by_expression] [ORDER BY column];  group_by_expression specifies columns whose values determine the basis for grouping rows.
  • 11. Using the GROUP BY Clause  All columns int the SELECT list that are not in group functions must by in the GROUP BY clause.  The GROUP BY column does not have to be in the SELECT list.
  • 12. Grouping by More Than One Column EMP “sum salaries in the EMP table for each job, grouped by department” • Display the total salary being paid to each job title, within each department.
  • 13. Using the GROUP BY Clause on Multiple Columns • Display the total salary being paid to each job title, within each department.
  • 14. Illegal Queries Using Group Functions  Any column or expression in the SELECT list that is not an aggregate function, must be in the GROUP BY clause.  Display the number of employees in each department.
  • 15. Illegal Queries Using Group Functions  We cannot use the WHERE clause to restrict groups.  We use the HAVING clause to restrict groups.  Display the average salaries of those departments that have an average salary greater than $2000.
  • 16. Excluding Group Results  In the same way that we use the WHERE clause to restrict rows that we select, we use the HAVING clause to restrict groups.  To find the maximum salary of each department, but show only the departments that have maximum salary more than $2900, we need to do the following:  Find the maximum salary for each department by grouping by department number.  Restrict the groups to those departments with a maximum salary greater than $2900.
  • 17. Excluding Group Results EMP 5000 3000 2850 “maximum salary per department greater than $2900”
  • 18. Excluding Group Results: Having Clause  Use the HAVING clause to restrict groups  Rows are grouped.  The group function is applied.  Groups matching the HAVING clause are displayed. SELECT column, group_function FROM table [WHERE condition] [GROUP BY group_by_expression] [HAVING group_condition] [ORDER BY column];
  • 19. Using the HAVING Clause  Display department numbers and maximum salary for those department whose maximum salary is greater than $2900.
  • 20.  Display the job title and total monthly salary for each job title with a total payroll exceeding $5000. Exclude salespeople and sort the list by the total monthly salary. Using the HAVING Clause
  • 21. Nesting Group Functions  Display the maximum average salary.
  • 22. Summary SELECT column, group_function(column) FROM table [WHERE condition] [GROUP BY group_by_expression] [HAVING group_condition] [ORDER BY column]; Order of evaluation of the clauses:  WHERE clause  GROUP BY clause  HAVING clause