LAB EXERCISE #3 - AGGREGATING DATA AND GROUP FUNCTIONS
1.1 Objective 1.2 Theory 1.3 Assignment
1.1 OBJECTIVE: To perform various group functions
1.2 THEORY AND CONCEPTS: Group functions differ from single-row functions in how they
are evaluated. Single-row functions are evaluated once for each row retrieved. Group functions are
evaluated on groups of one or more rows at a time. Group functions are sometimes called aggregate
functions and return a value based on a number of inputs. The exact number of inputs are not
determined until the query is executed and all rows are fetched. This differs from single-row
functions in which the number of inputs is knows at parse time-before the query is executed. These
are as follows:
Function Description
AVG Returns the statistical mean
COUNT Returns the number of non-NULL rows
MAX Returns the largest value
MIN Returns the smallest value
SUM Adds all values and returns the result
Grouping date with GROUP BY: Group functions work on data that is grouped. We tell the
database how to group or categorize data with a GROUP BY clause. Whenever we use a group
function in SELECT clause of a SELECT statement, we must place all non-grouping/non-constant
columns in the GROUP BY clause. If no GROUP BY clause id specified, the default grouping
becomes the entire result set.
Syntax:
SELECT <columnname1>,….,<columnname n>, <aggregate function(columnname)>
FROM <tablename>
GROUP BY <columnname>;
Limiting grouped data with HAVING: Group functions cannot be used in WHERE clause.
Grouping is done after all rows have been fetched.
Syntax:
SELECT <columnname1>,….,<columnname n>, <aggregate function(columnname)>
FROM <tablename>
GROUP BY <columnname>
HAVING <condition>;
DBMS Lab (BCA-507(P))
1.3 ASSIGNMENTS:
Assignment 6
Following table is created:
emp
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-81 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-81 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
On the basis of above table answer the following queries:
1. Calculate the average salary of all employees.
2. Calculate the total salary of all employees.
3. Find the name of the employee who is given the maximum salary.
4. Find the name of the employee who is given the minimum salary.
5. Calculate the department wise total salary of employees.
DBMS Lab (BCA-507(P))
Assignment 7
Following table is created:
emp
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-81 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-81 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
1. Calculate the maximum, minimum, and average salary given to each department.
2. Display the total salary given to various designations in the organization.
3. Display the total number of employees working on different designations in the
organization.
4. Display the department numbers where the employees have average salary greater
than 2000.
5. Display the names of all employees belonging to department’s having average
salary greater than 2000.
DBMS Lab (BCA-507(P))