Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Listing all column names alphabetically

I know that

SELECT * FROM Table 

will list all columns in the table, but I am interested in listing the columns in alphabetical order.

Say, I have three columns, "name", "age" and "sex".

I want the columns organized in the format

|age| |name| |sex| 

Is it possible to do this with SQL?

like image 866
Shamim Hafiz - MSFT Avatar asked Nov 02 '10 07:11

Shamim Hafiz - MSFT


People also ask

How do I get column names in alphabetical order in SQL?

The ORDER BY statement in SQL is used to sort the fetched data in either ascending or descending according to one or more columns. By default ORDER BY sorts the data in ascending order. We can use the keyword DESC to sort the data in descending order and the keyword ASC to sort in ascending order.

How do I sort a column alphabetically in MySQL?

Summary. Use the ORDER BY clause to sort the result set by one or more columns. Use the ASC option to sort the result set in ascending order and the DESC option to sort the result set in descending order. The ORDER BY clause is evaluated after the FROM and SELECT clauses.

How do I list all columns in SQL?

In a query editor, if you highlight the text of table name (ex dbo. MyTable) and hit ALT + F1 , you'll get a list of column names, type, length, etc.

How do I show alphabetical order in MySQL?

The MySQL ORDER BY Keyword The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.


2 Answers

This generates a query with all columns ordered alphabetically in the select statement.

DECLARE @QUERY VARCHAR(2000) DECLARE @TABLENAME VARCHAR(50) = '<YOU_TABLE>'  SET @QUERY = 'SELECT ' SELECT @QUERY = @QUERY + Column_name + ',  '   FROM INFORMATION_SCHEMA.COLUMNS   WHERE TABLE_NAME = @TABLENAME  ORDER BY Column_name  SET @QUERY =  LEFT(@QUERY, LEN(@QUERY) - 4) + '  FROM '+ @TABLENAME  PRINT @QUERY EXEC(@QUERY) 
like image 170
Maykol Rypka Avatar answered Oct 03 '22 12:10

Maykol Rypka


Yes, and no :-)

SQL itself doesn't care what order the columns come out in but, if you were to use:

select age, name, sex from ... 

you'd find that they probably came out in that order (though I'm not sure SQL standards mandate this).

Now you may not want to do that but sometimes life isn't fair :-)

You also have the other possibility of using the DBMS data definition tables to dynamically construct a query. This is non-portable but most DBMS' supply these table (such as DB/2's SYSIBM.SYSCOLUMNS) and you can select the column names from there in an ordered fashion. Something like:

select column_name from sysibm.syscolumns where owner = 'pax' and table_name = 'movies' order by column_name; 

Then you use the results of that query to construct the real query:

query1 = "select column_name from sysibm.syscolumns" +          " where owner = 'pax' and table_name = 'movies'" +          " order by column_name" rs = exec(query1) query2 = "select" sep = " " foreach colm in rs:     query2 += sep + colm["column_name"]     sep = ", " query2 += " from movies order by rating" rs = exec(query2) // Now you have the rs recordset with sorted columns. 

However, you really should critically examine all queries that select * - in the vast majority of cases, it's unnecessary and inefficient. And presentation of the data is something that should probably be done by the presentation layer, not the DBMS itself - the DBMS should be left to return the data in as efficient a manner as possible.

like image 22
paxdiablo Avatar answered Oct 03 '22 11:10

paxdiablo