0
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...
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_nam...
Count Function
 Now we want to count the number of orders from "Customer
Nilsen".
 We use the following SQL statement:
R...
Count Function
 The COUNT(*) function returns the number of records in a
table:
Rushdi Shams, Lecturer, Dept of CSE, KUET...
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:
R...
Count Function
which is the number of unique customers (Hansen, Nilsen, and
Jensen) in the "Orders" table.
Rushdi Shams, L...
Sum Function
 Now we want to find the sum of all "OrderPrice" fields".
 We use the following SQL statement:
Rushdi Shams...
Avg Function
 Now we want to find the average value of the "OrderPrice"
fields.
 We use the following SQL statement:
Rus...
Max Function
 Now we want to find the largest value of the "OrderPrice"
column.
 We use the following SQL statement:
Rus...
Min Function
 Now we want to find the smallest value of the "OrderPrice"
column.
 We use the following SQL statement:
Ru...
Group By Clause
 The GROUP BY statement is used in conjunction with the
aggregate functions to group the result-set by on...
Group By Clause
Rushdi Shams, Lecturer, Dept of CSE, KUET, Bangladesh 15
 Now we want to find the total sum (total order)...
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 stateme...
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.
R...
Having Clause
 Now we want to find if any of the customers have a total
order of less than 2000.
 We use the following S...
Having Clause
 Now we want to find if the customers "Hansen" or "Jensen"
have a total order of more than 1500.
 We add a...
Things to remember
 A HAVING condition can refer only to an
expression in the SELECT list, or to an
expression involving ...
Reference
 World Wide Web Schools,
http://www.w3schools.com/sql/sql_functions.asp
[05/04/2009]
Rushdi Shams, Lecturer, De...
Upcoming SlideShare
Loading in...5
×

L5 aggregate functions

163

Published on

Published in: Education, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
163
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
12
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×