If you use an aggregate and a non-aggregate in a selection set at once, then the row used for the non-aggregate field is essentially randomly selected.
In principle, how much it works max - it collects all the rows for each group on request (if there is no group, all of them), calculates max and puts it in the result.
But since you also put in a non-aggregate field, you need a value for that - so SQL does just select a random row. You might think, "well, why doesn't he select the same row as he did?" but what if you used avg or count? They do not have a string associated with it, so the best thing is to choose it at random. That is why this behavior exists at all.
What you need to do is use a subquery. Something like select d1.id from data_diri d1 where d1.graduate_year - d1.join_year = (select max(d2.graduate_year - d2.join_year from data_diri d2))
source share