

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL query to display records ordered by numeric difference?
Use ORDER BY and set the difference to display records ordered by numeric difference. Following is the syntax −
select *from yourTableName order by (yourIntegerColumnName1 - yourIntegerColumnName2);
Let us first create a table −
mysql> create table DemoTable1313 -> ( -> Name varchar(20), -> Score1 int, -> Score2 int -> ); Query OK, 0 rows affected (3.48 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1313 values('Chris',40,60); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable1313 values('David',70,50); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1313 values('Adam',35,30); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable1313;
Output
+-------+--------+--------+ | Name | Score1 | Score2 | +-------+--------+--------+ | Chris | 40 | 60 | | David | 70 | 50 | | Adam | 35 | 30 | +-------+--------+--------+ 3 rows in set (0.00 sec)
Following is the query to order by numeric difference −
mysql> select *from DemoTable1313 -> order by (Score1-Score2);
Output
+-------+--------+--------+ | Name | Score1 | Score2 | +-------+--------+--------+ | Chris | 40 | 60 | | Adam | 35 | 30 | | David | 70 | 50 | +-------+--------+--------+ 3 rows in set (0.00 sec)
- Related Questions & Answers
- MySQL query to display records ordered by DESC while skipping some?
- Display records by grouping dates in MySQL
- MySQL query to display only the records that contains single word?
- Display MongoDB records by ObjectId?
- MySQL query to display databases sorted by creation date?
- MySQL query to display the count of distinct records from a column with duplicate records
- MySQL query to subtract date records with week day and display the weekday with records
- MySQL query to group rows by the numeric characters in a string field?
- MySQL Query to set currency records
- Fetch ordered records after a specific limit in MySQL
- MySQL query to ORDER BY records on the basis of modulus result
- How to display records having sum between a specific range using GROUP BY, HAVING and ORDER BY in a single MySQL query?
- Get the first 10 rows followed by the syntax to display remaining row records with a single MySQL query
- Using GROUP BY and COUNT in a single MySQL query to group duplicate records and display corresponding max value
- Select last 20 records ordered in ascending order in MySQL?
Advertisements