
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Order a MySQL table by two columns?
Order a MySQL table by two columns with the help of below syntax −
order by yourColumnName1 DESC,yourColumnName2 DESC;
Let us first create a table for our example −
mysql> create table OrderByDemo -> ( -> StudentId int, -> StudentName varchar(100), -> StudentAge int -> ); Query OK, 0 rows affected (0.57 sec)
Inserting records into the table with the help of insert command. The query is as follows −
mysql> insert into OrderByDemo values(1,'John',23); Query OK, 1 row affected (0.20 sec) mysql> insert into OrderByDemo values(3,'Johnson',24); Query OK, 1 row affected (0.27 sec) mysql> insert into OrderByDemo values(4,'Carol',26); Query OK, 1 row affected (0.14 sec) mysql> insert into OrderByDemo values(2,'David',20); Query OK, 1 row affected (0.13 sec)
Now, apply the above syntax to order by two columns in MySQL table. The query is as follows −
mysql> select *from OrderByDemo order by StudentId ASC, StudentAge ASC;
The following is the output that orders two columns in ascending order −
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 1 | John | 23 | | 2 | David | 20 | | 3 | Johnson | 24 | | 4 | Carol | 26 | +-----------+-------------+------------+ 4 rows in set (0.00 sec)
Or you can do in descending order with the help of DESC command. The query is as follows −
mysql> select *from OrderByDemo order by StudentId DESC,StudentAge DESC;
The following is the output −
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 4 | Carol | 26 | | 3 | Johnson | 24 | | 2 | David | 20 | | 1 | John | 23 | +-----------+-------------+------------+ 4 rows in set (0.00 sec)
Note − The primary sort works first.
- Related Articles
- Order by a function of two columns in a single MySQL query
- How to order by the highest value from two columns in MySQL?
- How to order MySQL rows by multiple columns?
- Split float value in two columns of a MySQL table?
- Display two different columns from two different tables with ORDER BY?
- Order by multiple columns not working as expected in MySQL?
- Return order of MySQL SHOW COLUMNS?
- Select and add result of multiplying two columns from a table in MySQL?
- MySQL query to order by two fields and NULL values in chronological order?
- MySQL query error with a table named “order”?
- How to update a MySQL table by swapping two column values?
- Easy way to re-order columns in MySQL?
- Can we alter order of columns in MySQL?
- Get the number of columns in a MySQL table?
- Concatenate two columns in MySQL?

Advertisements