x
+----+--------------------+
| id | name | age |
+----+--------------------+
| 1 | James | 20 |
| 2 | Ruth | 20 |
| 3 | Blue | 15 |
| 4 | Redd | 15 |
| 5 | Arkk | 30 |
| 6 | Arthur | 30 |
Expected results:
+----+--------------------+
| id | name | age |
+----+--------------------+
| 1 | James | 20 |
| 3 | Blue | 15 |
| 6 | Arthur | 30 |
Explanation:
In the results, James is representing all people aged 20
. And Blue is representing all people aged 15
. And so on..
Question: How can this query type of query be achieved? Thank you.
Advertisement
Answer
If you want one name per age, you can use:
select max(name), age
from t
group by age;
If you want the first row, then:
select t.*
from t
where t.id = (select min(t2.id) from t t2 where t2.age = t.age);