SQL Exercise 8

GROUP FUNCTIONS

Group functions let you compute a single summary value from a column of individual values. The basic group functions are AVG, COUNT, MIN, MAX and SUM. Their use is straightforward. Group functions ignore NULL values.

 

GROUP FUNCTION

DEFINITION

AVG(column_name)

Average value for a group of rows.

COUNT(column_name)

Counts the number of rows where the expression is not null. If you use the * in the select clause, then all rows are counted whether they are null or not.

MIN(column_name)

Minimum of all values for a group of rows.

MAX(column_name)

Maximum of all values for a group of rows.

SUM(column_name)

Sum of all values for a group of rows.

 

Suppose you want to compute the average salary of all employees listed in the EMPLOYEE table. The SQL query would look like this:

SQL>SELECT AVG(SALARY)

FROM EMPLOYEE;

The results table would look like this:

AVG(SALARY)

-----------

2812.5

Suppose you want to find the minimum and maximum salaries listed in the employee table. The SQL query would look like this:

SQL> SELECT MIN(SALARY), MAX(SALARY)

FROM EMPLOYEE;

The results table would look like this:

MIN(SALARY)

MAX(SALARY)

825

4500

Consider that the values in the Salary column represent monthly salaries. Suppose you want to find the total amount paid per month in salary to employees in Department 40. The SQL query would look like this:

SQL> SELECT SUM(SALARY)

FROM EMPLOYEE

WHERE DEPTNO = 40;

The results table would look like this:

SUM(SALARY)

---------

10550

Suppose you want to find out how many employees are listed in the EMPLOYEE table. The SQL query would look like this:

SQL> SELECT COUNT(*)

FROM EMPLOYEE;

The results table looks like this:

COUNT(*)

--------

12

DISTINCT may be used with the COUNT function to count only distinct rows.

Suppose you want to know how many different jobs are listed in the EMPLOYEE table. The following query will return this information.

SQL> SELECT COUNT(DISTINCT JOB)

FROM EMPLOYEE;

The results table is shown here:

COUNT(DISTINCTJOB)

------------------

NOTE: Group functions may not be listed with column names in the SELECT clause (unless you also use the GROUP BY command as described below). For example, suppose you want to find the name of the employee who is paid the lowest salary. The query, "SELECT NAME, MIN(SALARY) FROM EMPLOYEE", would generate an error. A subquery will return the desired information. Go to SQL Exercise 9 to learn about how to use subqueries.

GROUP BY

The GROUP BY clause causes a select statement to produce one summary row for all selected rows that have the identical value as specified in the GROUP BY clause. The column referred to in the GROUP BY clause does not have to be in the select clause. However, if the column referred to in the GROUP BY clause is not listed in the select you will not know with which group the returned data is associated. For example, suppose you want to know the total amount paid in salary to each department. The query would look like this:

SQL> SELECT SUM(SALARY)

FROM EMPLOYEE

GROUP BY DEPTNO;

The results table from this query would look like this:

SUM(SALARY)

-----------

4600

8000

10550

5450

5150

Without the DEPTNO column in the select clause, you have data, but don't know which department is associated with each row of data. To solve this problem, write the query like this:

SQL> SELECT DEPTNO, SUM(SALARY)

FROM EMPLOYEE

GROUP BY DEPTNO;

This results table shows information that is useful.

DEPTNO

SUM(SALARY)

10

4600

30

8000

40

10550

50

5450

60

5150

 

Suppose you want to know the number of employees within each department from the EMPLOYEE table. The query would look like this:

SQL> SELECT DEPTNO, COUNT(*)

FROM EMPLOYEE

GROUP BY DEPTNO;

The results table would look like this:

DEPTNO

COUNT(*)

10

2

30

2

40

3

50

2

60

3

Suppose you want to find the minimum salary, maximum salary, and the average salary for each department. The SQL implementation for this query would be:

SQL> SELECT DEPTNO, MIN(SALARY),

MAX(SALARY), AVG(SALARY) ç group functions in select clause

FROM EMPLOYEE

GROUP BY DEPTNO; ç GROUP BY clause

The results table would look like this:

DEPTNO

MIN(SALARY)

MAX(SALARY)

AVG(SALARY)

10

1700

2900

2300

30

3500

4500

4000

40

2500

4250

3516.6667

50

1800

3650

2725

60

825

2700

1716.6667

 

HAVING

The HAVING clause is similar to the WHERE clause. The WHERE clause acts on individual rows while the HAVING clause acts on groups. The HAVING clause is used to determine which groups the GROUP BY is to include. For instance, suppose you want to find which departments have at least 3 employees listed in the EMPLOYEE table. Using the query above with the HAVING clause we choose to return only the rows with more than 2 employees per department. That query would be as follows:

SQL> SELECT DEPTNO, COUNT(*)

FROM EMPLOYEE

GROUP BY DEPTNO

HAVING COUNT(*) > 2; ç HAVING clause

The results table looks like this:

DEPTNO

COUNT(*)

40

3

60

3

 

Combining Group Functions and Arithmetic Functions

Suppose you want to find out how much total salary is paid out per month to all employees listed in the EMPLOYEE table. Suppose you are feeling very generous and want to find out how much total monthly salary you would pay if you give every employee a 3% raise. A single SQL query would return this information.

SQL> SELECT SUM(SALARY), SUM(SALARY * 1.03)

FROM EMPLOYEE;

The results table looks like this:

SUM(SALARY)

SUM(SALARY*1.03)

33750

34762.5

 

You may choose names for columns in your results table by including the names in the SELECT clause as illustrated below:

SQL> SELECT SUM(SALARY) NOW, SUM(SALARY * 1.03) WITHRAISE

FROM EMPLOYEE;

This query returns the following results table:

NOW

WITHRAISE

33750

34762.5

 

Be careful when naming columns in results tables. Avoid reserved words and don't include symbols other than the underscore. There are many reserved words in Oracle. If you try to name your results table columns and receive an error message, try another name.

For you to try:

  1. Suppose you want to know the minimum commission paid, the maximum commission paid, the sum of all commissions paid, and the number of employees paid commission. Write one SQL query to find this information.
  2. Suppose you want to find out how many employees are listed in each job category. Write a SQL query to find this information. 
  3. Suppose you want to find out how much total salary was paid to each different job category. That is, how much total were clerks paid, how much were managers paid, etc. Write a SQL query to find this information.

Go here to check your answers.

Last revised 10/21/1997 by J. Warren, Kennesaw State University

Department of Computer Science and Information Systems

Return to Oracle/SQL Tutorial Contents Page | Return to SQL Exercise 7 | Go to SQL Exercise 9