A rather complicated SQL query I was working on got me thinking about a limitation of (ANSI) SQL:
Is there a way to retrieve a record that is maximal or minimal with respect to an arbitrary ordering?
In other words:
Given a query like this:
SELECT * FROM mytable WHERE <various conditions> ORDER BY <order clause>
is it possible to write a query that returns only the first row (possibly by transforming the order clause into something else)?
I know you can do this using LIMIT (MySQL) / ROWNUM (Oracle) or similar, but that's not standard SQL.
I also know you can do this by fetching the max/min value you are interested in in a subquery (using MIN()/MAX()), then use that result as a criterion in your main SELECT, i.e.:
SELECT * FROM mytable WHERE <various conditions> AND myMaxColumn=(
SELECT MAX(myMaxColumn) FROM mytable WHERE <various conditions>
)
But that only works if I want to sort by a single column. I see no way to generalize this to multiple columns (other than nesting the above solution, but that would mean 2^n SELECTs when ordering by n coluns).
So is there a better way in standard SQL than nesting multiple subselects?
A related question is asked in Create a SQL query to retrieve most recent records. However, the answers there suggest either using LIMIT & friends, or to use a subquery with a MAX() as explained above, both of which are not solutions to my question.