
- 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
How to sort more than one column at a time in MySQL?
To sort more than one column at a time, you can use ORDER BY clause. Following is the syntax −
select yourColumnName1,yourColumnName2,yourColumnName3 from yourTableName order by yourColumnName2,yourColumnName3;
Let us first create a table −
mysql> create table doubleSortDemo -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(100), -> StudentCountryName varchar(10) -> ); Query OK, 0 rows affected (0.71 sec)
Following is the query to insert records in the table using insert command −
mysql> insert into doubleSortDemo(StudentName,StudentCountryName) values('John','AUS'); Query OK, 1 row affected (0.21 sec) mysql> insert into doubleSortDemo(StudentName,StudentCountryName) values('Sam','UK'); Query OK, 1 row affected (0.20 sec) mysql> insert into doubleSortDemo(StudentName,StudentCountryName) values('Bob','US'); Query OK, 1 row affected (0.16 sec) mysql> insert into doubleSortDemo(StudentName,StudentCountryName) values('Carol','UK'); Query OK, 1 row affected (0.32 sec) mysql> insert into doubleSortDemo(StudentName,StudentCountryName) values('David','AUS'); Query OK, 1 row affected (0.19 sec) mysql> insert into doubleSortDemo(StudentName,StudentCountryName) values('Larry','UK'); Query OK, 1 row affected (0.15 sec)
Following is the query to display all records from the table using select statement −
mysql> select * from doubleSortDemo;
This will produce the following output −
+-----------+-------------+--------------------+ | StudentId | StudentName | StudentCountryName | +-----------+-------------+--------------------+ | 1 | John | AUS | | 2 | Sam | UK | | 3 | Bob | US | | 4 | Carol | UK | | 5 | David | AUS | | 6 | Larry | UK | +-----------+-------------+--------------------+ 6 rows in set (0.00 sec)
Following is the query to perform MySQL sort on more than one column i.e. Student country and name −
mysql> select StudentId,StudentName,StudentCountryName from doubleSortDemo -> order by StudentCountryName,StudentName;
This will produce the following output −
+-----------+-------------+--------------------+ | StudentId | StudentName | StudentCountryName | +-----------+-------------+--------------------+ | 5 | David | AUS | | 1 | John | AUS | | 4 | Carol | UK | | 6 | Larry | UK | | 2 | Sam | UK | | 3 | Bob | US | +-----------+-------------+--------------------+ 6 rows in set (0.00 sec)
- Related Articles
- How do I alter table column datatype on more than 1 column at a time in MySql?
- How to select more than one row at a time in a JTable with Java?
- Select MySQL rows where column contains same data in more than one record?
- MySQL query to include more than one column in a table that doesn't already exist
- Suggest a situation where we obtain more than one shadow of an object at a time.
- How to get a value more than a particular value from varchar column in MySQL?
- Insert more than one element at once in a C# List
- MySQL query to select rows one batch at a time
- Match column I with column II. There can be more than one match.
- Delete more than one rows from a table using id in MySQL?
- How to sort an alphanumeric column in MySQL?
- How to sort a matrix based on one column in R?
- How to sort time in AM/ PM in MySQL?
- How to convert more than one column in R data frame to from integer to numeric in a single line code?
- How can we add FOREIGN KEY constraints to more than one fields of a MySQL table?

Advertisements