-2

I'm currently taking the online standford class on databases, If you could help me solve this sql problem I would greatly appreciate it. Sorry I'm a complete noob.

Table Movie:

mID | title | year | director

Table Rating

rID | mID | stars | ratingDate

Table Reviewer

rID | name

For all pairs of reviewers such that both reviewers gave a rating to the same movie, return the names of both reviewers. Eliminate duplicates, don't pair reviewers with themselves, and include each pair only once. For each pair, return the names in the pair in alphabetical order.

2
  • 1
    Do you have some sample data? You want an average rating per movie and the unique names of the reviewers. Is that a correct translation?
    – Jacco
    Jan 12, 2013 at 21:09
  • @lester for quick and better answers - you at least need to provide short sample data and corresponding resultant table
    – exexzian
    Jan 12, 2013 at 21:33

6 Answers 6

2

Okay. You actually asked two different questions.

First, I'll answer the question you asked in the title of the post.

This should work.

select Movie.title, avg(Rating.stars) as AR from
Rating join Movie on Rating.mID = Movie.mID
group by Movie.title
order by AR desc

If you don't specify the "on" clause in the join, like this,

select Movie.title, avg(Rating.stars) as AR from
Rating join Movie
group by Movie.title
order by AR desc

then all movies will have the same rating, the global average, to due the full cross product of the join. You want an inner join on name or, in this case, id.

Second, here is the answer to the other question you asked, about pairs of reviewers. R1.name < R2.name ensures the reviewer names will be sorted left to right. and order by R1.name ensures those pairs will be sorted top to bottom.

select distinct R1.name, R2.name from
Rating Ra1 join Rating Ra2
join Reviewer R1 join Reviewer R2
where Ra1.mID = Ra2.mID and Ra1.rID != Ra2.rID
and R1.name < R2.name
and Ra1.rID = R1.rID and Ra2.rID = R2.rID
order by R1.name
0

This will get you started.

SELECT m.*, ra.*, re.*
FROM Movie m
JOIN Rating ra ON ra.mID = m.mID
JOIN Reviewer re ON re.rID = ra.rID
0

Find the average rating per movie:

SELECT 
    [m].[mID],
    [m].[title],
    AVG([r].[stars]) AS [AvgRating]
  FROM [Movie] [m]
  LEFT JOIN [Rating] [r] ON [m].[mID] = [r].[mID]
  GROUP BY 
    [m].[mID],
    [m].[title]

Find the unique reviewers per movie:

SELECT
    [r].[mID],
    [v].[rID],
    [v].[name]
  FROM [Reviewer] [v]
  INNER JOIN [Rating] [r] ON [v].[rID] = [r].[rID]
  GROUP BY
    [v].[rID],
    [v].[name],
    [r].[mID]

Combine:

SELECT 
  [rat].[mID],
  [rat].[title],
  [rev].[rID],
  [rev].[name],
  [rat].[AvgRating]
FROM
(
  SELECT 
    [m].[mID],
    [m].[title],
    AVG([r].[stars]) AS [AvgRating]
  FROM [Movie] [m]
  LEFT JOIN [Rating] [r] ON [m].[mID] = [r].[mID]
  GROUP BY 
    [m].[mID],
    [m].[title]
) AS [rat]
LEFT JOIN
(
  SELECT
    [r].[mID],
    [v].[rID],
    [v].[name]
  FROM [Reviewer] [v]
  INNER JOIN [Rating] [r] ON [v].[rID] = [r].[rID]
  GROUP BY
    [v].[rID],
    [v].[name],
    [r].[mID]
) AS [rev] ON [rat].[mID] = [rev].[mID]
ORDER BY 
  [rat].[mID] ASC,
  [rev].[name] ASC

For a Fiddle, look here.

0

Maybe something like this:

SELECT rev.rID, rev.name, m.title
FROM [Reviewer] rev 
   JOIN [Rating] rate ON rev.rID = rate.rID
   JOIN [Movie] m ON rate.mID = m.mID
   JOIN (
     SELECT mID
     FROM [Rating] 
     GROUP BY mID
     HAVING COUNT(Distinct rID) > 1
   ) m2 on m.mID = m2.mID
ORDER BY rev.name

--EDIT

If you need to get the average as well:

SELECT rev.rID, rev.name, m.title, ar.avgRating
FROM [Reviewer] rev 
   JOIN [Rating] rate ON rev.rID = rate.rID
   JOIN [Movie] m ON rate.mID = m.mID
   JOIN (
     SELECT mID
     FROM [Rating] 
     GROUP BY mID
     HAVING COUNT(Distinct rID) > 1
   ) m2 on m.mID = m2.mID
   JOIN (
     SELECT mID, AVG(stars) as avgRating
     FROM [Rating] 
     GROUP BY mID
   ) ar on m.mID = ar.mID
ORDER BY rev.name

And the SQL Fiddle.

3
  • Your missing the title of this post... "average rating"
    – Jacco
    Jan 12, 2013 at 21:10
  • Hehe -- didn't even read the title, just read the paragraph asking for what he needed returned. I can add that :-) Thanks @Jacco.
    – sgeddes
    Jan 12, 2013 at 21:12
  • I read the paragraph too and was then looking for the real question... Found it in the title. But your answer is off for the part "don't pair reviewers with themselves", when using a larger data sample.
    – Jacco
    Jan 12, 2013 at 21:51
0
select distinct r2.name, r1.name
from 
(select r1.rid, r1.mid, r2.name
from (select * from reviewer order by name) r2
join (select r1.rid, r1.mid from rating r1 join reviewer r2 on r1.rid = r2.rid order by r2.name) r1
on r2.rid = r1.rid
group by mid
order by r2.name) r1,
(select r1.rid, r1.mid, r2.name
from reviewer r2,
rating r1
where r1.rid = r2.rid)r2
where r1.rid <> r2.rid and r1.mid = r2.mID
order by r2.name, r1.name
0

I'm doing the same course and this is the answer I got:

SELECT DISTINCT name1, name2
FROM (SELECT R1.name AS name1, R2.name AS name2
FROM (Rating JOIN Reviewer using(rId)) AS R1,
(Rating JOIN Reviewer using(rId)) AS R2
WHERE R1.mId = R2.mId AND R1.rId <> R2.rId) AS Tuple
WHERE name1 < name2;

I don't know why OP talk about average on title, since this question don't ask for it.

EDIT: for further reference, here's the data: https://class.stanford.edu/c4x/DB/SQL/asset/moviedata.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.