Homework 9: SQL
- Due: Sunday 11/29 @ 11:59pm
- Points: 1
- Download: hw09.zip
There are no Preceptor interviews for this homework.
Setup
Download hw09.zip from the link at the top of this page, unzip it, and open
the hw09 folder in VS Code. The folder contains:
hw09.sql: The file you'll edit. Each question asks you to complete oneCREATE TABLEstatement.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 hw09.sql and then type in more statements yourself, run:
python3 sqlite_shell.py --init hw09.sql
Type .quit or press Ctrl-C to exit. After you change hw09.sql, exit and run
the command again to see the result. Lab 10 has
more details.
Test Your Code
Run one question's tests by naming it after -k, or run them all with
--score:
python3 -m pytest -k by_parent_height # just this question
python3 -m pytest --score # every question, with your score
Getting Started Videos:
If you feel stuck, 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
Dog Data
In each question below, you will define a new table based on the following
tables about dogs, which appear at the top of hw09.sql.
CREATE TABLE parents (parent TEXT, child TEXT);
INSERT INTO parents VALUES
("ace", "bella"),
("ace", "charlie"),
("daisy", "hank"),
("finn", "ace"),
("finn", "daisy"),
("finn", "ginger"),
("ellie", "finn");
CREATE TABLE dogs (name TEXT, fur TEXT, height INTEGER);
INSERT INTO dogs VALUES
("ace", "long", 26),
("bella", "short", 52),
("charlie", "long", 47),
("daisy", "long", 46),
("ellie", "short", 35),
("finn", "curly", 32),
("ginger", "short", 28),
("hank", "curly", 31);
CREATE TABLE sizes (size TEXT, min INTEGER, max INTEGER);
INSERT INTO sizes VALUES
("toy", 24, 28),
("mini", 28, 35),
("medium", 35, 45),
("standard", 45, 60);
The parents table contains one row for each parent-child relationship; for
example, the first row describes that the dog "ace" is the parent of the dog
"bella". The dogs table contains the name, fur type, and height for each
dog. The sizes table has one row for each classification of dog. A dog
matches a particular classification if its height is greater than min and
less than or equal to max.
Your queries should still perform correctly even if the values in these tables
change. For example, if you are asked to list all dogs with a name that starts
with h, you should write:
SELECT name FROM dogs WHERE name LIKE "h%";
The % sign matches any number of characters, and patterns in sqlite are
case insensitive, so the pattern "h%" matches all strings that begin with h
or H. This query would still be correct if a row was added with the name
harry. Contrastingly, writing a query like SELECT "hank"; would not, and
only works for the example input.
Q1: By Parent Height
Create a table by_parent_height that has a column of the names of all dogs
that have a parent, ordered by the height of the parent dog from tallest
parent to shortest parent.
-- Q1: All dogs with parents ordered by decreasing height of their parent
CREATE TABLE by_parent_height AS
SELECT "REPLACE THIS LINE WITH YOUR SOLUTION";
For example, finn has a parent ellie with height 35, and so should appear
before ginger who has a parent finn with height 32. The names of dogs with
parents of the same height should appear together in any order. For example,
bella and charlie should both appear at the end, but either one can come
before the other.
For our example tables, the by_parent_height table should look like this:
| child |
|---|
| hank |
| finn |
| ace |
| daisy |
| ginger |
| bella |
| charlie |
python3 -m pytest -k by_parent_heightQ2: Size of Dogs
The Fédération Cynologique Internationale classifies a standard poodle as over
45 cm and up to 60 cm. The sizes table describes this and other such
classifications, where a dog must be over the min and less than or equal to
the max in height to qualify as size.
Create a size_of_dogs table with two columns, one for each dog's name and
another for its size.
-- Q2: The size of each dog
CREATE TABLE size_of_dogs AS
SELECT "REPLACE THIS LINE WITH YOUR SOLUTION";
The size_of_dogs table should look like this:
| name | size |
|---|---|
| ace | toy |
| bella | standard |
| charlie | standard |
| daisy | standard |
| ellie | mini |
| finn | mini |
| ginger | toy |
| hank | mini |
python3 -m pytest -k size_of_dogsQ3: Sentences
Siblings are pairs of dogs that have the same parent. Create a table that contains a row for each pair of siblings that have the same size classification, with a single column that contains a sentence describing the siblings by their size.
-- Q3: [Optional] Filling out this helper table is recommended
CREATE TABLE siblings AS
SELECT "REPLACE THIS LINE WITH YOUR SOLUTION";
-- Q3: Sentences about siblings that are the same size
CREATE TABLE sentences AS
SELECT "REPLACE THIS LINE WITH YOUR SOLUTION";
Each sibling pair should appear only once in the output, and siblings should be
listed in alphabetical order (e.g. "bella and charlie..." instead of
"charlie and bella..."), as follows:
sqlite> SELECT * FROM sentences;
The two siblings, bella and charlie, have the same size: standard
The two siblings, ace and ginger, have the same size: toy
Hints
- First, create a helper table containing the names of each pair of siblings. This will make comparing the sizes of siblings when constructing the main table easier. Make sure to not pair a child with themselves and do not include duplicate pairs.
- If you join a table with itself, use
ASwithin theFROMclause to give each table an alias. - In order to concatenate two strings into one, use the
||operator, e.g.SELECT "hello" || "world";will returnhelloworld.
python3 -m pytest -k sentencesQ4: Low Variance
We want to create a table that contains the height range (defined as the
difference between maximum and minimum height) of all dogs that share a fur
type. However, we'll only consider fur types where each dog with that fur
type is within 30% of the average height of all dogs with that fur type; we
call this the low variance criterion.
For example, if the average height for short-haired dogs is 10, then in order
to be included in our output, all dogs with short hair must have a height of
at most 13 and at least 7 (inclusive).
Hints
MIN,MAX, andAVGwill be useful here.- You may want to first find the average height and make sure that:
- There are no heights smaller than 0.7 (i.e. 70%) of the average.
- There are no heights greater than 1.3 (i.e. 130%) of the average.
-- Q4: Height range for each fur type where all of the heights differ by no more than 30% from the average height
CREATE TABLE low_variance AS
SELECT "REPLACE THIS LINE WITH YOUR SOLUTION";
Your output should have two columns, in this order: the fur type and the
height_range for the fur types that meet this criteria. For the example
tables, it should look like this:
| fur | height_range |
|---|---|
| curly | 1 |
Explanation
The average height of long-haired dogs is 39.7, so the low variance criterion
requires the height of each long-haired dog to be between 27.8 and 51.6.
However, ace is a long-haired dog with height 26, which is outside this
range. For short-haired dogs, bella falls outside the valid range (check!).
Thus, neither short nor long haired dogs are included in the output. There are
two curly haired dogs: finn with height 32 and hank with height 31. This
gives a height range of 1.
python3 -m pytest -k low_varianceSubmit
Run Provenance: Prepare Submission Bundle from the VS Code command palette to create your submission zip, and upload that zip to Gradescope. For a refresher on how to do this, refer to Lab 00.
Exam Practice
The following are some SQL exam problems from previous semesters that you may find useful as additional exam practice.
- Fall 2019 Final, Question 10: Big Game
- Summer 2019 Final, Question 8: The Big SQL
- Fall 2018 Final, Question 7: SQL of Course
Practice for Quiz 8
Quiz 8 will occur on Monday, November 30th and Tuesday, December 1st and will have a variant of Q2 (Size of Dogs). The practice quiz is released now on PrairieLearn so that you can prepare.