Introduction to MySQL ORDER BY DESC
MySQL ORDER BY DESC is an ORDER BY Clause in MySQL, which is responsible to sort the database records when fetched as the result rows.
Basically, the ORDER BY clause takes attributes to specify the sorting order of the table records. Therefore, we need to include the ASC or DESC modifiers with the ORDER BY clause for this query command.
These attributes, along with the ORDER BY clause, make the arrangement of rows in the result table to be displayed either in ascending or descending order. But by default, the value will be set to ASC when the modifier is not provided to the ORDER BY clause. So, we will implement the ORDER BY DESC query to result from rows in descending order.
The subsequent syntax is a simple structure for applying for ORDER BY DESC clause with MySQL database query:
SELECT ColumnName1, ColumnName2, ….FROM TableName WHERE Cond_Expr ORDER BY ColumnName1 DESC, ColumnName2DESC,….ColumnNameNDESC;
The syntax is explained below:
- To apply for the ORDER BY DESC clause, we will use the SELECT statement for writing a query.
- ColumnName denotes the name of the table columns that the user wishes to fetch.
- TableName denotes the name of the table included in the query.
- WHERE clause defines the conditional expression to be applied for the table to provide the result, this is an optional condition if required. However, if the condition is matched, then the selected rows will be displayed in the result set.
- ORDER BY clause with DESC defines the ordering structure with a specific column name.
How does ORDER BY DESC work in MySQL?
- Implementing the SELECT statement in the MySQL query will fetch the data records from the specific table; then, you will notice that the result rows table provided is not in an ordered form. Therefore, to present the result set in an arrangement with a sorted list, we need to add MySQL ORDER BY clause with the SELECT query.
- With the help of the ORDER BY clause, you can order the rows with any specific column name. To further make the result displayed in a sorted form, i.e., increasing or decreasing order, we will use the keywords ASC or DESC, which defines the attributes for this ORDER BY clause.
- The DESC keyword is used with the ORDER BY Clause to make the records retrieval in decreasing order. We can specify one or more table columns that are required for sorting after the clause ORDER BY, but a comma should separate the column list. Here, you should note that the different columns can be sorted differently either with ASC or DESC keywords after ORDER BY according to the syntax mentioned. Like this one below:
SELECT ColumnName1, ColumnName2, …. FROM TableName ORDER BY ColumnName1 ASC, ColumnName2 DESC,….ColumnNameN [ASC | DESC];
Hence, this will sort the result rows by re-arranging them in a definite way.
Examples of MySQL ORDER BY DESC
Let us demonstrate some examples with the ORDER BY DESC clause and see its uses and working of it in MySQL:
Example #1 – MySQL ORDER BY DESC using one table column
Suppose, for illustration; we will take a sample table in our database named as Customer having the fields CusotmerID, CutomerName, Credit_Limit, and City. View the contents of the table as follows:
SELECT * FROM Customer;
Let us query data records from the Customer table to display the result using the ORDER BY clause simply without the attribute:
SELECT CustomerID, CustomerName, City FROM Customer ORDER BY CustomerName;
As you can see from the above output, the result rows are sorted by default in ascending order by omitting the ASC and DESC keywords either.
Now, let us write a query using the MySQL ORDER BY DESC clause to sort the query result set by a particular table column in descending order:
SELECT CustomerID, CustomerName, City FROM Customer ORDER BY CustomerName DESC;
The difference can be viewed by using the DESC modifier with the ORDER BY clause to sort the contents in decreasing order arrangement. For example, the CustomerName column in the query is specified with ORDER BY DESC.
Example #2 – MySQL ORDER BY DESC using multiple table columns
We will use the MySQL ORDER BY DESC clause with multiple columns of the table in the query and see the result set when executed. The MySQL statement querying on the same table Customer as the previous one is as follows:
SELECTCustomerID, CustomerName, City FROM Customer ORDER BY CustomerName DESC, City DESC;
Here, we have used DESC keywords with both the Column names after the ORDER BY clause to produce the result set in descending order. Thus, in the case when one column is added DESC and the other not, it will automatically be sorted in ascending order.
Example #3 – MySQL ORDER BY DESC using WHERE clause
Suppose we need to generate the result set with customers having a credit limit greater than or equal to 3000from the same query as using the ORDER BY DESC clause to fetch the result rows. We will query the records with the WHERE clause as follows:
SELECT CustomerName, City, Credit_LimitFROM Customer WHERE Credit_Limit>=3000 ORDER BY City DESC;
Example #4 – MySQL ORDER BY DESC using the LIMIT option
We will apply the LIMIT clause, which is optional to provide a restriction for the result rows to be fetched. We will sue along with the ORDER BY DESC clause the LIMIT option at the end to see the results. Let us take the previous example and modifying it a bit as below:
SELECT * FROM Customer WHERE Credit_Limit>=1500 ORDER BY Credit_Limit DESC;
Here, we have simply retrieved the results having 6 rows with an order by column credit limit and WHERE clause. Now, using LIMIT, the query becomes as follows:
SELECT * FROM Customer WHERE Credit_Limit>=1500 ORDER BY Credit_LimitDESC LIMIT 3;
Now, see the output; the result rows are reduced to 3 as mentioned, and the credit limit is in descending order, as mentioned in the query above.
The ORDER BY clause accompanied by the DESC modifier attribute defines the sorting order of the table rows when fetched with the query. If any attribute is not specified in the query for the ORDER BY clause then, the result rows will be in increasing order by default.
In MySQL, the ORDER BY DESC clause can be applied in a SELECT LIMIT statement, SELECT statement, and DELETE LIMIT statement.
This is a guide to MySQL ORDER BY DESC. Here we discuss How ORDER BY DESC works in MySQL and Examples along with the outputs. You may also have a look at the following articles to learn more –