Home > Miscellaneous > SQL order by without using Column Name

SQL order by without using Column Name

While working with dynamic queries or otherwise when we are not sure of the number of columns that will be returned from the query or simply due to laziness for typing the entire column name we can use the index of the column by whom we want our result set to be sorted.

 

For instance lets say my dynamic query returns 4 to 10 columns randomly and we want the result set to be sorted by the second column in any which case we can simply write order by 2 in place of order by second column.

 

Eg.

 

both the above cases will give the same result

 

Post comments if this helps

This Article is TAGGED in , , , , , . BOOKMARK THE permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">