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

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 Aggregating and Grouping Data

Similar to Aggregating and Grouping Data (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

Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 

Recently uploaded (20)

Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 

Aggregating and Grouping Data

  • 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