- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get table column names in alphabetical order in MySQL?
To get the table column names in alphabetical order, you need to use ORDER BY. The syntax is as follows −
SELECT anyReferenceName.COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS anyReferenceName WHERE anyReferenceName.TABLE_NAME = ’yourTableName’ ORDER BY anyReferenceName.COLUMN_NAME
First, we need to get all the columns and then we need to use ORDER BY. In the above query, we are getting all columns using INFORMATION_SCHEMA.COLUMNS.
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table ColumnsOrder -> ( -> StudentFirstName varchar(20), -> Id int, -> StudentAge int, -> StudentLastName varchar(20) -> ); Query OK, 0 rows affected (0.90 sec)
Implement the above syntax to get table columns in alphabetical order.
Case 1 − By default, ORDER BY gives ascending order.
The query is as follows −
mysql> select ref.column_name from information_schema.columns ref -> where ref.table_name = 'ColumnsOrder' -> order by ref.column_name;
The following is the output −
+------------------+ | COLUMN_NAME | +------------------+ | Id | | StudentAge | | StudentFirstName | | StudentLastName | +------------------+ 4 rows in set (0.13 sec)
Case 2 − If you want in descending order, then use DESC command in the end.
The query is as follows −
mysql> select ref.column_name from information_schema.columns ref -> where ref.table_name = 'ColumnsOrder' -> order by ref.column_name desc;
The following is the output −
+------------------+ | COLUMN_NAME | +------------------+ | StudentLastName | | StudentFirstName | | StudentAge | | Id | +------------------+ 4 rows in set (0.00 sec)
- Related Articles
- C program to sort names in alphabetical order
- C program to sort names in alphabetical order using structures
- Get all the column names in a table in MongoDB
- What is the syntax in MySQL to get the column names of a table?
- C program to sort names in alphabetical order with string functions.
- Get table names using SELECT statement in MySQL?
- How to display the column names from a table excluding some in MySQL?
- Fetch specific rows from a MySQL table with duplicate column values (names)?
- MySQL Quoted table/field names vs unquoted names?
- Set Blank spaces in column names with MySQL?
- Select column names containing a string in MySQL?
- How to can I get the names of my MySQL table columns?
- Get only the file extension from a column with file names as strings in MySQL?
- Setting column values as column names in the MySQL query result?
- Order By Length of Column in MySQL

Advertisements