¶Preface
when there is rain neither sun nor moon, the people actually do not think so.
Perhaps there are seasonal climate not to be cold rain, let the sun or the cool side.
Has the rain the night is otherwise a moonlit night no flavor.
Sometimes not reminiscent of Li Shangyin “when he cut a total of west window candle, but then when the famous hope of reunion among friends”
In the rain, fom the sky come the silk notes; here, quiet and comfortable.
¶Presentation
In this page, we will show the homework of the course of the Databases Technology in CMU. All of the questions we tested in the postgresql.
This section mainly is to practice the query operation of the SQL, include SQL view, index, etc. We also sure that all the sqls will pass in the mysql, sqlite… , this article also will provide the Chinese version in detail.
¶Question Details
In this homework you will have to write SQL queries to answer questions on a movie dataset, about movies and actors. The database contains two tables:
• movies(mid, title, year, num ratings, rating) , Primary key: mid
• play in(mid, name, cast position), Primary key: mid, name
The tables contain the obvious information: which actor played in what movie, at what position; for each movie, we have the title (eg., ’Gone with the wind’), year of production, count of rating reviews it received, and the average score of those ratings (a float in the range 0 to 10, with ’10’ meaning ’excellent’).
We will use Postgres, which is installed in the your own machines.
Question 1: Warm-up queries . . . . . . . . . . . . . . . . . . . . . . . . . [5 points]
(a) [2 points] Print all actors in the movie Quantum of Solace, sorted by cast position. Print only their names.
(b) [3 points] Print all movie titles that were released in 2002, with rating larger than 8 and with more than one rating (num ratings > 1).
Question 2: Find the star’s movies . . . . . . . . . . . . . . . . . . . . [5 points]
(a) [5 points] Print movie titles where Sean Connery was the star (i.e. he had position 1 in the cast). Sort the movie titles alphabetically.
Question 3: Popular actors . . . . . . . . . . . . . . . . . . . . . . . . . . [15 points]
(a) [8 points] We want to find the actors of the highest quality. We define their quality as the weighted average of the ratings of the movies they have played in (regardless of cast position), using the number of ratings for each movie as the
weight. In other words, we define the quality for a particular actor as
Print the names of the top 5 actors, according to the above metric. Break ties alphabetically.
(b) [7 points] Now we want to find the 5 most popular actors, in terms of number of ratings (regardless of positive or negative popularity). I.e, if actor ‘Smith’ played in 2 movies, with num ratings 10 and 15, then Smith’s popularity is 25 (=10+15). Print the top 5 actor names according to popularity. Again, break ties alphabetically.
Question 4: Most controversial actor . . . . . . . . . . . . . . . . [10 points]
(a) [10 points] We want to find the most controversial actor. As a measure of controversy, we define the maximum difference between the ratings of two movies that an actor has played in (regardless of cast position). That is, if actor ‘Smith’ played in a movie that got rating=1.2, and another that got rating=9.5, and all the other movies he played in, obtained scores within that range, then Smith’s contoversy score is 9.5-1.2= 8.3. Print the name of the top-most controversial actor - again, if there is a tie in first place, break it alphabetically.
Question 5: The minions . . . . . . . . . . . . . . . . . . . . . . . . . . . . [20 points]
(a) [20 points] Find the “minions” of Annette Nicole: Print the names of actors who only played in movies with her and never without her. The answer should not contain the name of Annette Nicole. Order the names alphabetically.
Question 6: High productivity . . . . . . . . . . . . . . . . . . . . . . . . [5 points]
(a) [5 points] Find the top 2 most productive years (by number of movies produced). Solve ties by preferring chronologically older years, and print only the years.
Question 7: Movies with similar cast . . . . . . . . . . . . . . . . [15 points]
(a) [8 points] Print the count of distinct pairs of movies that have at least one actor in common (ignoring cast position). Exclude self-pairs, and mirror-pairs.
(b) [7 points] Print the count of distinct pairs of moves that have at least two actors in common (again, ignoring cast position). Again, exclude self-pairs, and mirror pairs.
Question 8: Skyline query . . . . . . . . . . . . . . . . . . . . . . . . . . . [25 points]
(a) [25 points] We want to find a set of movies that have both high popularity (ie, high num ratings) as well as high quality (rating). No single movie may achieve both - in which case, we want the so-called Skyline query 2 . More specifically, we want all movies that are not “dominated” by any other movie:
Definition of domination : Movie “A” dominates movie “B” if movie “A” wins over movie “B”, on both criteria, or wins on one, and ties on the rest.
Figure 1 gives a pictorial example: the solid dots (’A’, ’D’, ’F’) are not dominated by any other dot, and thus form the skyline. All other dots are dominated by at least one other dot: e.g., dot ’B’ is dominated by dot ’A’, being inside the shaded rectangle that has ’A’ as the upper-right corner.
Figure 1: Illustration of Skyline and domination : ’A’ dominates all points in the shaded rectangle; ’A’, ’D’ and ’F’ form the skyline of this cloud of points.
Given the above description, print the title of all the movies on the skyline, along with the rating and the number of ratings.
¶Answer
we give the Postgres version in detail, we will see you can tranfer it easily in mysql or sqlite.
¶Initialization:
1 | ## drop the table if exists |
¶Insert Values
Insert into some values into the table movies and play_in,
you will find the datas just in the follow links in my 360 yunFiles:
https://yunpan.cn/cSfLzxQApRXSi password: f3ab
1 | ## use "copy" in Postgres |
The flowing image show the test infos in my ubuntu os:
¶Solution 1
1 | (a) SELECT name FROM play_in p, movies m |
¶Solution 2
1 | SELECT title from movies m, play_in p |
The flowing image show the test solution 2 infos in my ubuntu os:
¶Solution 3
1 | (a) DROP VIEW IF EXISTS WeigthedRatings; |
¶Solution 4
1 | DROP VIEW IF EXISTS RatingGap; |
The flowing image show the test solution 4 infos in my ubuntu os:
¶Solution 5
1 | DROP VIEW IF EXISTS MastersMovies CASCADE; |
¶Solution 6
1 | DROP VIEW IF EXISTS MoviesPerYear; |
¶Solution 7
1 | (a) SELECT COUNT(*) FROM |
¶Solution 8
1 | DROP VIEW IF EXISTS Dominated; |
¶Reference
[1] http://www.ruanyifeng.com/blog/2013/12/getting_started_with_postgresql.html
[2] http://www.postgresql.org/docs/
[3] http://www.cs.cmu.edu/~epapalex/15415S14/PostgreSQLReadme.htm
[4] http://www.cs.cmu.edu/~christos/courses/
[5] http://blog.chinaunix.net/uid-20685819-id-4267454.html