Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL - Comparing two rows and two columns
    primarykey
    data
    text
    <p>I'm studying SQL and can't seem to find an answer to this exercise.</p> <p><em>Exercise: For all cases where the same reviewer rated the same movie twice and gave it a higher rating the second time, return the reviewer's name and the title of the movie.</em> </p> <p>I don't know how to compare 2 rows and then get the higher rating.</p> <p>The tables' schemas are:</p> <ul> <li>Movie ( mID, title, year, director ) English: There is a movie with ID number mID, a title, a release year, and a director.</li> <li>Reviewer ( rID, name ) English: The reviewer with ID number rID has a certain name. </li> <li>Rating ( rID, mID, stars, ratingDate ) English: The reviewer rID gave the movie mIDa number of stars rating (1-5) on a certain ratingDate.* </li> </ul> <p>Researching here in the forum I've got as far as to this point:</p> <pre><code>select * from rating a join Reviewer rv on rv.rid = a.rid where 1 &lt; (select COUNT(*) from rating b where b.rid = a.rid and b.mid = a.mid) </code></pre> <p>I'd be glad to be given also an explanation of the code. Since even the code above is making me really confused.</p> <pre><code>/* Create the schema for our tables */ create table Movie(mID int, title text, year int, director text); create table Reviewer(rID int, name text); create table Rating(rID int, mID int, stars int, ratingDate date); /* Populate the tables with our data */ insert into Movie values(101, 'Gone with the Wind', 1939, 'Victor Fleming'); insert into Movie values(102, 'Star Wars', 1977, 'George Lucas'); insert into Movie values(103, 'The Sound of Music', 1965, 'Robert Wise'); insert into Movie values(104, 'E.T.', 1982, 'Steven Spielberg'); insert into Movie values(105, 'Titanic', 1997, 'James Cameron'); insert into Movie values(106, 'Snow White', 1937, null); insert into Movie values(107, 'Avatar', 2009, 'James Cameron'); insert into Movie values(108, 'Raiders of the Lost Ark', 1981, 'Steven Spielberg'); insert into Reviewer values(201, 'Sarah Martinez'); insert into Reviewer values(202, 'Daniel Lewis'); insert into Reviewer values(203, 'Brittany Harris'); insert into Reviewer values(204, 'Mike Anderson'); insert into Reviewer values(205, 'Chris Jackson'); insert into Reviewer values(206, 'Elizabeth Thomas'); insert into Reviewer values(207, 'James Cameron'); insert into Reviewer values(208, 'Ashley White'); insert into Rating values(201, 101, 2, '2011-01-22'); insert into Rating values(201, 101, 4, '2011-01-27'); insert into Rating values(202, 106, 4, null); insert into Rating values(203, 103, 2, '2011-01-20'); insert into Rating values(203, 108, 4, '2011-01-12'); insert into Rating values(203, 108, 2, '2011-01-30'); insert into Rating values(204, 101, 3, '2011-01-09'); insert into Rating values(205, 103, 3, '2011-01-27'); insert into Rating values(205, 104, 2, '2011-01-22'); insert into Rating values(205, 108, 4, null); insert into Rating values(206, 107, 3, '2011-01-15'); insert into Rating values(206, 106, 5, '2011-01-19'); insert into Rating values(207, 107, 5, '2011-01-20'); insert into Rating values(208, 104, 3, '2011-01-02'); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload