An aggregate function returns a single result row, based on a group of rows in a Table.
Count returns the number of rows returned by the query. If asterisk (*) is specified, then this function returns all rows, including duplicates and nulls. COUNT never returns null.
Syntax:
Count (*[DISTINCT] All/Column name)
Ques. Display the number of Employees working in the Organization.
SELECT COUNT(*)
FROM emp;
Ques. Count distinct Managers in the Employees table.
SELECT COUNT (Distinct MGR) "Managers"
FROM emp;
Ques. Display all the jobs present in emp table
SELECT job
FROM emp;
The Sum function returns the Sum of values of the select list of columns.
Syntax:
Sum(Distinct /ALL/Column name)
Ques. List the total salaries payable to employees.
SELECT Sum(Sal)
FROM emp;
The AVG function returns the Average of values of column values.
Ques. Calculate the Average of all the Salaries in the emp table.
SELECT Avg(Sal)
FROM emp;
Syntax:
SELECT <Column-list> FROM <table-name>
WHERE <condition>
GROUP BY <column-name(s)>
HAVING <condition>
ORDER BY <expression>;
The syntax of Group by clause is:
SELECT <Column1, .. Column n>
FROM <Table name1, Table name2..Table name n>
Group By ColumnsList;
Ques. List the department and number of employees working in each department.
SELECT DeptNo, Count (*)
FROM emp
GROUP BY DeptNo;
Practice: Write the above query without GROUP BY (last line) and discuss the result.
Ques. List the department number and the total salary payable in each department
SELECT deptno, Sum(Sal)
FROM emp
GROUP BY deptno;
Ques. Find the minimum salary in each department
SELECT deptno, MIN(Sal) AS "Lowest salary"
FROM emp
GROUP BY deptno;
Having clause is used to specify which groups are to be displayed i.e. restrict the groups that you return on the basis of aggregate functions. Having is similar to Where Clause but used to restrict the Groups.
Ques. List total salary for all the departments employing more than five people
SELECT deptno, Sum (Sal), count(empno)
FROM emp
GROUP BY deptno
HAVING count (*) > 5;
The above clauses are executed in the following order: