It is an incredibly common scenario where I want to select the row (or rows) that have either the maximum or minimum value for some column - often a datetime stamp of some kind. It would seem logical that a simple way to do this would be something like this:
SELECT *
FROM MyTable
WHERE DateColumn = MAX(DateColumn)
This, of course, is not allowed. Aggregate functions are not allowed in a WHERE clause (though I don't know why, exactly). One could use a HAVING clause, but this doesn't actually work either:
SELECT *
FROM MyTable
HAVING DateColumn = MAX(DateColumn)
Instead, the only solutions seem to be some variation of a subquery, something like this:
SELECT *
FROM MyTable
WHERE DateColumn = (
SELECT MAX(DateColumn)
FROM MyTable
)
Why is such a common need made so complicated? The intent of both of my examples above seems quite obvious, so why can't the SQL compiler be made to understand them? Or, if there is some technical reason why the existing implementation of WHERE cannot handle this syntax, why has no simple syntax been added to the language? I run into this particular need very frequently, and I see from searching online that I am hardly the only one. It would seem like the language should have accounted for this LONG ago, but it never has. Is there some serious technical or logical limitation I am missing here that makes this unrealistic?