php mysql aggregate functions
TRANSCRIPT
Aggregate Functions
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)
Operates - on a single column Return - a single value. Used only in the SELECT list and in the
HAVING clause.
Behavior of Aggregate Functions
Types of SQL Aggregate Functions
SUM AVG MIN MAX COUNT
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.
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
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:)
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()
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:)
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?
Revised Query for AVG()Query:
SELECT AVG(ALL salary) AS avg_salaryFROM StaffWHERE position = ‘Project Manager’;
Result : avg_salary 11000.00
CAUTION: Using DISTINCT and ALL in SUM() and AVG()
AVG()
SELECT student_name,avg(mark)FROM student,enrolmentWHERE student.student_id=enrolment.student_idgroup by student_name;
(Contd:)
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;
COUNT()
Returns: The number of values in the specified column. Example: Count number of staffs who are Manager.
Query: SELECT COUNT(sno) AS sno_countFROM StaffWHERE Staff.position = ‘Manager’;
Result: sno_count 2
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:)