+ All Categories
Home > Education > PHP mysql Aggregate functions

PHP mysql Aggregate functions

Date post: 29-Jul-2015
Category:
Upload: syed-mudasir-shah
View: 311 times
Download: 0 times
Share this document with a friend
15
Aggregate Functions
Transcript
Page 1: PHP mysql Aggregate functions

Aggregate Functions

Page 2: PHP mysql 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)

Page 3: PHP mysql Aggregate functions

Operates - on a single column Return - a single value. Used only in the SELECT list and in the

HAVING clause.

Behavior of Aggregate Functions

Page 4: PHP mysql Aggregate functions

Types of SQL Aggregate Functions

SUM AVG MIN MAX COUNT

Page 5: PHP mysql Aggregate functions

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.

Page 6: PHP mysql Aggregate functions

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

Page 7: PHP mysql Aggregate functions

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:)

Page 8: PHP mysql Aggregate functions

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()

Page 9: PHP mysql Aggregate functions

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:)

Page 10: PHP mysql Aggregate functions

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?

Page 11: PHP mysql Aggregate functions

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()

Page 12: PHP mysql Aggregate functions

AVG()

SELECT student_name,avg(mark)FROM student,enrolmentWHERE student.student_id=enrolment.student_idgroup by student_name;

(Contd:)

Page 13: PHP mysql Aggregate functions

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;

Page 14: PHP mysql Aggregate functions

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

Page 15: PHP mysql Aggregate functions

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:)


Recommended