SlideShare una empresa de Scribd logo
1 de 23
Aggregate Functions
1Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh
Aggregate Functions
 ISO standard defines five aggregate functions:
1. COUNT returns number of values in specified
column.
2. SUM returns sum of values in specified column.
3. AVG returns average of values in specified
column.
4. MIN returns smallest value in specified column.
5. MAX returns largest value in specified column.
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 2
Things to remember
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 3
Count Function
 The COUNT() function returns the number of rows that
matches a specified criteria.
 The COUNT(column_name) function returns the number of
values (NULL values will not be counted) of the specified
column:
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 4
Count Function
 Now we want to count the number of orders from "Customer
Nilsen".
 We use the following SQL statement:
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 5
Count Function
 The COUNT(*) function returns the number of records in a
table:
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 6
Count Function
which is the total number of rows in the table.
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 7
Count Function
 The COUNT(DISTINCT column_name) function returns the
number of distinct values of the specified column:
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 8
Count Function
which is the number of unique customers (Hansen, Nilsen, and
Jensen) in the "Orders" table.
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 9
Sum Function
 Now we want to find the sum of all "OrderPrice" fields".
 We use the following SQL statement:
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 10
Avg Function
 Now we want to find the average value of the "OrderPrice"
fields.
 We use the following SQL statement:
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 11
Max Function
 Now we want to find the largest value of the "OrderPrice"
column.
 We use the following SQL statement:
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 12
Min Function
 Now we want to find the smallest value of the "OrderPrice"
column.
 We use the following SQL statement:
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 13
Group By Clause
 The GROUP BY statement is used in conjunction with the
aggregate functions to group the result-set by one or more
columns.
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 14
Group By Clause
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 15
 Now we want to find the total sum (total order) of each
customer.
 We will have to use the GROUP BY statement to group the
customers.
 We use the following SQL statement:
Group By Clause
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 16
Group By Clause
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 17
 If we omit the group by clause from the statement-
Things to remember
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 18
Having Clause
 The HAVING clause was added to SQL because the WHERE
keyword could not be used with aggregate functions.
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 19
Having Clause
 Now we want to find if any of the customers have a total
order of less than 2000.
 We use the following SQL statement:
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 20
Having Clause
 Now we want to find if the customers "Hansen" or "Jensen"
have a total order of more than 1500.
 We add an ordinary WHERE clause to the SQL statement:
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 21
Things to remember
 A HAVING condition can refer only to an
expression in the SELECT list, or to an
expression involving an aggregate function.
 If you specify an expression in the HAVING
clause that isn't in the SELECT list, or that
isn't an aggregate expression, you will get
an error.
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 22
Reference
 World Wide Web Schools,
http://www.w3schools.com/sql/sql_functions.asp
[05/04/2009]
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 23

Más contenido relacionado

Similar a L5 aggregate functions

SQA server performance tuning
SQA server performance tuningSQA server performance tuning
SQA server performance tuning
Duy Tan Geek
 
Lec 10. Functions (Part II)
Lec 10. Functions (Part II)Lec 10. Functions (Part II)
Lec 10. Functions (Part II)
Rushdi Shams
 
Lec 09. Introduction to Functions / Call by Values
Lec 09. Introduction to Functions / Call by ValuesLec 09. Introduction to Functions / Call by Values
Lec 09. Introduction to Functions / Call by Values
Rushdi Shams
 
CIS 515 Discussion post responses.Respondto the colleagu.docx
CIS 515 Discussion post responses.Respondto the colleagu.docxCIS 515 Discussion post responses.Respondto the colleagu.docx
CIS 515 Discussion post responses.Respondto the colleagu.docx
sleeperharwell
 
Module 3 Part I - Bk1 Chapter 07.ppt
Module 3 Part I - Bk1 Chapter 07.pptModule 3 Part I - Bk1 Chapter 07.ppt
Module 3 Part I - Bk1 Chapter 07.ppt
KusumaS36
 

Similar a L5 aggregate functions (20)

SQA server performance tuning
SQA server performance tuningSQA server performance tuning
SQA server performance tuning
 
Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptx
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
 
Lec 10. Functions (Part II)
Lec 10. Functions (Part II)Lec 10. Functions (Part II)
Lec 10. Functions (Part II)
 
L6 views
L6  viewsL6  views
L6 views
 
Lec 09. Introduction to Functions / Call by Values
Lec 09. Introduction to Functions / Call by ValuesLec 09. Introduction to Functions / Call by Values
Lec 09. Introduction to Functions / Call by Values
 
Sql
SqlSql
Sql
 
MySQL Performance Optimization
MySQL Performance OptimizationMySQL Performance Optimization
MySQL Performance Optimization
 
Advanced MySQL Query Optimizations
Advanced MySQL Query OptimizationsAdvanced MySQL Query Optimizations
Advanced MySQL Query Optimizations
 
Intro to SQL for Beginners
Intro to SQL for BeginnersIntro to SQL for Beginners
Intro to SQL for Beginners
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
12. Basic SQL Queries (2).pptx
12. Basic SQL Queries  (2).pptx12. Basic SQL Queries  (2).pptx
12. Basic SQL Queries (2).pptx
 
CIS 515 Discussion post responses.Respondto the colleagu.docx
CIS 515 Discussion post responses.Respondto the colleagu.docxCIS 515 Discussion post responses.Respondto the colleagu.docx
CIS 515 Discussion post responses.Respondto the colleagu.docx
 
Module 3 Part I - Bk1 Chapter 07.ppt
Module 3 Part I - Bk1 Chapter 07.pptModule 3 Part I - Bk1 Chapter 07.ppt
Module 3 Part I - Bk1 Chapter 07.ppt
 
MySQL: Indexing for Better Performance
MySQL: Indexing for Better PerformanceMySQL: Indexing for Better Performance
MySQL: Indexing for Better Performance
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
Ch 3.pdf
Ch 3.pdfCh 3.pdf
Ch 3.pdf
 

Más de Rushdi Shams

Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translation
Rushdi Shams
 
L1 l2 l3 introduction to machine translation
L1 l2 l3  introduction to machine translationL1 l2 l3  introduction to machine translation
L1 l2 l3 introduction to machine translation
Rushdi Shams
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semantics
Rushdi Shams
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
Rushdi Shams
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logic
Rushdi Shams
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structure
Rushdi Shams
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representation
Rushdi Shams
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hacking
Rushdi Shams
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)
Rushdi Shams
 

Más de Rushdi Shams (20)

Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better Research
 
Common evaluation measures in NLP and IR
Common evaluation measures in NLP and IRCommon evaluation measures in NLP and IR
Common evaluation measures in NLP and IR
 
Machine learning with nlp 101
Machine learning with nlp 101Machine learning with nlp 101
Machine learning with nlp 101
 
Semi-supervised classification for natural language processing
Semi-supervised classification for natural language processingSemi-supervised classification for natural language processing
Semi-supervised classification for natural language processing
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: Parsing
 
Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translation
 
L1 l2 l3 introduction to machine translation
L1 l2 l3  introduction to machine translationL1 l2 l3  introduction to machine translation
L1 l2 l3 introduction to machine translation
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semantics
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logic
 
L15 fuzzy logic
L15  fuzzy logicL15  fuzzy logic
L15 fuzzy logic
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structure
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representation
 
First order logic
First order logicFirst order logic
First order logic
 
Belief function
Belief functionBelief function
Belief function
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hacking
 
L4 vpn
L4  vpnL4  vpn
L4 vpn
 
L3 defense
L3  defenseL3  defense
L3 defense
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)
 
L1 phishing
L1  phishingL1  phishing
L1 phishing
 

Último

Último (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 

L5 aggregate functions

  • 1. Aggregate Functions 1Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh
  • 2. Aggregate Functions  ISO standard defines five aggregate functions: 1. COUNT returns number of values in specified column. 2. SUM returns sum of values in specified column. 3. AVG returns average of values in specified column. 4. MIN returns smallest value in specified column. 5. MAX returns largest value in specified column. Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 2
  • 3. Things to remember Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 3
  • 4. Count Function  The COUNT() function returns the number of rows that matches a specified criteria.  The COUNT(column_name) function returns the number of values (NULL values will not be counted) of the specified column: Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 4
  • 5. Count Function  Now we want to count the number of orders from "Customer Nilsen".  We use the following SQL statement: Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 5
  • 6. Count Function  The COUNT(*) function returns the number of records in a table: Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 6
  • 7. Count Function which is the total number of rows in the table. Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 7
  • 8. Count Function  The COUNT(DISTINCT column_name) function returns the number of distinct values of the specified column: Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 8
  • 9. Count Function which is the number of unique customers (Hansen, Nilsen, and Jensen) in the "Orders" table. Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 9
  • 10. Sum Function  Now we want to find the sum of all "OrderPrice" fields".  We use the following SQL statement: Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 10
  • 11. Avg Function  Now we want to find the average value of the "OrderPrice" fields.  We use the following SQL statement: Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 11
  • 12. Max Function  Now we want to find the largest value of the "OrderPrice" column.  We use the following SQL statement: Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 12
  • 13. Min Function  Now we want to find the smallest value of the "OrderPrice" column.  We use the following SQL statement: Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 13
  • 14. Group By Clause  The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns. Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 14
  • 15. Group By Clause Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 15  Now we want to find the total sum (total order) of each customer.  We will have to use the GROUP BY statement to group the customers.  We use the following SQL statement:
  • 16. Group By Clause Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 16
  • 17. Group By Clause Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 17  If we omit the group by clause from the statement-
  • 18. Things to remember Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 18
  • 19. Having Clause  The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions. Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 19
  • 20. Having Clause  Now we want to find if any of the customers have a total order of less than 2000.  We use the following SQL statement: Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 20
  • 21. Having Clause  Now we want to find if the customers "Hansen" or "Jensen" have a total order of more than 1500.  We add an ordinary WHERE clause to the SQL statement: Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 21
  • 22. Things to remember  A HAVING condition can refer only to an expression in the SELECT list, or to an expression involving an aggregate function.  If you specify an expression in the HAVING clause that isn't in the SELECT list, or that isn't an aggregate expression, you will get an error. Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 22
  • 23. Reference  World Wide Web Schools, http://www.w3schools.com/sql/sql_functions.asp [05/04/2009] Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 23