ByteBoard Notes Notes

Use the math functions POWER(), ROUND() and MOD()

Informatics Practices XII · Experiment 30 of 38 · SQL

12-IPSQL queries
Expt. No.30
Page No.60

POWER, ROUND, MOD

Aim

To use the math functions POWER(), ROUND() and MOD() on values and on table columns.

Concepts used

Queries

USE school;SELECT POWER(2, 5), ROUND(345.678, 2), ROUND(345.678), MOD(17, 5);SELECT name, marks, MOD(marks, 2), ROUND(marks / 3, 1) 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 POWER(2, 5), ROUND(345.678, 2), ROUND(345.678), MOD(17, 5); +-------------+-------------------+----------------+------------+ | POWER(2, 5) | ROUND(345.678, 2) | ROUND(345.678) | MOD(17, 5) | +-------------+-------------------+----------------+------------+ | 32 | 345.68 | 346 | 2 | +-------------+-------------------+----------------+------------+ 1 row in set
mysql> SELECT name, marks, MOD(marks, 2), ROUND(marks / 3, 1) FROM student; +-------+-------+---------------+---------------------+ | name | marks | MOD(marks, 2) | ROUND(marks / 3, 1) | +-------+-------+---------------+---------------------+ | Asha | 82 | 0 | 27.3 | | Rohan | 67 | 1 | 22.3 | | Ira | 88 | 0 | 29.3 | | Kabir | 91 | 1 | 30.3 | | Diya | 74 | 0 | 24.7 | +-------+-------+---------------+---------------------+ 5 rows in set

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

Result

POWER(2, 5) gave 32, ROUND(345.678, 2) gave 345.68, ROUND(345.678) gave 346 and MOD(17, 5) gave 2. On the table, MOD(marks, 2) was 0 for even marks and 1 for odd, and ROUND(marks / 3, 1) rounded each third to one decimal place.

Viva voce: what the examiner may ask

01What does MOD(a, b) return?

The remainder when a is divided by b.

02What does ROUND(x) with no second argument do?

It rounds to the nearest whole number.

03Can SELECT be used without FROM?

Yes, to work out values that don't come from a table.

Variations for the lab test

  1. Display the square of every student's marks.
  2. Display students with even marks using MOD in WHERE.
  3. Round 345.678 to -1 places and explain the result.
! Common mistake Writing POWER(2^5) or 2 ** 5 gives a wrong answer or an error; use POWER(2, 5).
Back to the 12-IP index