Tuesday, 17 July 2018

MySQL Order By

MySQL Order By

MySQL Order By clause is used to sort the table data in either Ascending order or Descending order. By default data will not be 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.

MySQL Order By Syntax

The basic syntax of Order By in MySQL is as shown below:
From the above 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 Asc 1

MySQL Order By Ascending

In order to sort data in Ascending order, we have to use Order By statement followed by ASC keyword. In MySQL, Following are the list of ways we can 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 less sales) then you can write the query as:
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 example, we are going to sort the Data in Ascending Order using Yearly Income. Please replace yearly income with any of your required column.
From the below screenshot you can see, Data is sorted by Yearly Income in Ascending order.
MySQL Order By Asc 3

MySQL Sort in Ascending Order – without using ASC

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 customers data by First_Name in ascending order without using ASC Keyword.
OUTPUT
MySQL Order By Asc 2

MySQL Sort in Ascending Order – Use Numerical Position

In this example, We are going to sort customers table in Ascending Order using Numerical Position of a Column Name.
The Numerical position of Last_Name is 2 so, data will be sorted by this column.
MySQL Order By Asc 4

MySQL Order By Multiple Columns in Ascending Order

In this example we are sorting the Data using multiple columns.
First, data will be sorted by First Name in Ascending Order, and then data will be sorted by Last Name in Ascending Order
MySQL Order By Asc 5

MySQL Sort By ASC – Using Alias Column

In this example, we are going to sort the customers Data in Ascending Order using Alias Column Name.
We used the concat string function to concat 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, customers data will be sorted by Name in Ascending Order.
MySQL Order By Asc 6

SQL ORDER BY Descending

In order to sort data in Descending order, we have to use Order By statement followed by DESC keyword. In MySQL, Following are the list of ways we can sort data in Descending order.
For example, If you are searching for shoes in Amazon. If you type shoe in the search bar, it will display shoes by Rating. It means Shoes are sorted as per the Rating. Technically,

MySQL Sort in Descending Order Example

In this example, We are going to sort customers data in Descending Order using Sales column.
From the above screenshot you can see, data is sorted by Sales in Descending order.
MySQL Order By DESC 7

MySQL Sort in Descending Order – Use Numerical Position

The Numerical position of Education column is 3. So, data will be sorted by this column in Descending order
OUTPUT
MySQL Order By DESC 8

MySQL Order By Multiple Columns in Descending Order

In this example we are sorting the Data using multiple columns. First, data will be sorted by Education in Descending Order, and then data will be sorted by Yearly Income in Descending Order
OUTPUT
MySQL Order By DESC 9

MySQL Sort in Descending Order – Using Alias Column

In this example, We are going to sort the table Data in Descending Order using the Alias Column Name.
We added 12500 to each and every yearly income column, and used Alias to assign New Income name. Next, we used the Alias name in the Order By clause. It means, Data will be sorted by New Income in Descending Order.
MySQL Order By DESC 10

Combine MySQL Order By ASC and DESC

MySQL Sort allows us to combine both ASC, and DESC keywords in a single statement. In this example we are sorting Education in Ascending Order, and then sorting Yearly Income in Descending Order
OUTPUT
MySQL Order By DESC 11
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
OUTPUT
MySQL Order By DESC 12

MySQL Order By Example – Command Prompt

Let me show you, How to sort records using the command prompt. In this example we are sorting the First Name, and Last Name in Ascending Order, and then we are sorting Yearly Income in Descending Order
OUTPUT
MySQL Order By DESC 13

0 comments:

Post a Comment