CS50 Week 7: Movies
By: Sarah Hwozdyk
Tags: Cs50sql, Sql, Beginners Guide, Cs50, Programming

Some basics tips for those starting pset7.

This week I started working on Harvard’s CS50 week 7 and came up with some simple tips for those tackling the problem set for the first time. The following article provides the necessary tools for completing problem set Movies without providing answers.

SQL is helpful when it comes to data analysis. It allows the user to access data from a table, or series of tables, to answer some question. For example, if I imported a data table from IMDb (like in week 7’s assignment Movies) and wanted to find the names of movie produced in 2008. I would pose a query as such,

SELECT title

FROM movies

WHERE year = 2008;

The code above is based on a single table call “movies” that contains the variables title, year, and id. Id, in this example, represent a unique identifying number assigned to each row of data. These are important for identifying each movie and become even more useful when dealing with multiple tables.

The words, SELECT, FROM, and WHERE, represent commands and they are the bones of the query. SELECT is the desired output, in this case movie titles, FROM tells the location of the titles, found in the “movies” table, and WHERE is the constraint being put on the output. If I were to read the code in layman’s terms I would say, “SELECT the titles column FROM the table called movies WHERE the year produced is 2008”.

If the question ask to be more specific, for example to order the output alphabetically by title you can use the command “ORDER BY title” followed by ASC or DESC, meaning ascending or descending order respectively. Another useful feature is the LIMIT command which restrict the amount of output. I normally add limits when checking if my code complies or to see the top results from a set of output.

More Than One Table

Now that was breezy but what about dealing with two or more tables? Often times our data isn’t nicely ordered into one specific place. This is where the command JOIN comes into play. To generalize things I am going to step away from the movie example. Adding JOIN to your program is as follows,

SELECT variable_1

FROM table_1

JOIN table_2 ON table_1 . id = table_2 . id

I can JOIN as many tables as needed as long as their is a unique identifier common between the tables being joined. These identifiers may have different column names but their values should be the same. It is a good idea to check that the value, used to define one specific row, is the same in the tables you would like to join. Once you’ve joined the two tables, you can use variables present in either table for adding conditions or for ordering data.

Subqueries

Subqueries are the final tool that you will need to successfully complete assignment Movies. A subquery, also referred to as a nested query, is essentially a query within a query. It can be added to a number of commands including SELECT, FROM, or WHERE as well as other commands not mentioned in this article. Below is an example of one way to use subqueries.

SELECT variable_1

FROM table_1

JOIN table_2 ON table_1.id = table_2.id

WHERE variable_3 IN (SELECT variable FROM table_x WHERE condition);

Varaible_3 represents a variable in either table 1 or 2 the subquery that follows adds a condition on variable_3 that is applied to the output of variable_1. For example question 8 asked for the names of the stars featured in Toy Story.

SELECT name

FROM people

JOIN stars ON people.id = stars.person_id

WHERE movie_id IN (SELECT id FROM movies WHERE title = “Toy Story”);

In this example I am working with three tables: people, stars, and movies. The question requires the actors names which can be found in the “people” table. Because I am only concerned about the stars of the movie I will JOIN the “stars” table through their unique identifiers. Then, from the “stars” table I put a condition the movie_id’s to only give id’s where the movie title is Toy Story.

These are all the tools that I used to successfully complete assassinent Movies.