Sekhar Dutta
Sekhar Dutta

Reputation: 109

Join with other table and get aggregated d

I have two table User and Score, each user have multiple score. How can I query average score with user name:

Ex: Jack 6 Sham 5

User

Name    user_id
Jack   123
Tony   234
Sham   456

Score

id     score    user_id
1        4        123
2        8        123
3        9        234
4        2        456
5        10       456
6        3        456

Upvotes: 0

Views: 37

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269443

If I understand, this is a join and group by:

select u.name, avg(s.score)
from users u join
     scores s
     using (user_id)
group by user_id, u.name;

Note that I've included user_id in the group by, in case two users have the same name.

Upvotes: 1

Related Questions