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

More Related Content

Similar to L5 aggregate functions

SQA server performance tuning
SQA server performance tuningSQA server performance tuning
SQA server performance tuningDuy Tan Geek
 
Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptx
ANSHVAJPAI
 
Lec9_Lab_CSC371_Database Systems course.pptx
Lec9_Lab_CSC371_Database Systems course.pptxLec9_Lab_CSC371_Database Systems course.pptx
Lec9_Lab_CSC371_Database Systems course.pptx
khaqan2
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
mukesh24pandey
 
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 ValuesRushdi Shams
 
MySQL Performance Optimization
MySQL Performance OptimizationMySQL Performance Optimization
MySQL Performance Optimization
Mindfire Solutions
 
Advanced MySQL Query Optimizations
Advanced MySQL Query OptimizationsAdvanced MySQL Query Optimizations
Advanced MySQL Query Optimizations
Dave Stokes
 
Intro to SQL for Beginners
Intro to SQL for BeginnersIntro to SQL for Beginners
Intro to SQL for Beginners
Product School
 
12. Basic SQL Queries (2).pptx
12. Basic SQL Queries  (2).pptx12. Basic SQL Queries  (2).pptx
12. Basic SQL Queries (2).pptx
SabrinaShanta2
 
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
 
MySQL: Indexing for Better Performance
MySQL: Indexing for Better PerformanceMySQL: Indexing for Better Performance
MySQL: Indexing for Better Performance
jkeriaki
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
rahulnadola3
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
DHAAROUN
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
poovathi nps
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
pradnyamulay
 

Similar to 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
 
Lec9_Lab_CSC371_Database Systems course.pptx
Lec9_Lab_CSC371_Database Systems course.pptxLec9_Lab_CSC371_Database Systems course.pptx
Lec9_Lab_CSC371_Database Systems course.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
 

More from Rushdi Shams

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
Rushdi Shams
 
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
Rushdi Shams
 
Machine learning with nlp 101
Machine learning with nlp 101Machine learning with nlp 101
Machine learning with nlp 101
Rushdi Shams
 
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
Rushdi Shams
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: Parsing
Rushdi Shams
 
Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translationRushdi 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 translationRushdi Shams
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semanticsRushdi Shams
 
Propositional logic
Propositional logicPropositional logic
Propositional logicRushdi Shams
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logicRushdi Shams
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structureRushdi Shams
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representationRushdi Shams
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hackingRushdi Shams
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)Rushdi Shams
 

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

Recently uploaded

June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
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
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 

Recently uploaded (20)

June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
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 ...
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 

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