The left join restricts the returned values to only rows where the age in p2
is larger than the age in p1
, the WHERE
clause then says only return rows where p2.age IS NULL
, there can only be one value in p1
that does not have a value in p2
that is higher; the highest value (given that the tables are the same data the highest value cannot have a higher value).
Changing the join to be p1.age > p2.age
would return the lowest age.
EDIT - alternate solution
I think what may be a more efficient way to get the same result would be:
SELECT p1.Name, p1.Age
FROM persons p1
INNER JOIN (
SELECT max(p2.age) As MaxAge FROM persons p2) As q ON (p1.age = q.MaxAge)
Or maybe
SELECT p1.Name, p1.Age
FROM persons p1
WHERE p1.age = (SELECT max(p2.age) As MaxAge FROM persons p2)
Not a SQL master so there may be better ways, from a cursory glance at the execution plans they (the two alternatives above) look like they're doing the same but I'm working with a tiny amount of data.