SlideShare una empresa de Scribd logo
1 de 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]
 
 
 

Más contenido relacionado

La actualidad más candente

La actualidad más candente (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)
 

Destacado

Destacado (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 a Les05

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

Último

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
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 educationjfdjdjcjdnsjd
 
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 2024The Digital Insurer
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
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 AutomationSafe Software
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
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...Miguel Araújo
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
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)wesley chun
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 

Último (20)

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
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
 
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
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
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...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
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)
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 

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.  

Notas del editor

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