
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Sorted difference between two columns in MySQL?
Use ORDER BY clause for this. Let us first create a table −
mysql> create table DemoTable ( Value1 int, Value2 int ); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(10,40); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(50,5); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(51,56); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(52,78); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(90,7); Query OK, 1 row affected (0.21 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+--------+--------+ | Value1 | Value2 | +--------+--------+ | 10 | 40 | | 50 | 5 | | 51 | 56 | | 52 | 78 | | 90 | 7 | +--------+--------+ 5 rows in set (0.00 sec)
Following is the query to get sorted difference between two columns −
mysql> select (Value1-Value2) as Sub from DemoTable order by Sub DESC;
Output
+------+ | Sub | +------+ | 83 | | 45 | | -5 | | -26 | | -30 | +------+ 5 rows in set (0.00 sec)
- Related Articles
- Swap data between two columns in MySQL?
- How to get the difference between two columns in a new column in MySQL?
- Difference between two selects in MySQL?
- Concatenate two columns in MySQL?
- MySQL difference between two timestamps in Seconds?
- Get the time difference between values in different columns with MySQL
- How to calculate the difference between time in different MySQL columns?
- Difference between two timestamps in seconds in MySQL?
- How to move data between two tables with columns in different MySQL databases?
- Get the difference between two timestamps in seconds in MySQL?
- How to search between columns in MySQL?
- MySQL to perform DateTime comparison and find the difference between dates in different columns
- Find the difference between two timestamps in days with MySQL
- How to find absolute difference between two numbers in MySQL?
- Difference between Rows and Columns

Advertisements