Skip to content

Stanford SQL online course #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 14 additions & 23 deletions SQL exercise.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,16 @@ order by year;
Find the titles of all movies that have no ratings.

```sql
select title from Movie
left outer join Rating on Movie.mID = Rating.mID
select title from movie
left outer join Rating using (mID)
where stars is null;
```

Some reviewers didn't provide a date with their rating. Find the names of all reviewers who have ratings with a NULL value for the date.

```sql
select name from Rating
left outer join Reviewer
on Reviewer.rID = Rating.rID
select name from reviewer
join Rating using (rID)
where ratingDate is null;
```

Expand All @@ -64,29 +63,21 @@ order by name, title, stars;
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.

```sql
select name, title from Rating r1
join Rating r2 on(r1.rID=r2.rID and r1.mID=r2.mID)
join Movie on(Movie.mID = r1.mID)
join Reviewer on(Reviewer.rID = r1.rID)
where r1.stars < r2.stars
and r1.ratingDate < r2.ratingDate
and r1.rID in ( select rID from Rating
group by rID, mID
having count(*) = 2);
select name, title from rating r1
join rating r2 on (r1.mID = r2.mID and r1.rID = r2.rID)
join reviewer using (rID)
join movie on (Movie.mID = r1.mID)
where r1.stars > r2.stars and r1.ratingDate > r2.ratingDate;
```

For each movie that has at least one rating, find the highest number of stars that movie received. Return the movie title and number of stars. Sort by movie title.

```sql
select title, max(stars)
from Rating
join Movie on(Rating.mID = Movie.mID)
join Reviewer on(Rating.rID = Reviewer.rID)
where Rating.mID in
(select mID from Rating group by mID
having count(stars) >= 1)
group by title
order by title
select title, max(stars) from rating
join movie using (mID)
join reviewer using (rID)
group by mID
order by title;
```

For each movie, return the title and the 'rating spread', that is, the difference between highest and lowest ratings given to that movie. Sort by rating spread from highest to lowest, then by movie title.
Expand Down