Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sort results without use of order by clause

Tags:

sql

database

I am asked a question in interview that sort result without using of order by clause and without using the script like php. I enough google it but did not find the way. Is there any method to sort results in this way. And it should be without using of any script like php etc.

like image 281
nbhatti2001 Avatar asked Feb 20 '12 06:02

nbhatti2001


2 Answers

You cannot, at least not reliably.

Some SQL implementations may well return rows in the order of their primary keys or clustered indexes, but SQL itself is a relational algebra that returns arbitrarily ordered sets unless specifically told otherwise.

There's a good chance that the order in which rows are returned may well depend on the insertion and deletion activity since the table was created.

My answer to an interview question like that would be:

Is there some sort of reason why we can't use 'order by' in our queries? Is the company so hard up for money that they cannot afford the disk space to store those extra few bytes for each query? Are you out of you goddamned mind? Ask me a question that's going to have some relevance :-)

like image 113
paxdiablo Avatar answered Sep 21 '22 22:09

paxdiablo


I expect they were fishing to see if you knew that most SQL implementations in general do not guarantee the order of rows returned unless you explicitly use an order by clause.

like image 25
My Other Me Avatar answered Sep 18 '22 22:09

My Other Me