Reputation: 95
I've written a query that pulls out the average score per player who was in inning 4:
SELECT batsmen.player_id, AVG(score)
FROM batsmen
WHERE batsmen.inning_no=4
GROUP BY player_id;
But now I need to find the highest average score. obviously i could look at the output, orderby and easily see the highest average, but i would like a query that pulls it out for me.
I assume somehow i need to use
SELECT MAX(score);
with the result set received in my avg query above. Do I need to join/subquery?
Thanks Theresa
Upvotes: 1
Views: 5299
Reputation: 941
I believe the SQL should be:
SELECT MAX(
SELECT AVG(score)
FROM batsmen
WHERE batsmen.inning_no=4
GROUP BY player_id
);
Upvotes: -1
Reputation: 15717
Perhaps this will fill your needs:
SELECT player_id, AVG(score)
FROM batsmen
WHERE batsmen.inning_no=4
GROUP BY player_id
ORDER BY 2 DESC
LIMIT 1;
Upvotes: 3
Reputation: 48357
Not tested, but try this:
SELECT player_id, avg_score
FROM (
SELECT batsmen.player_id,
AVG(score) avg_score
FROM batsmen
WHERE batsmen.inning_no=4
GROUP BY player_id
)
ORDER BY avg_score DESC
LIMIT 0,1;
C.
Upvotes: 0