Phillip Schikora
Phillip Schikora

Reputation: 37

SQL, Databases, Joining two tables, get average

I have two tables one called Hotels and one called Ratings. I want to display the hotel names and then get the average rating for each hotel and display this. In the ratings table I might have 5 different ratings of a hotel so I want the average number and then display this. How would my query have to look like ?

Upvotes: 0

Views: 198

Answers (1)

Szymon
Szymon

Reputation: 43023

You didn't provide details but I assume you have a primary key in Hotels which is a foreign key in Ratings (HotelId). You didn't specify your RDBMS but each one should have a function to average and the query will look like:

select h.HotelId, avg(r.Rating)
from Hotels h
inner join Ratings r on h.HotelId = r.HotelId
group by h.HotelId

Upvotes: 1

Related Questions