While working with MySQL queries, the results are obtained in the same sequence as the records inserted into the schema utilizing the SELECT command. It’s the standard order for sorting. You would be aiming at how we might arrange our query result. Sorting is re-arranging the outputs of our query in a defined manner. Sorting may be done on one field or more than one field. The ORDER BY statement is being used to arrange the query results in an ascending or descending order in MySQL. The ORDER BY statement organizes data by default in go-up order if ASC or DESC is not specified. The DESC term is being used to organize the data in descending way.

Syntax:

Let’s have a glimpse at the explanation of a query.

  • Table_name: Name of a table to get data from
  • Expression: Name of a column to be retrieved or name of a column used to arrange data.
  • ASC: Used to categorize data in ascending order. It is optional.
  • DESC: Used to arrange data in descending order. It is optional
  • WHERE condition: It is an optional constraint to be used.

Get started with opening the MySQL command-line client shell to start working on sorting. It may ask for your MySQL password. Type your password and tap Enter to continue.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-302.png" data-lazy- height="45" src="data:image/svg xml,” width=”560″>

Example: Sort without ORDER BY (ASC or DESC) clause:

To elaborate sorting with the ORDER BY clause, we have been starting our first example without using the ORDER BY clause. We have a table ‘teacher’ in the schema ‘data’ of MySQL with some records in it. When you want to fetch the data from this table, you will get it as it is, as it was inserted in the table without performing extra sorting, as presented below.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-304.png" data-lazy- height="292" src="data:image/svg xml,” width=”724″>

Example: Sort with ORDER BY Column Name without ASC|DESC:

Taking the same table with a little change in the SELECT query. We have specified the name of a column according to which the whole table will get sorted. We have been using the column ‘id’ to sort the table. As we haven’t defined the sort type, e.g., Ascending or descending, that’s why it will be automatically sorted in ascending order of ‘id’.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-306.png" data-lazy- height="289" src="data:image/svg xml,” width=”725″>

Let’s sort the same table without using ASC or DESC expression in the SELECT statement while using another column. We will be sorting this table ORDER BY the column ‘subject’. All the data in the column ‘subject’ will get sorted alphabetically first; then, the whole table will be sorted according to it.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-308.png" data-lazy- height="292" src="data:image/svg xml,” width=”726″>

Now. We will be sorting the table ‘teacher’, according to the column ‘qualification’. This query will sort the column ‘qualification’ alphabetically first. After that, all the records get sorted by this column as below.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-309.png" data-lazy- height="288" src="data:image/svg xml,” width=”720″>

You may also fetch the specified column data from the table with the ORDER BY clause. Let’s display the three-column data from the table ‘teacher’ and sort this data according to the column ‘firstname’. We will be getting three columns sorted record as shown.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-311.png" data-lazy- height="290" src="data:image/svg xml,” width=”686″>

Example: Sort with ORDER BY Single Column Name with ASC|DESC:

Now, we will be performing the same query with a little change in its syntax. We will specify the sorting type while defining the column name in the query. Let us fetch the record of four columns: firstname, lastname, subject, and qualification from a table ‘teacher’ while sorting this record according to the column ‘firstname’ in ascending order. This means that the column ‘firstname’ will be sorted in ascending order first then all the data regarding it will get sorted.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-313.png" data-lazy- height="291" src="data:image/svg xml,” width=”806″>

According to the descending order of column ‘firstname’, Sorting the same record of four columns’ is as follows.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-316.png" data-lazy- height="291" src="data:image/svg xml,” width=”789″>

Fetching the fully-fledge table ‘teacher’ while using the column ‘firstname’ in ascending order is as follows.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-318.png" data-lazy- height="289" src="data:image/svg xml,” width=”720″>

Let’s retrieve the whole table by the descending order of column ‘id’ as below.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-319.png" data-lazy- height="288" src="data:image/svg xml,” width=”727″>

Example: Sort with ORDER BY Multiple Column Name with ASC|DESC:

Yes! You can easily sort your table with multiple columns. You just need to specify the ORDER BY clause while a comma separates each column name with its sorting type. Let’ take a glimpse of a simple example. We have been selecting four-column data from a table. Firstly, this data will be sorted according to the column ‘id’ in descending order then into descending order by the column ‘firstname’.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-320.png" data-lazy- height="290" src="data:image/svg xml,” width=”819″>

Example: Sort with ORDER BY with WHERE clause:

As we know that the WHERE clause is being used for performing some conditions on data. We can easily sort our data while using the WHERE clause and fetch it according to it. We have performed a simple query in which we have fetched all records from the table ‘teacher’ where the ‘id’ is greater than 4 and the ‘qualification’ of a teacher is ‘Mphil’. This query will fetch the records of teachers whom qualification is equaled to MPhil, and their ‘id’ is not less than 5. After that, this data will b sorted in descending order of the ‘ids’ of teachers as shown in the image.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-321.png" data-lazy- height="179" src="data:image/svg xml,” width=”739″>

If you want to fetch the specified columns from a table, you can also do that. Let’s do that as below.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-322.png" data-lazy- height="148" src="data:image/svg xml,” width=”762″>

Conclusion:

We have done almost all the examples to learn the ORDER BY clause for sorting the records. I hope this article will help you to sort results in MySQL.

About the author

<img alt="Aqsa Yasin" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/Author-Image-150×150.jpg603d9a1f875e2.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.