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

L5 aggregate functions

  • 1.
    Aggregate Functions 1Rushdi Shams,Lecturer, Dept of CSE, KUET, Bangladesh
  • 2.
    Aggregate Functions  ISOstandard 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 RushdiShams, Lecturer, Dept of CSE, KUET, Bangladesh 3
  • 4.
    Count Function  TheCOUNT() 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  Nowwe 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  TheCOUNT(*) function returns the number of records in a table: Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 6
  • 7.
    Count Function which isthe total number of rows in the table. Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 7
  • 8.
    Count Function  TheCOUNT(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 isthe number of unique customers (Hansen, Nilsen, and Jensen) in the "Orders" table. Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 9
  • 10.
    Sum Function  Nowwe 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  Nowwe 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  Nowwe 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  Nowwe 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 RushdiShams, 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 RushdiShams, Lecturer, Dept of CSE, KUET, Bangladesh 16
  • 17.
    Group By Clause RushdiShams, Lecturer, Dept of CSE, KUET, Bangladesh 17  If we omit the group by clause from the statement-
  • 18.
    Things to remember RushdiShams, Lecturer, Dept of CSE, KUET, Bangladesh 18
  • 19.
    Having Clause  TheHAVING 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  Nowwe 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  Nowwe 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 WideWeb Schools, http://www.w3schools.com/sql/sql_functions.asp [05/04/2009] Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 23