SlideShare a Scribd company logo
1 of 30
Aggregating Data  Using Group Functions
Objectives ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
What Are Group Functions? ,[object Object],EMPLOYEES The maximum  salary in  the  EMPLOYEES   table. …
Types of Group Functions ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Group Functions Syntax SELECT [ column ,]  group_function(column), ... FROM table [WHERE condition ] [GROUP BY column ] [ORDER BY column ];
Using the  AVG  and  SUM  Functions ,[object Object],SELECT AVG(salary), MAX(salary), MIN(salary), SUM(salary) FROM  employees WHERE  job_id LIKE '%REP%';
Using the  MIN  and  MAX  Functions ,[object Object],SELECT MIN(hire_date), MAX(hire_date) FROM   employees;
Using the  COUNT  Function ,[object Object],SELECT COUNT(*) FROM   employees WHERE  department_id = 50;
Using the  COUNT  Function ,[object Object],[object Object],SELECT COUNT(commission_pct) FROM  employees WHERE  department_id = 80;
Using the  DISTINCT  Keyword ,[object Object],[object Object],SELECT COUNT(DISTINCT department_id) FROM  employees;
Group Functions and Null Values ,[object Object],SELECT AVG(commission_pct) FROM  employees;
Using the  NVL  Function  with Group Functions ,[object Object],[object Object],SELECT AVG(NVL(commission_pct, 0)) FROM  employees;
Creating Groups of Data  EMPLOYEES The  average salary  in  EMPLOYEES table  for each  department. 4400 … 9500 3500 6400 10033
Creating Groups of Data:  The  GROUP BY  Clause Syntax ,[object Object],[object Object],SELECT column ,  group_function(column) FROM table [WHERE condition ] [GROUP BY group_by_expression ] [ORDER BY column ];
Using the  GROUP BY  Clause  ,[object Object],[object Object],SELECT  department_id, AVG(salary) FROM  employees GROUP BY department_id ;
Using the  GROUP BY  Clause  ,[object Object],[object Object],SELECT  AVG(salary) FROM  employees GROUP BY department_id ;
Grouping by More Than One Column EMPLOYEES “ Add up the  salaries in  the  EMPLOYEES table for each job,  grouped by  department. …
Using the  GROUP BY  Clause  on Multiple Columns SELECT  department_id dept_id, job_id, SUM(salary) FROM  employees GROUP BY department_id, job_id ;
Illegal Queries  Using Group Functions ,[object Object],[object Object],[object Object],SELECT department_id, COUNT(last_name) FROM  employees; SELECT department_id, COUNT(last_name) * ERROR at line 1: ORA-00937: not a single-group group function Column missing in the  GROUP BY  clause
Illegal Queries  Using Group Functions ,[object Object],[object Object],[object Object],SELECT  department_id, AVG(salary) FROM  employees WHERE  AVG(salary) > 8000 GROUP BY department_id; WHERE  AVG(salary) > 8000 * ERROR at line 3: ORA-00934: group function is not allowed here Cannot use the  WHERE  clause to restrict groups
Excluding Group Results The maximum salary per department when it is greater than $10,000 EMPLOYEES …
Excluding Group Results: The  HAVING  Clause ,[object Object],[object Object],[object Object],[object Object],SELECT column ,  group_function FROM table [WHERE condition ] [GROUP BY group_by_expression ] [HAVING group_condition ] [ORDER BY column ];
Using the  HAVING  Clause SELECT  department_id, MAX(salary) FROM  employees GROUP BY department_id HAVING  MAX(salary)>10000 ;
Using the  HAVING  Clause SELECT  job_id, SUM(salary) PAYROLL FROM  employees WHERE  job_id NOT LIKE '%REP%' GROUP BY job_id HAVING  SUM(salary) > 13000 ORDER BY SUM(salary);
Nesting Group Functions ,[object Object],SELECT  MAX(AVG(salary)) FROM  employees GROUP BY department_id;
Summary ,[object Object],[object Object],[object Object],[object Object],SELECT column ,  group_function(column) FROM table [WHERE condition ] [GROUP BY group_by_expression ] [HAVING group_condition ] [ORDER BY column ];
Practice 5 Overview ,[object Object],[object Object],[object Object],[object Object]
 
 
 

More Related Content

What's hot (20)

Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
 
Les02
Les02Les02
Les02
 
Les06
Les06Les06
Les06
 
Dynamic websites lec2
Dynamic websites lec2Dynamic websites lec2
Dynamic websites lec2
 
Les11
Les11Les11
Les11
 
Module03
Module03Module03
Module03
 
Les15
Les15Les15
Les15
 
Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)
 
Les07
Les07Les07
Les07
 
SQL WORKSHOP::Lecture 7
SQL WORKSHOP::Lecture 7SQL WORKSHOP::Lecture 7
SQL WORKSHOP::Lecture 7
 
Sql task
Sql taskSql task
Sql task
 
Les08
Les08Les08
Les08
 
Les04
Les04Les04
Les04
 
Les07
Les07Les07
Les07
 
Using the set operators
Using the set operatorsUsing the set operators
Using the set operators
 
Les02
Les02Les02
Les02
 
Dubles de teste
Dubles de testeDubles de teste
Dubles de teste
 
Using subqueries to solve queries
Using subqueries to solve queriesUsing subqueries to solve queries
Using subqueries to solve queries
 
Les04
Les04Les04
Les04
 
Les07 (using the set operators)
Les07 (using the set operators)Les07 (using the set operators)
Les07 (using the set operators)
 

Viewers also liked (8)

Les09
Les09Les09
Les09
 
Les03
Les03Les03
Les03
 
Les06
Les06Les06
Les06
 
Les19
Les19Les19
Les19
 
Les20
Les20Les20
Les20
 
Advanced functions in PL SQL
Advanced functions in PL SQLAdvanced functions in PL SQL
Advanced functions in PL SQL
 
Oracle sql developer_slides
Oracle sql developer_slidesOracle sql developer_slides
Oracle sql developer_slides
 
20 Ideas for your Website Homepage Content
20 Ideas for your Website Homepage Content20 Ideas for your Website Homepage Content
20 Ideas for your Website Homepage Content
 

Similar to Les05

e computer notes - Enhancements to the group by clause
e computer notes - Enhancements to the group by clausee computer notes - Enhancements to the group by clause
e computer notes - Enhancements to the group by clause
ecomputernotes
 

Similar to Les05 (20)

Writing Group Functions - DBMS
Writing Group Functions - DBMSWriting Group Functions - DBMS
Writing Group Functions - DBMS
 
Lab3 aggregating data
Lab3   aggregating dataLab3   aggregating data
Lab3 aggregating data
 
Les04
Les04Les04
Les04
 
Group by clause mod
Group by clause modGroup by clause mod
Group by clause mod
 
5. Group Functions
5. Group Functions5. Group Functions
5. Group Functions
 
Consultas con agrupaci¾n de datos
Consultas con agrupaci¾n de datosConsultas con agrupaci¾n de datos
Consultas con agrupaci¾n de datos
 
Aggregating Data Using Group Functions
Aggregating Data Using Group FunctionsAggregating Data Using Group Functions
Aggregating Data Using Group Functions
 
PHP mysql Aggregate functions
PHP mysql Aggregate functionsPHP mysql Aggregate functions
PHP mysql Aggregate functions
 
Oracle SQL - Aggregating Data Les 05.ppt
Oracle SQL - Aggregating Data Les 05.pptOracle SQL - Aggregating Data Les 05.ppt
Oracle SQL - Aggregating Data Les 05.ppt
 
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
 
Les06
Les06Les06
Les06
 
Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
 
SQL.pptx
SQL.pptxSQL.pptx
SQL.pptx
 
e computer notes - Enhancements to the group by clause
e computer notes - Enhancements to the group by clausee computer notes - Enhancements to the group by clause
e computer notes - Enhancements to the group by clause
 
Sql5
Sql5Sql5
Sql5
 
Clauses
ClausesClauses
Clauses
 
SQL5.ppt
SQL5.pptSQL5.ppt
SQL5.ppt
 
Les03
Les03Les03
Les03
 
Sql query [select, sub] 4
Sql query [select, sub] 4Sql query [select, sub] 4
Sql query [select, sub] 4
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 

Les05

  • 1. Aggregating Data Using Group Functions
  • 2.
  • 3.
  • 4.
  • 5. Group Functions Syntax SELECT [ column ,] group_function(column), ... FROM table [WHERE condition ] [GROUP BY column ] [ORDER BY column ];
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13. Creating Groups of Data EMPLOYEES The average salary in EMPLOYEES table for each department. 4400 … 9500 3500 6400 10033
  • 14.
  • 15.
  • 16.
  • 17. Grouping by More Than One Column EMPLOYEES “ Add up the salaries in the EMPLOYEES table for each job, grouped by department. …
  • 18. Using the GROUP BY Clause on Multiple Columns SELECT department_id dept_id, job_id, SUM(salary) FROM employees GROUP BY department_id, job_id ;
  • 19.
  • 20.
  • 21. Excluding Group Results The maximum salary per department when it is greater than $10,000 EMPLOYEES …
  • 22.
  • 23. Using the HAVING Clause SELECT department_id, MAX(salary) FROM employees GROUP BY department_id HAVING MAX(salary)>10000 ;
  • 24. Using the HAVING Clause SELECT job_id, SUM(salary) PAYROLL FROM employees WHERE job_id NOT LIKE '%REP%' GROUP BY job_id HAVING SUM(salary) > 13000 ORDER BY SUM(salary);
  • 25.
  • 26.
  • 27.
  • 28.  
  • 29.  
  • 30.  

Editor's Notes

  1. Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice 75 minutes Total