How can you order the result obtained by select query in MySQL?

Learn, how can you order the result obtained by select query in MySQL?
Submitted by Apurva Mathur, on November 08, 2022

MySQL is simply a database management system, the two terms which are important here is database and management. So, the database is a collection of data, we can assume it is a hard copy file but when it comes to manage the data electronically the database comes into the picture. And, the other term is management which means a way to store that crucial data.

Sometimes when we want a result of a query in some specific order like ascending or descending than in such case, we use the ORDER BY clause. As the name suggests, this ORDER BY clause is used when you want to sort your result in a specific order. It gives the result in ascending or descending order. To sort the result in ascending order ASC is used while to sort the result in descending order DESC is used. By default, the result is always displayed in ascending order.

Syntax:

SELECT Column_name
FROM table_name
ORDER BY column_name ASC|DESC;

Examples:

Suppose I have a table named "students", and inside this table, I have the following columns:

order the result obtained by select query (1)

Case 1: Arranging the names in ASCENDING ORDER

In this case, we will use the following query:

SELECT * FROM students
ORDER BY Name ASC;

Here, the "student" is the table name and the Name is the column name that you want to order.

order the result obtained by select query (2)

Case 2: Arranging the names in DESCENDING ORDER

In this case, we will use the following query:

SELECT * FROM students 
ORDER BY Name DESC;
order the result obtained by select query (2)

As we can see after running the query our result is displayed in the specific order. The same syntax will be followed if you want to arrange the other columns in ascending or descending order.




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.