
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Sort MySQL Output Based on a Non-Result Set Column
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)
Advertisements