Skip to content

Instantly share code, notes, and snippets.

@desmondrawls
Created June 7, 2013 00:02
Show Gist options
  • Save desmondrawls/5726080 to your computer and use it in GitHub Desktop.
Save desmondrawls/5726080 to your computer and use it in GitHub Desktop.
sql joins homework some marvin gaye, luther van drosse, a little anitaaaa will surely get this party off riiight
CREATE TABLE projects (
id INTEGER PRIMARY KEY,
title TEXT,
category TEXT,
goal INTEGER,
start INTEGER,
end INTEGER
);
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT,
age TEXT
);
CREATE TABLE pledges (
id INTEGER PRIMARY KEY,
amount INTEGER,
users_id INTEGER,
projects_id INTEGER
);
INSERT INTO projects (id, title, category, goal, start, end)
VALUES
(1, 'manhattan', 'charity', 100000000, 1941, 1945),
(2, 'bang on a can', 'charity', 100000, 2000, 2020),
(3, 'literacy', 'charity', 10000000, 2001, 2010),
(4, 'better beats', 'music', 10000000, 2002, 2009),
(5, 'comics for phonics', 'books', 1000000, 2003, 2008),
(6, 'phonograph', 'music', 100, 2004, 2010),
(7, 'impossible', 'music', 10000000, 2005, 2011),
(8, 'even more impossible', 'music', 10, 2006, 2014),
(9, 'most impossible', 'music', 1, 2007, 2012),
(10, 'burn a book', 'books', 100, 2008, 2013);
INSERT INTO users (id, name, age)
VALUES
(1, 'jen', 21),
(2, 'jack', 22),
(3, 'adam', 23),
(4, 'carlos', 24),
(5, 'anisha', 25),
(6, 'chris', 26),
(7, 'kristen', 27),
(8, 'jen', 28),
(9, 'jordan', 28),
(10, 'matthew', 28),
(11, 'thomas', 27),
(12, 'ruthie', 29),
(13, 'kelley', 26),
(14, 'avi', 29),
(15, 'adam', 30),
(16, 'bob', 28),
(17, 'ashley', 26),
(18, 'george', 26),
(19, 'john', 27),
(20, 'ning', 27);
INSERT INTO pledges(id, amount, users_id, projects_id)
VALUES
(1, 100000, 1, 1),
(2, 100, 3, 2),
(3, 1000, 10, 3),
(4, 100, 20, 4),
(5, 1000, 13, 5),
(6, 100, 4, 6),
(7, 10, 5, 7),
(8, 1000, 6, 8),
(9, 100, 7, 5),
(10, 1000, 8, 4),
(11, 1000, 9, 9),
(12, 10, 6, 10),
(13, 1000, 3, 4),
(14, 10000, 17, 2),
(15, 100000, 17, 1),
(16, 100, 18, 4),
(17, 10000, 19, 5),
(18, 10000, 11, 6),
(19, 100, 12, 7),
(20, 10000, 14, 9),
(21, 10000, 7, 3),
(22, 100000, 2, 5),
(23, 100, 9, 8),
(24, 10000, 13, 9),
(25, 100, 14, 7),
(26, 10000, 15, 3),
(27, 10000, 17, 5),
(28, 100000, 3, 3),
(29, 1, 8, 2),
(30, 10, 1, 5);
--------------QUESTIONS----------------
---Select the titles of all projects and their pledge amounts.
SELECT projects.title, pledges.amount
FROM projects
JOIN pledges
ON pledges.projects_id = projects.id;
-----Select the user name, age, and pledge amount for all pledges.
SELECT users.name, users.age, pledges.amount
FROM pledges
JOIN users
ON pledges.users_id = users.id;
-----Select the titles of all projects that have met their funding goal.
--COULDN'T GET AWAY WITH A SINGLE STATEMENT------
SELECT pledges.id, projects.id AS help
FROM pledges
JOIN projects
ON pledges.projects_id = projects.id
WHERE projects.goal < (SELECT SUM(amount)
FROM pledges WHERE projects_id = help);
-----Select user names and amounts of all pledges. Order them by the amount.
SELECT users.name, pledges.amount
FROM pledges
JOIN users
ON pledges.users_id = users.id
ORDER BY pledges.amount DESC;
----Select the category names, and pledge amounts of all pledges in the music category.
SELECT projects.category, pledges.amount
FROM pledges
JOIN projects
ON pledges.projects_id = projects.id
WHERE projects.category = 'music';
-----Select the category names and the sum total of the pledge amounts of all the pledges in the book category.
SELECT category, SUM(amount)
FROM pledges
JOIN projects
ON pledges.projects_id = projects.id
WHERE projects.category = "books";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment