ByteBoard Notes Notes

SQL Set 3: aggregate functions with GROUP BY and HAVING

Computer Science XII · Experiment 21 of 29 · SQL

12-CSSQL query sets
Expt. No.21
Page No.42

Aggregates, GROUP BY and HAVING

Aim

To write SQL queries on the EMPLOYEE table using COUNT, SUM, AVG, MAX and MIN with GROUP BY, HAVING and ORDER BY.

Concepts used

Queries

USE company;-- Number of employees, and number who get a commissionSELECT COUNT(*), COUNT(commission) FROM employee;-- Highest, lowest and average salarySELECT MAX(salary), MIN(salary), AVG(salary) FROM employee;-- Number of employees and total salary in each departmentSELECT dept, COUNT(*), SUM(salary) FROM employee GROUP BY dept;-- Departments whose average salary is above 50000SELECT dept, AVG(salary) FROM employee GROUP BY dept HAVING AVG(salary) > 50000;-- Departments with at least 2 employees, largest firstSELECT dept, COUNT(*) FROM employee GROUP BY dept HAVING COUNT(*) >= 2 ORDER BY COUNT(*) DESC;-- Highest salary in each department, counting only salaries above 40000SELECT dept, MAX(salary) FROM employee WHERE salary > 40000 GROUP BY dept;

A marked line continues the line above it. In your file, write it on the same line.

Table used

Table: employee
+-------+--------+----------+--------+------------+
| empno | name   | dept     | salary | commission |
+-------+--------+----------+--------+------------+
|   101 | Anil   | Sales    |  45000 |       5000 |
|   102 | Bhavna | HR       |  52000 |       NULL |
|   103 | Charu  | IT       |  68000 |       NULL |
|   104 | Amit   | Sales    |  39000 |       3000 |
|   105 | Farhan | IT       |  75000 |       NULL |
|   106 | Gauri  | Accounts |  48000 |       NULL |
|   107 | Harish | Sales    |  56000 |       6500 |
|   108 | Kavita | HR       |  41000 |       NULL |
+-------+--------+----------+--------+------------+

Output

mysql> USE company; Database changed
mysql> SELECT COUNT(*), COUNT(commission) FROM employee; +----------+-------------------+ | COUNT(*) | COUNT(commission) | +----------+-------------------+ | 8 | 3 | +----------+-------------------+ 1 row in set
mysql> SELECT MAX(salary), MIN(salary), AVG(salary) FROM employee; +-------------+-------------+-------------+ | MAX(salary) | MIN(salary) | AVG(salary) | +-------------+-------------+-------------+ | 75000 | 39000 | 53000.0000 | +-------------+-------------+-------------+ 1 row in set
mysql> SELECT dept, COUNT(*), SUM(salary) FROM employee GROUP BY dept; +----------+----------+-------------+ | dept | COUNT(*) | SUM(salary) | +----------+----------+-------------+ | Sales | 3 | 140000 | | HR | 2 | 93000 | | IT | 2 | 143000 | | Accounts | 1 | 48000 | +----------+----------+-------------+ 4 rows in set
mysql> SELECT dept, AVG(salary) FROM employee GROUP BY dept HAVING AVG(salary) > 50000; +------+-------------+ | dept | AVG(salary) | +------+-------------+ | IT | 71500.0000 | +------+-------------+ 1 row in set
mysql> SELECT dept, COUNT(*) FROM employee GROUP BY dept HAVING COUNT(*) >= 2 ORDER BY COUNT(*) DESC; +-------+----------+ | dept | COUNT(*) | +-------+----------+ | Sales | 3 | | HR | 2 | | IT | 2 | +-------+----------+ 3 rows in set
mysql> SELECT dept, MAX(salary) FROM employee WHERE salary > 40000 GROUP BY dept; +----------+-------------+ | dept | MAX(salary) | +----------+-------------+ | Sales | 56000 | | HR | 52000 | | IT | 75000 | | Accounts | 48000 | +----------+-------------+ 4 rows in set

✓ Printed by running this query set, not typed by hand.

Result

COUNT(*) gave 8 but COUNT(commission) gave 3, because NULLs are not counted. GROUP BY gave one row per department; HAVING kept only the departments whose average salary is above 50000, and the WHERE clause removed Amit (39000) before grouping.

Viva voce: what the examiner may ask

01What is the difference between WHERE and HAVING?

WHERE filters rows before grouping; HAVING filters groups after GROUP BY, so it can use aggregate functions.

02Why are COUNT(*) and COUNT(commission) different?

COUNT(column) ignores NULL values; COUNT(*) counts every row.

03Can an aggregate function be used in WHERE?

No. WHERE AVG(salary) > 50000 is an error; use HAVING.

04What does AVG ignore?

NULL values; it divides by the number of non-NULL values.

Variations for the lab test

  1. Find the total commission paid in each department.
  2. Display departments whose highest salary is above 60000.
  3. Find the number of employees whose salary is above the average (work it out in two queries).
! Common mistake Putting HAVING before GROUP BY, or using HAVING without GROUP BY to filter rows, gives an error or the wrong result; filter rows with WHERE.
Back to the 12-CS index