Hi all! I am just beginning to learn the ins and out of database querying and have been stuck on this particular query for quite a while.
I am trying to list the names of players who are above average age of their teammates.
A few of my attempts have gotten close, but are not returning the right results.
Ideally I am trying to find the average age grouped by team, and test each player to see if they are older than that value.
My relational schema are as follows:
player(player_name, age, field_position)
plays_for(player_name, team_name)
This is my proposed query that runs without error, but returns me players above the total average age, not the average age of their respective team.
Any and all help or insight would be greatly appreciated.
A friend suggested perhaps making 2 copies of players and plays_for, but I'm unfamiliar with that technique.
SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
select A.player_name
from plays_for A
join player B
on A.player_name = B.player_name
where B.age > (select avg(age) from player
group by A.team_name