The rather complex SQL query that I was working on made me think about the SQL (ANSI) constraint:
Is there a way to get the maximum or minimum record with respect to an arbitrary order?
In other words:
Given such a request:
SELECT * FROM mytable WHERE <various conditions> ORDER BY <order clause>
Is it possible to write a query that returns only the first row (perhaps by converting the order clause to another)?
I know that you can do this using LIMIT (MySQL) / ROWNUM (Oracle) or similar, but this is not standard SQL.
I also know that you can do this by extracting the maximum value that interests you in the subquery (using MIN () / MAX ()), and then use this 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 this only works if I want to sort by one column. I do not see the possibility of generalizing this to several columns (except for the nesting of the above solution, but this will mean 2 ^ n SELECT when ordering by n coluns).
So, is there a better way in standard SQL than nesting multiple subqueries?
A related question is asked in Create an SQL query to retrieve the latest records . However, the answers there suggest using LIMIT and friends or using a subquery with MAX (), as explained above, both of which are not the solution to my question.
source share