
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
How can we sort multiple columns in a single query?
We can sort multiple columns in a single query by giving more than one column name with ORDER BY Clause. The syntax of the above is as follows β
Syntax
Select Col1,Col2,β¦ from table_name ORDER BY Col1, Col2,β¦
Example
Suppose we want to sort the table named βStudentβ by columns βNameβ and βRollNoβ both then we can write the single query for this as follows β
mysql> Select Name, RollNo from student order by name,rollno; +--------+--------+ | name | rollno | +--------+--------+ | Aarav | 150 | | Aryan | 165 | | Gaurav | 100 | +--------+--------+ 3 rows in set (0.00 sec)
The above query gave βNameβ and βRollnoβ as the sorted output. We can also get all the columns of the table as output as follows β
mysql> Select * from student order by name,rollno; +--------+--------+--------+ | Name | RollNo | Grade | +--------+--------+--------+ | Aarav | 150 | M.SC | | Aryan | 165 | M.tech | | Gaurav | 100 | B.tech | +--------+--------+--------+ 3 rows in set (0.00 sec)
Advertisements