How can we select records from a table if the absolute value of the difference between two values is greater than a certain number?


We can use MySQL absolute value i.e. ABS() function to select records from a table if the absolute value of the difference between two values is greater than a certain number. We are using the data from ‘Marks’ table to demonstrate it for example.

Example

mysql> Select * from studentmarks where abs(Hindi-English)>10;
+--------+-------+---------+------+---------+-----------+
| Name   | Hindi | English | Math | Physics | Chemistry |
+--------+-------+---------+------+---------+-----------+
| Gaurav | 75    | 86      | 95   | 69      | 85        |
| Rahul  | 74    | 85      | 97   | 69      | 86        |
| Mohit  | 73    | 87      | 89   | 69      | 86        |
+--------+-------+---------+------+---------+-----------+
3 rows in set (0.00 sec)

The above result set has only those records for which the absolute value of the difference between two subjects, English and Hindi is greater than 10.

Sai Subramanyam
Sai Subramanyam

Passionate, Curious and Enthusiastic.

Updated on: 30-Jul-2019

50 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements