Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

ORDER BY Statement in MySQL | Sorting in MySQL

Ujjwal Abhishek
Ujjwal Abhishek

ORDER BY in MySQL

The ORDER BY clause is used in MySQL to sort the retrieved data in a particular order. When data is selected by the SELECT statement the data is not sorted in any order. The ORDER BY clause helps in sorting the fetched rows in ascending or descending order.

Syntax: 

SELECT column1, column2,...
FROM table_name 
ORDER BY Column1 ASC/DESC, Column2 ASC/DESC,... ;
  • Here, In the above-shown syntax, it can be seen that there can be multiple columns after the ORDER BY clause and any of the columns can be sorted either in ascending or in descending order of their values.
  • By default ORDER BY sorts the values of a column in ascending order. It means if there is nothing stated about the order of sorting (ASC or DESC) after the ORDER BY clause, it will be sorted in ascending order.
  • For example;
  1. SELECT column_name FROM table_name ORDER BY column_name ASC;
  2. SELECT column_name FROM table_name ORDER BY column_name;

        The output of both queries 1 and 2 will be the same.

1. Using ORDER BY for only one column
cities  
s_nostatecity
1BiharPatna
2West BengalKolkata
3Uttar PradeshVaranasi
4Madhya PradeshBhopal
5Uttar PradeshLucknow
6West BengalSiliguri
7Madhya PradeshIndore

The query written below uses the ORDER BY clause to sort the ‘city’ column in ascending order.

For example - SELECT city, state FROM cities ORDER BY city ASC;

Output:

statecity
Madhya PradeshBhopal
Madhya PradeshIndore
West BengalKolkata
Uttar PradeshLucknow
BiharPatna
West BengalSiliguri
Uttar PradeshVaranasi

It can be seen in the above table returned as an output, the ‘city’ column is sorted in ascending order. 

2. Using ORDER BY for multiple columns

The query written below will first sort the state column in ascending order and then the city column in descending order without changing the sorted order of the state column. 

For example - SELECT state, city FROM cities ORDER BY state ASC, city DESC;

Output:

statecity
BiharPatna
Madhya PradeshIndore
Madhya PradeshBhopal
Uttar PradeshVaranasi
Uttar PradeshLucknow
West BengalSiliguri
West BengalKolkata

In the above output, it can be seen that the output table is sorted in ascending order according to the ‘state’ column and there are multiple rows with the same value of ‘state’. Further, the output table is sorted in descending order of ‘city’ in the rows which have the same ‘state’.

3. Using ORDER BY with an expression

The ORDER BY clause can be used to sort the rows of a resulting table according to the value of an expression.

For example:

shop   
customer_idproduct_idprice quantity
0001#0005003
0002#2224002
0003#1116003
0004#2224003

SELECT price, quantity, price*quantity FROM shop ORDER BY price*quantity;

Output:

pricequantityprice*quantity
4002800
40031200
50031500
60031800
4. Using ORDER BY with NULL value present in the column

While sorting a table in ascending order according to any column, if NULL appears in that column. It is placed before any non-NULL value. While sorting in descending order, NULL is placed after all non-NULL values.

For example:

shop 
product_idprice 
#000500
#222400
#111NULL
#222400

SELECT product_id, price FROM shop ORDER BY price;

Output:

shop 
product_idprice 
#111NULL
#222400
#222400
#000500

It can be seen in the above example that NULL appears before any non-NULL values when the ORDER BY clause is used to sort the rows of the resulting table in ascending order of price. 

Ujjwal Abhishek
Ujjwal Abhishek
Ujjwal is final-year CSE Undergraduate, a competitive coder, and a web developer passionate about problem-solving and data structures and algorithms.
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet