Upcoming SlideShare
×

# L5 aggregate functions

203
-1

Published on

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total Views
203
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
15
0
Likes
1
Embeds 0
No embeds

No notes for slide

### L5 aggregate functions

1. 1. Aggregate Functions 1Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh
2. 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. 3. Things to remember Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 3
4. 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. 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. 6. Count Function  The COUNT(*) function returns the number of records in a table: Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 6
7. 7. Count Function which is the total number of rows in the table. Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 7
8. 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. 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. 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. 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. 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. 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. 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. 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. 16. Group By Clause Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 16
17. 17. Group By Clause Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 17  If we omit the group by clause from the statement-
18. 18. Things to remember Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 18
19. 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. 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. 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. 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. 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