SlideShare a Scribd company logo
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

More Related Content

What's hot

Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
sinhacp
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
DataminingTools Inc
 
SQL subquery
SQL subquerySQL subquery
SQL subquery
Vikas Gupta
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
baabtra.com - No. 1 supplier of quality freshers
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
Nitesh Singh
 
Oracle views
Oracle viewsOracle views
Oracle views
Balqees Al.Mubarak
 
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
Umar Ali
 
Functions oracle (pl/sql)
Functions oracle (pl/sql)Functions oracle (pl/sql)
Functions oracle (pl/sql)harman kaur
 
ADVANCE SQL-"Sub queries"
ADVANCE SQL-"Sub queries"ADVANCE SQL-"Sub queries"
ADVANCE SQL-"Sub queries"
Ankit Surti
 
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
Sarfaraz Ghanta
 
Writing Group Functions - DBMS
Writing Group Functions - DBMSWriting Group Functions - DBMS
Writing Group Functions - DBMS
Solaiman Hridoy
 
Subqueries
SubqueriesSubqueries
e computer notes - Subqueries
e computer notes - Subqueriese computer notes - Subqueries
e computer notes - Subqueriesecomputernotes
 
Sql insert statement
Sql insert statementSql insert statement
Sql insert statementVivek Singh
 
Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)
Achmad Solichin
 
Les06
Les06Les06

What's hot (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
 

Similar to Lab3 aggregating data

Les04
Les04Les04
Les04
Les04Les04
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
Syed Zaid Irshad
 
MySQL-commands.pdf
MySQL-commands.pdfMySQL-commands.pdf
MySQL-commands.pdf
ssuserc5aa74
 
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 functionsecomputernotes
 
SQL5.ppt
SQL5.pptSQL5.ppt
SQL5.ppt
ayeshaasmat4
 
Clauses in sql server
Clauses in sql serverClauses in sql server
Clauses in sql server
Naveen Kumar Veligeti
 
SQL.pptx
SQL.pptxSQL.pptx
SQL.pptx
MrHello6
 
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)
Ehtisham Ali
 
Day-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptxDay-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptx
uzmasulthana3
 
MYSQL GROUP FUNCTION.pptx
MYSQL GROUP FUNCTION.pptxMYSQL GROUP FUNCTION.pptx
MYSQL GROUP FUNCTION.pptx
kumarkaushal17
 
Bt0075 rdbms with mysql 2
Bt0075 rdbms with mysql 2Bt0075 rdbms with mysql 2
Bt0075 rdbms with mysql 2
Techglyphs
 

Similar to 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
 

More from Balqees Al.Mubarak

Using scripts
Using scriptsUsing scripts
Using scripts
Balqees Al.Mubarak
 
Single row functions
Single row functionsSingle row functions
Single row functions
Balqees Al.Mubarak
 
Sequences and indexes
Sequences and indexesSequences and indexes
Sequences and indexes
Balqees Al.Mubarak
 
Lab2 ddl commands
Lab2 ddl commandsLab2 ddl commands
Lab2 ddl commands
Balqees Al.Mubarak
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
Balqees Al.Mubarak
 

More from 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
 

Recently uploaded

How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 

Recently uploaded (20)

How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 

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