MySQL sorts data alphabetically
In this article, we will learn about various ways to sort data alphabetically in MySQL.
Sort MySQL data alphabetically
When you use the SELECT command to query data from a table, the rows in the result set are in arbitrary order. To order the rows in the result set, you must include an ORDER BY clause in the SELECT statement.
When you run a SELECT statement that contains an ORDER BY clause, MySQL always evaluates the ORDER BY clause after the FROM and SELECT clauses.
syntax:
SELECT column_name FROM table_name ORDER BY column_name ASC;
SELECT column_name FROM table_name ORDER BY column_name DESC;
SELECT column_name FROM table_name ORDER BY column_name ASC|DESC, column_name2 ASC|DESC;
The terms ASC and DESC stand for ascending and descending, respectively. ASC and DESC are used to sort the result set in ascending and descending order.
If you do not explicitly indicate any selection, the ORDER BY clause defaults to ASC. Therefore, the following ORDER BY clauses are interchangeable:
SELECT column_name FROM table_name ORDER BY column_name ASC;
SELECT column_name FROM table_name ORDER BY column_name;
To further understand the previous concepts, consider the following example:
SELECT name FROM Employees ORDER BY name ASC;
SELECT name FROM Employees ORDER BY name DESC;
The Name column values are displayed in the preceding example, first in ascending order and then in descending order.
All names that begin with A and end with Z will be displayed in ascending order. Likewise, all names that begin with Z and end with A will be displayed in descending order.
Run the above line of code in any browser compatible with MySQL. It will display the following result:
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Display tables and database structure in MySQL
Publish Date:2025/04/23 Views:97 Category:MySQL
-
Today, we will learn about queries in MySQL that can display the table and database structure. We will use the mysqldump utility, DESCRIBE the , SHOW TABLES and SHOW CREATE TABLE the statements. We are using MySQL version 8.0.28 while writi
Select first row from MySQL table
Publish Date:2025/04/23 Views:112 Category:MySQL
-
Today, we will explore three scenarios and their solutions where we want to select the first row from a MySQL table. In the first scenario, we will learn to get the first row from a MySQL table where there are multiple instances of a partic
Insert timestamp into MySQL table
Publish Date:2025/04/23 Views:77 Category:MySQL
-
Today, we will learn how to TIMESTAMP insert date and time into a type column of a MySQL table according to the table definition. Create a MySQL table First, we will create the tables that we will use in this tutorial. Sample code: CREATE T
The difference between two tables in MySQL
Publish Date:2025/04/23 Views:102 Category:MySQL
-
In this article, we will learn how to find the difference between two tables in MySQL. The difference between two tables in MySQL We often need to compare two tables to find records in one table that have no matching records in the other ta
Converting from datetime type to date-only in MySQL
Publish Date:2025/04/23 Views:199 Category:MySQL
-
Today, we will learn the DATE(), CAST(), CONVERT() and DATE_FORMAT() methods to convert DATETIME type to DATE type in MySQL. The above mentioned methods can be used in MySQL 4.0 and above. Converting from DATETIME to DATE in MySQL To unders
Changing max_allowed_packet Size in MySQL Server
Publish Date:2025/04/22 Views:193 Category:MySQL
-
This article explains how to change the max_allowed_packet size in MySQL server. To understand this, we will use two operating systems, Windows 10 and Linux (Ubuntu). Changing max_allowed_packet Size in MySQL Server If we try to upload a fi
Zerofill usage, advantages and alternatives in MySQL
Publish Date:2025/04/22 Views:196 Category:MySQL
-
In this article we will understand the uses, advantages and alternatives of ZEROFILL attribute in MySQL. Use and benefits of the ZEROFILL attribute in MySQL The benefit of using the ZEROFILL attribute is that it has nothing to do with input
Compare only MySQL timestamp dates to date parameters
Publish Date:2025/04/22 Views:64 Category:MySQL
-
In this article we will use the DATE() , CAST() , and CONVERT() functions to compare MySQL timestamp dates with only the date parameter. DATE() vs. CAST() vs. CONVERT() in MySQL Below is a brief description of each function. You can also fi
Calculating Percentages in MySQL
Publish Date:2025/04/22 Views:67 Category:MySQL
-
We will use one or more columns to calculate percentages in MySQL. There are different ways to do this, and for each method we will use an example table. Calculate percentage using a column in MySQL We have a table called sales where ID, Re