Using Functions for Arithmetic Operations

Many of the uses you have for the data you retrieve involve mathematics. Most implementations of SQL provide arithmetic functions similar to the functions covered here. The examples in this section use the NUMBERS table:

SQL> SELECT *
  2  FROM NUMBERS;

        A         B
--------- ---------
   3.1415         4
      -45      .707
        5         9
  -57.667        42
       15        55
     -7.2       5.3

6 rows selected.

The MySQL equivalent is

mysql> select * from numbers;
+----------+---------+
| a        | b       |
+----------+---------+
|   3.1415 |  4.0000 |
| -45.0000 |  0.7070 |
|   5.0000 |  9.0000 |
| -57.6670 | 42.0000 |
|  15.0000 | 55.0000 |
|  -7.2000 |  5.3000 |
+----------+---------+
6 rows in set (0.39 sec)

ABS

The ABS function returns the absolute value of the number you point to—for example, ...

Get Sams Teach Yourself SQL in 21 Days, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.