Successfully reported this slideshow.
Upcoming SlideShare
×

# Aggregate functions

176 views

Published on

• Full Name
Comment goes here.

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

• Be the first to like this

### Aggregate functions

1. 1. Aggregate Functions
2. 2. Aggregate Functions  What is an aggregate function?  An aggregate function summarizes the results of an expression over a number of rows, returning a single value.  The general syntax for most of the aggregate functions is as follows:  aggregate_function ([DISTINCT|ALL] expression)
3. 3.  Operates - on a single column  Return - a single value.  Used only in the SELECT list and in the HAVING clause. Behavior of Aggregate Functions
4. 4. Types of SQL Aggregate Functions  SUM  AVG  MIN  MAX  COUNT
5. 5. Input to Aggregate Function  SUM and AVG :  Operates only on collections of numbers .  MIN , MAX and COUNT  Operates on collection of numeric and non-numeric data types.  Each function eliminates NULL values and operates on Non- null values.
6. 6. SUM()  Returns: The sum of the values in a specified column.  Query 1: To find the sum of all salaries in the organization:  SELECT SUM(salary) FROM employees;  375000  Query 2: To find the sum of the salaries grouped by dept  SELECT SUM(salary) FROM employees GROUP BY  dept_name
7. 7. SUM()  If we take a look at the previous query the information won’t  tell us what’s the sum for a particular department. So to include that  information we add DEPT_NAME in the SELECT  SELECT dept_name,SUM(salary) FROM employees  GROUP BY dept_name; (Contd:)
8. 8.  The query in the previous slide lists the information for all the departments. What if we want the information to be restricted only for a particular department like Engg  Is this query correct?  SELECT dept_name,SUM(salary)  FROM employees  GROUP BY dept_name  WHERE dept_name = 'ENG'; (Contd:)SUM()
9. 9.  No, the query would result in the sql error.  Remember : If we use the aggregate functions then you cannot use the WHERE clause. In order to get the result what we need to use is the HAVING clause. So the query would be:  SELECT dept_name, SUM(salary)  FROM employees  GROUP BY dept_name  HAVING dept_name = 'ENG'; SUM() (Contd:)
10. 10. AVG()  Returns: The average of the values in a specified column.  Example: Find the average of the Project Managers salary . Query:  SELECT AVG( DISTINCT salary) AS avg_salary  FROM Staff  WHERE position = ‘Project Manager’;  Result: avg_salary  10500.00 // Error in Result  // avg_salary = 11000.00  // What is wrong?
11. 11. Revised Query for AVG() Query: SELECT AVG(ALL salary) AS avg_salary FROM Staff WHERE position = ‘Project Manager’; Result :  avg_salary  11000.00 CAUTION: Using DISTINCT and ALL in SUM() and AVG()
12. 12. AVG() SELECT student_name,avg(mark) FROM student,enrolment WHERE student.student_id=enrolment.student_id group by student_name; (Contd:)
13. 13. MIN() and MAX()  Returns: MIN() returns the smallest value of a column.  MAX() returns the largest value of a column.  Query 1: To find the minimum salary within a particular department  SELECT MIN(salary),dept_name FROM employees  GROUP BY dept_name;  Query 2: To find the maximum salary within a particular department  SELECT MAX(salary), dept_name FROM employees  GROUP BY dept_name;
14. 14. COUNT() Returns: The number of values in the specified column. Example: Count number of staffs who are Manager. Query: SELECT COUNT(sno) AS sno_count FROM Staff WHERE Staff.position = ‘Manager’; Result: sno_count 2
15. 15. COUNT(*) Input: There is no input to this function. Returns: It counts all the rows of a table , regardless of whether Nulls or the duplicate occur. Example: How many Project Manager salary is more than 9000.00 Query: SELECT COUNT(*) AS Count_Salary FROM Staff WHERE Staff.position = ‘Project Manager’ AND Staff.salary > 9000.00 (Contd:)