- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to sort MySQL output on the basis of the column which is not in the result set?
It is quite possible to get the sorted output on the basis of the column which is not even the part of that output or not in the result set. It can be done by selecting the required fields and writing the name of the fields on the basis of which sorting order is desired. Following is an example to demonstrate it, in which we sorted out the result set, having ‘Name’ and ‘Address’ fields, on the basis of column ‘id’.
mysql> Select Name, Subject From Student ORDER BY Id; +---------+-----------+ | Name | Subject | +---------+-----------+ | Gaurav | Computers | | Aarav | History | | Harshit | Commerce | | Raman | Computers | +---------+-----------+ 4 rows in set (0.00 sec)
We can also use DESC or ASC keywords as follows
mysql> Select Name, Subject from Student ORDER BY Id DESC; +---------+-----------+ | Name | Subject | +---------+-----------+ | Raman | Computers | | Harshit | Commerce | | Aarav | History | | Gaurav | Computers | +---------+-----------+ 4 rows in set (0.00 sec)
- Related Articles
- How can we get the summary output of a column in MySQL result set itself?
- Set custom messages on the basis of a column with student marks in MySQL
- MySQL query to ORDER BY records on the basis of modulus result
- Match column values on the basis of the other two column values in MySQL
- Add a new column and set values in it on the basis of conditions in MySQL?
- MySQL query to select three highest values and sort alphabetically on the basis of corresponding column with name
- Perform MySQL UPDATE on the basis of DATE value in another column
- What is the meaning of ‘empty set’ in MySQL result set?
- Sort a list in MySQL and display a fixed result at the end of the column?
- Display the student marks in a single column on the basis of subject in MySQL?
- How to sort the output in PowerShell?
- Concatenate rows on the basis of boolean values in another column with MySQL
- Display custom text in a new column on the basis of null values in MySQL?
- How to make MySQL result set the same as specified?
- Python – Sort List items on the basis of their Digits

Advertisements