MySQL Order By clause is used to sort the table data in either Ascending order or Descending order. By default, data is not inserted into Tables in any order unless you have an index.
So, If you want to retrieve the data in any particular order, you have to sort it by using MySQL Order By statement. The basic syntax of Order By in MySQL is as shown below:
SELECT Column_Names FROM Table_Name ORDER BY {Column, Numerical Position, Alias Columns}[ASC | DESC]
From the above MySQL order by syntax, you can observe that we can use the Column Name, or Numerical Position of the Column Name, or an Alias Column Name in the Order By statement. We are going to use the below-shown data to explain the MySQL Order By statement with example.
MySQL Order By Ascending
To sort data in ascending order, we have to use Order By statement, followed by the ASC keyword. The following are the list of ways to sort data in ascending order. For example, as a Sales Manager, If you want to identify the low performing products (Products with No Sales, or fewer sales), then write the query as:
Select Product Name From Products Table Order By Sales Asc.
By seeing the result, you can understand that x is not performing well, and y has no sales at all. Using this data, you can try different strategies to improve sales.
MySQL Sort in Ascending Order Example
In this MySQL example, we are going to sort the Data in Ascending Order using Yearly Income. Please replace yearly income with any of your required columns in a Table.
-- MySQL Order By Asc Example USE company; SELECT First_Name, Last_Name, Education, Profession, Yearly_Income, Sales FROM customers ORDER BY Yearly_Income ASC;
From the below screenshot, you can see the Data was sorted by Yearly Income in Ascending order.
MySQL Sort in Ascending Order without using ASC
The ASC keyword is the default keyword in Order By statement, that’s why it is optional to use ASC. In this example, we are going to sort the customer’s data by First_Name in ascending order without using ASC Keyword.
-- MySQL Order By Asc Example USE company; SELECT First_Name, Last_Name, Education, Profession, Yearly_Income, Sales FROM customers ORDER BY First_Name;
MySQL Sort Numeric Position in Ascending Order
In this MySQL Order By ASC example, We are going to sort customers table in Ascending Order using Numerical Position of a Column Name.
-- MySQL Order By Asc Example USE company; SELECT First_Name, Last_Name, Education, Profession, Yearly_Income, Sales FROM customers ORDER BY 2 ASC;
The Numerical position of Last_Name is 2. So, data sorted by this column.
MySQL Order By Multiple Columns in Ascending Order
In this MySQL Order By ASC example, we are sorting the Data using multiple columns.
-- MySQL Order By ASC Example USE company; SELECT First_Name, Last_Name, Education, Profession, Yearly_Income, Sales FROM customers ORDER BY First_Name ASC, Last_Name ASC; --OR you can Simply Write ORDER BY First_Name, Last_Name
First, data sorted in Ascending Order by First Name. The Last Name then sorts it in Ascending Order.
MySQL Sort By ASC using Alias Column
In this MySQL Order by ASC example, we are going to sort the customer’s Data in Ascending Order using Alias Column Name.
-- MySQL Order By Asc Example USE company; SELECT CONCAT(First_Name, ' ', Last_Name) AS 'Name', Education, Profession, Yearly_Income, Sales FROM customers ORDER BY 'Name' ASC;
We used the concat string function to concat the First Name, and Last_Name columns to create Name (an Alias Name). Next, we used the Alias name in the ORDER BY Clause. It means customer’s data sort by Name in Ascending Order.
MySQL ORDER BY Descending
To sort data in Descending order, use Order By statement followed by the DESC keyword. The following are the list of ways we can sort data in Descending order.
For example, If you are searching for shoes on Amazon. If you type shoe in the search bar, it displays shoes by Rating. It means, Shoes are sorted as per the Rating. Technically,
Select Shoe from Product table order by Product Rating desc
MySQL Sort in Descending Order Example
In this MySQL Order By Desc example, we are going to sort customer’s data in Descending Order using the Sales column.
-- MySQL Order By DESC Example USE company; SELECT First_Name, Last_Name, Education, Profession, Yearly_Income, Sales FROM customers ORDER BY Sales DESC;
From the above screenshot, you can see data sorted by Sales in Descending order.
MySQL Sort in Descending Order using Numerical Position
The Numerical position of Education column is 3. So, data sorted by this column in Descending order.
-- MySQL Order By DESC Example USE company; SELECT First_Name, Last_Name, Education, Profession, Yearly_Income, Sales FROM customers ORDER BY 3 DESC;
MySQL Order By Multiple Columns in Descending Order
In this order by DESC example, we are sorting the Data using multiple columns. First, data sorted by Education in Descending Order and then sorted by Yearly Income in Descending Order.
-- MySQL Order By DESC Example USE company; SELECT First_Name, Last_Name, Education, Profession, Yearly_Income, Sales FROM customers ORDER BY 3 DESC, Yearly_Income DESC;
Sort in Descending Order using Alias Column
In this MySQL Order By Desc example, We are going to sort the table Data in Descending Order using the Alias Column Name.
-- MySQL Order By DESC Example USE company; SELECT First_Name, Last_Name, Education, Profession, Yearly_Income, Yearly_Income + 12500 AS 'New Income', Sales FROM customers ORDER BY 'New Income' DESC;
We added 12500 to each yearly income column and used Alias to assign a New Income name. Next, we used the Alias name in the Order By clause. It means, Data sort by New Income in Descending Order.
Combine Order By ASC and DESC
We can also combine both ASC and DESC keywords in a single MySQL statement. In this example, we are sorting Education in Ascending Order, and then sorting Yearly Income in Descending Order.
-- MySQL Order By DESC and ASC Example USE company; SELECT First_Name, Last_Name, Education, Profession, Yearly_Income, Sales FROM customers ORDER BY Education ASC, Yearly_Income DESC;
Let us use the DESC keyword first, and ASC keyword second. It means, below query sort the Data by Education in Descending Order, and Yearly Income in Ascending Order
-- MySQL Order By DESC and ASC Example USE company; SELECT First_Name, Last_Name, Education, Profession, Yearly_Income, Sales FROM customers ORDER BY Education DESC, Yearly_Income ASC;
Order By Command Prompt Example
Let me show you how to sort records using the command prompt MySQL sort. In this example, we are sorting the First Name and Last Name in Ascending Order. Then we are sorting Yearly Income in Descending Order.
-- MySQL Order By DESC and ASC Example USE company; SELECT First_Name, Last_Name, Education, Profession, Yearly_Income, Sales FROM customers ORDER BY First_Name ASC, Last_Name ASC, Yearly_Income DESC;