Is this valid SQL? If yes, could you please tell me what it does?
Select *
from MyFirstTable
order by (select min(somefield)
from MySecondTable
where MyFirstTable.id = MySecondTable.id)
A subselect in an "order by", how is that possible?? In effect this SQL query does not sort by a field, but by some value in a row of a field (min). It does not seem logical so sort by anything else other than a field name. But min(somefield) <> somefield! But, yes, this query works and someone at work who teaches me told me this, and i'm sceptical. Can you tell me what this means? Or just post an equivilant query?
Thanks!
This query orders
MyFirstTable
by the minimum value ofsomefield
stored inMySecondTable
under the sameid
.Here's a quick example:
In the above case, your query would return