HOWTO · MySQL

How to Sort MySQL Data in Alphabetical Order

Learn how to sort MySQL rows by a text column in ascending or descending alphabetical order.

On this page

To retrieve MySQL rows by name from A to Z, add ORDER BY followed by the text column to your SELECT query. Ascending order is the default; add DESC to reverse it from Z to A. The examples below use the student_details table and its stu_firstName column.

Create a Table in MySQL

The sample table has an ID, a first name, and a last name. To create it, run CREATE TABLE student_details (stu_id INT, stu_firstName VARCHAR(255) DEFAULT NULL, stu_lastName VARCHAR(255) DEFAULT NULL, PRIMARY KEY (stu_id));. Then add the sample records with INSERT INTO student_details (stu_id, stu_firstName, stu_lastName) VALUES (1, 'Preet', 'Sanghavi'), (2, 'Rich', 'John'), (3, 'Veron', 'Brow'), (4, 'Geo', 'Jos'), (5, 'Hash', 'Shah'), (6, 'Sachin', 'Parker'), (7, 'David', 'Miller');. The primary key gives each student row a distinct ID; the names are the values used in the sorting examples. These SQL statements are query examples; their results depend on running them against a MySQL database.

To inspect the inserted rows, run SELECT * FROM student_details;. This query does not specify a sort order, so the database is free to return rows in an order that is not alphabetical. Do not rely on the insertion order as a sorting guarantee; request the order you need in each query.

Sort Data in Alphabetical Order in a MySQL Database

The sorting clause is ORDER BY; ORDER by itself is not enough. Its general form is SELECT columns FROM table_name ORDER BY column_name;. For the sample, select just the first names with SELECT stu_firstName FROM student_details ORDER BY stu_firstName;. Because ascending order is the default, this arranges the names alphabetically from A to Z. The resulting name sequence is David, Geo, Hash, Preet, Rich, Sachin, Veron.

You can write ASC explicitly to make the direction clear: SELECT stu_firstName FROM student_details ORDER BY stu_firstName ASC;. It has the same ascending direction as leaving the direction out. ASC and DESC are direction keywords that follow the column name in the ORDER BY clause.

To reverse the sequence, append DESC: SELECT stu_firstName FROM student_details ORDER BY stu_firstName DESC;. The result is Veron, Sachin, Rich, Preet, Hash, Geo, David. This is descending alphabetical order, from Z toward A. The direction affects the order of returned rows, not the stored values; it does not rearrange the table itself. Since the example selects only stu_firstName, the result contains one name column. Add stu_id or stu_lastName to the SELECT list if you want the sorted result to show more details; sorting still comes from the column named after ORDER BY.

Adapt the query by replacing student_details with your table name and stu_firstName with the text column you want to sort. Keep the column in the ORDER BY clause even if you select other columns, such as SELECT stu_firstName, stu_lastName FROM student_details ORDER BY stu_firstName;. Use the default or ASC for A-to-Z order and DESC for the reverse. The actual order of text values follows the column’s MySQL collation, so this basic example demonstrates the supplied English names and does not define language-specific sorting rules.

If the query also filters rows, place ORDER BY after WHERE; if it limits the number of returned rows, place LIMIT after the ordering clause. For example, the shape is SELECT columns FROM table_name WHERE condition ORDER BY column_name DESC LIMIT row_count;. MySQL first applies the filter, then orders the matching rows, and finally returns up to the requested count. The sorting column must be available to the query, and the direction belongs after that column name.