In a SQL GROUP BY query, what value is used for the non-aggregate columns?

Posted by Queencity13 on Stack Overflow See other posts from Stack Overflow or by Queencity13
Published on 2011-02-28T15:20:47Z Indexed on 2011/02/28 15:25 UTC
Read the original article Hit count: 420

Filed under:

Say I've got the following data back from a SQL query:

Lastname Firstname Age
Anderson Jane      28
Anderson Lisa      22
Anderson Jack      37

If I want to know the age of the oldest person with the last name Anderson, I can select MAX(Age) and GROUP BY Lastname. But I also want to know the first name of that oldest person. How can I make sure that, when the Firstname values are collapsed into one row by the GROUP BY, I get the Firstname value from the same row where I got the max age?

© Stack Overflow or respective owner

Related posts about sql