Skip to content

Instantly share code, notes, and snippets.

@maxjacobson
Created June 6, 2013 18:53
Show Gist options
  • Save maxjacobson/5723940 to your computer and use it in GitHub Desktop.
Save maxjacobson/5723940 to your computer and use it in GitHub Desktop.
Modeling Amazon in SQL
-- run while testing:
-- sqlite3 -echo amazon.db < amazon.sql
DROP TABLE dvd;
DROP TABLE review;
-- 1) Create a database with tables and columns that
-- will hold information about DVD's
-- for sale on Amazon
CREATE TABLE dvd (
id int,
title text,
price int -- round dollars for now
);
-- 2) Add 10 more DVD's to the database.
INSERT INTO dvd VALUES
(1, "Crouching Tiger Hidden Dragon", 10),
(2, "Hulk", 3),
(3, "Brokeback Mountain", 11),
(4, "Lust, Caution", 20), -- imported
(5, "Life of Pi", 22), -- 3D dvd
(6, "Taking Woodstock", 5),
(7, "Sense and Sensibility", 9),
(8, "The Wedding Banquet", 10),
(9, "Eat Drink Man Woman", 9),
(10, "The Ice Storm", 10);
-- 4) What is the total cost of any two DVD's (your choice which two)
SELECT SUM(price) FROM dvd WHERE id=1 OR id=2;
-- 5) Return just the titles for all the DVD's in the database.
SELECT title FROM dvd;
-- 6) If you don't already have a table to hold product
-- reviews table create one, and add the appropriate
-- columns to the table to model the product reviews
-- for a DVD. Make sure a product review can be related
-- to its corresponding DVD in the database.
CREATE TABLE review (
id int,
review text,
dvd_id int
);
-- 7) Create 5 product reviews.
INSERT INTO review VALUES
(1, "I hear it's underrated", 2),
(2, "My favorite movie of 2012", 5),
(3, "Classic movie", 3),
(4, "Kind of lame", 6),
(5, "It's so cool when they fly through the air!", 1);
-- 8) Select a DVD, and then select all its product reviews.
SELECT review FROM review WHERE dvd_id=1;
-- 9) Select a product review, and then select all the product
-- reviews that correspond to the first product reviews DVD.
-- not sure what to do different from the previous one?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment