The document discusses aggregate functions in SQL such as COUNT, SUM, AVG, MIN, and MAX. It provides examples of how to use each function to return the number of rows, sum of column values, average of column values, minimum/maximum column values, and how to use GROUP BY and HAVING clauses to group and filter aggregated results.
The document discusses aggregate functions in SQL such as COUNT, SUM, AVG, MIN, and MAX. It provides examples of how to use each function to return the number of rows, sum of column values, average of column values, minimum/maximum column values, and how to use GROUP BY and HAVING clauses to group and filter aggregated results.
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23
Aggregate Functions
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 1
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
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:
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 15
Group By Clause
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 16
Group By Clause
If we omit the group by clause from the statement-
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 17
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_functio ns.asp [05/04/2009]
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 23