Lab 10: SQL
- Due: Wednesday 11/18 @ 11:59pm
- Points: 1
- Download: lab10.zip
Attendance
You need to submit the lab problems in addition to attending to get credit for lab. Students in mega lab only need to submit the lab problems. Preceptor interviews are highly recommended but not required for lab submissions.
If you are in regular lab, your TA will come around during lab to check you in. If you didn't attend for a good reason (such as being sick), fill out this form (within 2 weeks of your lab): attendance form.
There are no Preceptor interviews for this lab.
Setup
Download lab10.zip from the link at the top of this page, unzip it, and open
the lab10 folder in VS Code. The folder contains:
lab10.sql: The file you'll edit. Each question asks you to complete oneCREATE TABLEstatement.movies.sql: The movie data used in Questions 3 and 4.sqlite_shell.py: A program for running SQL interactively.tables.pyandtests/: Tests for the questions.
Running SQL
From the Terminal menu within VS Code, select New Terminal. To run all of
the statements in lab10.sql and then type in more statements yourself, run:
python3 sqlite_shell.py --init lab10.sql
The sqlite> prompt means it's ready for a statement. Every statement ends with
a semicolon; if you see ...> after pressing enter, you probably forgot one.
For example, once you've completed Question 1, you can see the rows of the
opening table:
sqlite> SELECT * FROM opening;
Sliver
La Val's
Artichoke
Type .quit or press Ctrl-C to exit. After you change lab10.sql, exit and
run the command again to see the result.
If you've installed SQLite on your computer, you can run sqlite3 --init lab10.sql instead, which works the same way.
SQL
A SELECT statement describes an output table based on input rows. To write
one:
- Describe the input rows using
FROMandWHEREclauses. - Format and order the output rows and columns using
SELECTandORDER BYclauses.
SELECT (Step 2) FROM (Step 1) WHERE (Step 1) ORDER BY (Step 2);
The WHERE and ORDER BY clauses are optional.
Consult the drop-down for a refresher on SQL. It's okay to skip directly to the questions and refer back here should you get stuck.
SQL
SELECT name, close - open FROM pizzas;has one output row for each row ofpizzas. The columns afterSELECTare expressions that can do arithmetic with the values in a row.+,-, and*work as they do in Python. On integers,/truncates toward zero (-7 / 2is-3), and%returns the corresponding remainder (-7 % 2is-1).ASnames a column:SELECT name, close - open AS hours FROM pizzas;WHEREkeeps only the input rows for which a condition is true. Conditions compare values with<,<=,>,>=,=(equal), and!=(not equal), and combine withAND,OR, andNOT. For example,WHERE open < 12 AND close >= 20. The<>comparison is the same as!=.ORDER BYsorts the output rows by an expression or a column name, from smallest to largest, or from largest to smallest when followed byDESC. To break ties, list more than one:ORDER BY close DESC, namesorts by decreasingclose, and rows with the sameclosebyname. Strings sort in alphabetical order.LIMIT nat the end keeps only the firstnoutput rows.CREATE TABLE opening AS SELECT ...;names the table that aSELECTstatement describes, so that it can be used later.--starts a comment, which lasts until the end of the line.
Getting Started Videos
These videos may provide some helpful direction for tackling the coding problems on this assignment.
To see these videos, you should be logged into your berkeley.edu email.
Required Questions
Pizza Time
The pizzas table at the top of lab10.sql contains the names, opening, and
closing hours of great pizza places in Berkeley.
CREATE TABLE pizzas (name TEXT, open INTEGER, close INTEGER);
INSERT INTO pizzas VALUES
("Artichoke", 12, 15),
("La Val's", 11, 22),
("Sliver", 11, 20),
("Cheeseboard", 16, 23),
("Emilia's", 13, 18);
Q1: Open Early
You'd like to have pizza before 13 o'clock (1pm). Create an opening table with
the names of all pizza places that open before 13 o'clock, listed in reverse
alphabetical order.
opening table:
| name |
|---|
| Sliver |
| La Val's |
| Artichoke |
Hint
To order by name in reverse alphabetical order, write ORDER BY name DESC.
CREATE TABLE opening AS
SELECT "REPLACE THIS LINE WITH YOUR SOLUTION";
python3 -m pytest -k openingQ2: Study Session
You're planning to study at a pizza place from the moment it opens until 14
o'clock (2pm). Create a study table with two columns: the name of each pizza
place that opens before 14 o'clock, and the duration of the study session you
would have if you studied there (the difference between 14 o'clock and when it
opens). The rows may appear in any order.
study table:
| name | duration |
|---|---|
| La Val's | 3 |
| Sliver | 3 |
| Artichoke | 2 |
| Emilia's | 1 |
Hint: Template
CREATE TABLE study AS
SELECT ____, ____ AS duration FROM ____ WHERE ____;
CREATE TABLE study AS
SELECT "REPLACE THIS LINE WITH YOUR SOLUTION";
python3 -m pytest -k studyIMDb
The IMDb 1000 dataset contains the 1000 highest rated popular movies (top 0.2%
of votes) from the
Internet Movie Database.
The line .read movies.sql at the top of lab10.sql creates a titles table
that has a row for each movie. Here are its columns:
tconst(text): A unique identifier for the movietitle(text): The name of the movieyear(integer): The year the movie was maderuntime(integer): How long the movie is, in minutesgenres(text): The movie's genres, separated by commas
The table is too large to look at all at once, so it's useful to add LIMIT to
a query to see just a few of its rows:
sqlite> SELECT title, year, runtime FROM titles LIMIT 3;
The Kid|1921|68
Nosferatu: A Symphony of Horror|1922|94
The Gold Rush|1925|95
Q3: Newest Movies
Create a newest table with two columns, the title and year of the 10
newest movies in the dataset, from newest to oldest. Order movies made in the
same year alphabetically by title.
If you'd like, see if you can guess which movies will be in the results.
Hint: Template
CREATE TABLE newest AS
SELECT ____, ____
FROM ____
ORDER BY ____
LIMIT ____;
- Use
FROMto specify which table to read the data from. - Use
ORDER BYto sort by which movies are the newest, breaking ties by title. - Use
LIMITto select only the 10 newest movies. - Use
SELECTto put the movie title and year in the output.
CREATE TABLE newest AS
SELECT "REPLACE THIS LINE WITH YOUR SOLUTION";
python3 -m pytest -k newestQ4: Long Movies
Create a long_movies table with two columns, the title of each movie made in
2000 or later that is over 3 hours (180 minutes) long, and the number of
extra minutes it runs beyond 3 hours. List the longest movies first.
long_movies table:
| title | extra |
|---|---|
| Gangs of Wasseypur | 141 |
| Zack Snyder's Justice League | 62 |
| Lagaan: Once Upon a Time in India | 44 |
| The Irishman | 29 |
| Killers of the Flower Moon | 26 |
| The Lord of the Rings: The Return of the King | 21 |
| Avatar: The Way of Water | 12 |
| Grindhouse | 11 |
| Swades | 9 |
| RRR | 7 |
| Avengers: Endgame | 1 |
Hint: Template
CREATE TABLE long_movies AS
SELECT ____, ____ AS extra
FROM ____
WHERE ____
ORDER BY ____;
- Use
FROMandWHEREto choose only the movies made in 2000 or later with a runtime greater than 180 minutes. - Use
SELECTto compute how many minutes past 180 each one runs. - Use
ORDER BYto put the movies with the most extra minutes first.
CREATE TABLE long_movies AS
SELECT "REPLACE THIS LINE WITH YOUR SOLUTION";
python3 -m pytest -k long_moviesSubmit Assignment
Submit this assignment by running Provenance: Prepare Submission Bundle from the VS Code command palette and uploading the resulting zip to Gradescope. The zip already contains the files you've edited. Lab 00 has detailed instructions.
Your responses to WWPD questions are not submitted, and they do not need to be. Lab credit is based on the code writing questions.