EN
MySQL - get difference between two columns
0 points
In this article, we would like to show you how to get the difference between two columns in MySQL.
Quick solution:
xxxxxxxxxx
1
SELECT `column1`, `column2`, `columnN`,
2
`column1`-`column2` AS 'alias_name'
3
FROM `values`;
To show how to get the difference between two columns, we will use the following values
table:

Note:
At the end of this article you can find database preparation SQL queries.
In this example, we will get the difference between max_value
and min_value
columns from values
table.
Query:
xxxxxxxxxx
1
SELECT `id`, `min_value`, `max_value`,
2
`max_value`-`min_value` AS 'difference'
3
FROM `values`;
Output:

create_tables.sql
file:
xxxxxxxxxx
1
CREATE TABLE `values` (
2
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
3
`min_value` INT,
4
`max_value` INT,
5
PRIMARY KEY (`id`)
6
);
insert_data.sql
file:
xxxxxxxxxx
1
INSERT INTO `values`
2
(`min_value`, `max_value`)
3
values
4
(1000, 1500),
5
(1500, 2000),
6
(1400, 2500),
7
(1200, 3000),
8
(1800, 3500);