What is an aggregate function?

https://www.computersprofessor.com/2017/09/what-is-aggregate-function.html
w 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)
Some
of the commonly used aggregate functions are :
•
SUM COUNT AVG MIN MAX
Consider
the following Employee table:
EMPLOYEE
( EMP_ID, NAME, DEPT_NAME, SALARY)
CREATE
TABLE EMPLOYEE
( EMP_ID NUMBER, NAME VARCHAR2(50), DEPT_NAME VARCHAR2(50), SALARY NUMBER);
After
the insert when we query the Employee table we get the following results:
Select
* from Employee;
INSERT
INTO EMPLOYEE VALUES (100,'ABC','ENG',50000);
INSERT
INTO EMPLOYEE VALUES (101,'DEF','ENG',60000);
INSERT
INTO EMPLOYEE VALUES (102,'GHI','PS',50000);
INSERT
INTO EMPLOYEE VALUES (103,'JKL','PS',70000);
INSERT
INTO EMPLOYEE VALUES (104,'MNO','SALES',75000);
INSERT
INTO EMPLOYEE VALUES (105,'PQR','MKTG',70000);
INSERT
INTO EMPLOYEE VALUES (106,‘STU','SALES',null);
COMMIT;
Query 1: To
find the sum of all salaries in the organization:
SELECT
SUM(SALARY) FROM EMPLOYEE;
375000
Query 2: To
find the sum of the salaries grouped by dept
SELECT
SUM(SALARY) FROM EMPLOYEE GROUP BY DEPT_NAME
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 EMPLOYEE
GROUP
BY DEPT_NAME;
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 EMPLOYEE
GROUP
BY
DEPT_NAME
WHERE
DEPT_NAME = 'ENG';
No,
the query would result in the sql error (in Oracle)
ORA-00933:
SQL Command not properly ended
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 EMPLOYEE
GROUP
BY
DEPT_NAME
HAVING
DEPT_NAME = 'ENG';
AVG Function
Query 1: If
we want to calculate the AVG of all the salaries in
the
organization the SQL would be
SELECT
AVG(SALARY) FROM EMPLOYEE
62,500
Is
this what we expect????
Employee
table has 7 records and the salaries are
50,000+60,000+50,000+70,000+75,000+70,000+null/7
= 53571
But
we obtained 62500 from the query? Why is this so????
Remember
: COUNT(*) is the only function which won’t ignore
Nulls.
Other functions like SUM,AVG,MIN,MAX they ignore
Nulls.
What it means is in the previous query the salary value for
a
particular employee was NULL. So the query
SELECT
AVG(SALARY) FROM EMPLOYEE
would
ignore nulls and the way the average is calculated then would be
50,000+60,000+50,000+70,000+75,000+70,000/6
= 62500
From
the information given in the previous slide what do you think would be the
output of the following query
Select
COUNT(*),COUNT(SALARY) FROM EMPLOYEE;
It
would be
COUNT(*) COUNT(SALARY)
7 6
Because
COUNT(*) is not going to ignore the Nulls in the result
whereas
COUNT(SALARY) is going to ignore the Nulls.
SELECT
student_name,avg(mark) FROM student,enrolment
WHERE
student.student_id=enrolment.student_id;
Which
one of the following is correct for the query?
(a)
The query is not legal
(b)
The query retrieves for each student enrolled,his/her name and their
average mark
(c)
The query retrieves for each student enrolled, his/her name and the class
average mark
(d)
The query retrieves for each student enrolled, his/her name and the mark in
each subject
Is the answer (a) or (b)??????
Query 1: To
find the minimum salary within a particular department
SELECT
MIN(SALARY),NAME FROM EMPLOYEE
GROUP
BY NAME;
Query 2: To
find the maximum salary within a particular department
SELECT
MAX(SALARY),NAME FROM EMPLOYEE
GROUP
BY NAME;