SQL Listing all column names alphabetically

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)

  • There is no way to do this automatically without dynamic SQL.
  • SELECT * is not recommended and will not sort column names
  • You'd have to explicitly do SELECT age, name, sex FROM

At the SQL level, it does not matter. Not does it matter to any client code object-

If it's important, then sort when you present the data to the client.

Sorry, it just is that way...


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.


SQL-92 Standard specifies that when using SELECT * the columns are referenced in the ascending sequence of their ordinal position within the table. The relevant sections are 4.8 (columns) and 7.9 (query specification). I don't know of any vendor extensions to the Standard that would allow columns to be returned in any other order, probably because use of SELECT * is generally discouraged.

You can use SQL DDL to ensure that columns' ordinal positions match the desired alphabetical order. However, this will only work in the way you want when referening a sinlge table in the FROM clause. If two tables are referenced, SELECT * will return the columns from the first table in ordinal position order followed by the second table's columns in ordinal position, so the complete resultset's columns may not be in alphabetical order.