ByteBoard Notes Notes

Find the min, max, sum and average of the marks in a student marks table

Informatics Practices XII · Experiment 27 of 38 · SQL

★ CBSE suggested12-IPSQL queries
Expt. No.27
Page No.54

Min, max, sum and average

Aim

To find the minimum, maximum, total and average of the marks in the STUDENT table.

Concepts used

Queries

USE school;SELECT MIN(marks), MAX(marks), SUM(marks), AVG(marks) FROM student;

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

Table used

Table: student
+-----+-------+-------+
| sid | name  | marks |
+-----+-------+-------+
|   1 | Asha  |    82 |
|   2 | Rohan |    67 |
|   3 | Ira   |    88 |
|   4 | Kabir |    91 |
|   5 | Diya  |    74 |
+-----+-------+-------+

Output

mysql> USE school; Database changed
mysql> SELECT MIN(marks), MAX(marks), SUM(marks), AVG(marks) FROM student; +------------+------------+------------+------------+ | MIN(marks) | MAX(marks) | SUM(marks) | AVG(marks) | +------------+------------+------------+------------+ | 67 | 91 | 402 | 80.4000 | +------------+------------+------------+------------+ 1 row in set

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

Result

The lowest mark was 67, the highest 91, the total 402 and the average 80.4000. Each aggregate function returned one value for the whole table.

Viva voce: what the examiner may ask

01What is an aggregate function?

A function that works on a set of rows and returns one value, such as SUM or AVG.

02Why does AVG show 80.4000?

MySQL returns AVG as a decimal with four places.

03How do aggregate functions treat NULL?

They ignore NULL values (COUNT(*) is the exception).

Variations for the lab test

  1. Find the average marks of students who scored above 70.
  2. Find the number of students with COUNT(*).
  3. Round the average to 1 decimal place with ROUND().
! Common mistake Writing SELECT name, MAX(marks) FROM student; does not show the topper's name reliably; with ONLY_FULL_GROUP_BY it is an error.
Back to the 12-IP index