Tuesday, September 14, 2010

Database:Records Sorting


Here is my question, how do you sort the records of the displayed data of SQL query result?

Answer is simple by using the ASC and DESC keyword (i.e. Ascending and descending followed by column name or list of column name separated by comma, where ascending is default).

Absolutely correct, but have you tried it without the column name and replacing the column name by number like 1 , 2 etc. 

No, then there you go:

Traditional query,

Select columnA, columnB, columnC from tableA order by columnA, columnB

Now try this query 

Select columnA, columnB, columnC from tableA order by 1,2

Both the above query will result the same.

Here 1,2, 3 represent the column name in the select statements.

The same is true for group by also

Note: This is in Oracle DB , not sure in other DB

No comments:

Post a Comment