Home »
MySQL
MySQL ORDER BY Clause
MySQL | ORDER BY Clause: Learn about the MySQL ORDER BY Clause, with its explanation, syntax, and query examples.
Submitted by Apurva Mathur, on September 05, 2022
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.
ORDER BY Clause Syntax
SELECT Column_name
FROM table_name
ORDER BY column_name ASC|DESC;
Note: We can write as many columns as we want in a single query, also we can use WHERE conditions with this clause, and for that we'll use the following syntax,
SELECT Column_name
FROM table_name
[WHERE conditions]
ORDER BY column_name ASC|DESC;
Suppose we have a table named "student_details" and inside this database, we have the following columns.
Case 1: Using ORDER BY to sort the names in ascending order
In this case, our query will be:
SELECT * FROM Student_details
ORDER BY Student_name ASC;
This query will show the following result;
You all can see that the whole table is sorted according to the column "student_name" in ascending order.
Case 2: Using ORDER BY to sort the names in descending order
Suppose we only want names in descending order;
In this case, our query will be:
SELECT Student_name
FROM Student_details
ORDER BY Student_name DESC;
This query will show the following result;