
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
We know that MySQL SELECT command is used to fetch data from a MySQL table. When you select rows, the MySQL server is free to return them in any order, unless you instruct it otherwise by saying how to sort the result. But, we can sort a result set by adding an ORDER BY clause that names the column or columns which you want to sort.
Syntax
Select column1, column2,…,columN From table_name ORDER BY column1[column2,…];
Example
In the example below, MySQL returns the result set sorted on the basis of column ‘Name’;
mysql> Select Id, Name, Address from Student ORDER BY Subject; +------+---------+---------+ | Id | Name | Address | +------+---------+---------+ | 15 | Harshit | Delhi | | 1 | Gaurav | Delhi | | 17 | Raman | Shimla | | 2 | Aarav | Mumbai | +------+---------+---------+ 4 rows in set (0.00 sec)
Advertisements