From 14ca9bd5518bd9e24c56cbe69b8014e4c65e8c2d Mon Sep 17 00:00:00 2001 From: Aaron Villanueva Date: Wed, 8 Mar 2017 12:05:30 -0800 Subject: [PATCH 01/12] friend table --- sql/02-create-table-friend.sql | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/sql/02-create-table-friend.sql b/sql/02-create-table-friend.sql index e69de29..33ee9c0 100644 --- a/sql/02-create-table-friend.sql +++ b/sql/02-create-table-friend.sql @@ -0,0 +1,31 @@ +psql sql-exercises; --allows me to switch in terminal + create table student.friend ( + id1 INT NOT NULL, + id2 INT NOT NULL + ); + \d student.friend; --allows me to see the table in terminal + + INSERT INTO student.friend values (1510, 1381); + INSERT INTO student.friend values (1510, 1689); + INSERT INTO student.friend values (1689, 1709); + INSERT INTO student.friend values (1381, 1247); + INSERT INTO student.friend values (1709, 1247); + INSERT INTO student.friend values (1689, 1782); + INSERT INTO student.friend values (1782, 1468); + INSERT INTO student.friend values (1782, 1316); + INSERT INTO student.friend values (1782, 1304); + INSERT INTO student.friend values (1468, 1101); + INSERT INTO student.friend values (1468, 1641); + INSERT INTO student.friend values (1101, 1641); + INSERT INTO student.friend values (1247, 1911); + INSERT INTO student.friend values (1247, 1501); + INSERT INTO student.friend values (1911, 1501); + INSERT INTO student.friend values (1501, 1934); + INSERT INTO student.friend values (1316, 1934); + INSERT INTO student.friend values (1934, 1304); + INSERT INTO student.friend values (1304, 1661); + INSERT INTO student.friend values (1661, 1025); + + select * from student.friend; + + \ No newline at end of file From 0961c4f6fa19593f3938faf521733113b2d90607 Mon Sep 17 00:00:00 2001 From: Aaron Villanueva Date: Wed, 8 Mar 2017 12:33:32 -0800 Subject: [PATCH 02/12] student like --- sql/03-create-table-like.sql | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/sql/03-create-table-like.sql b/sql/03-create-table-like.sql index e69de29..d1f0b06 100644 --- a/sql/03-create-table-like.sql +++ b/sql/03-create-table-like.sql @@ -0,0 +1,19 @@ +psql sql-exercises; --allows me to switch in terminal + create table student.student_like ( + liker_id INT NOT NULL, + likee_id INT NOT NULL + ); + \d student.student_like; --allows me to see the table in terminal + + INSERT INTO student.student_like values (1689, 1709); + INSERT INTO student.student_like values (1709, 1689); + INSERT INTO student.student_like values (1782, 1709); + INSERT INTO student.student_like values (1911, 1247); + INSERT INTO student.student_like values (1247, 1468); + INSERT INTO student.student_like values (1641, 1468); + INSERT INTO student.student_like values (1316, 1304); + INSERT INTO student.student_like values (1501, 1934); + INSERT INTO student.student_like values (1934, 1501); + INSERT INTO student.student_like values (1025, 1101); + + select * from student.student_like; \ No newline at end of file From 14eeab2df11b407bda4f9697da398a914648ea59 Mon Sep 17 00:00:00 2001 From: Aaron Villanueva Date: Wed, 8 Mar 2017 13:11:09 -0800 Subject: [PATCH 03/12] loaded --- sql/04-load-table-student.sql | 16 ++++++++++++++++ sql/05-load-table-friend.sql | 20 ++++++++++++++++++++ sql/06-load-table-like.sql | 10 ++++++++++ 3 files changed, 46 insertions(+) diff --git a/sql/04-load-table-student.sql b/sql/04-load-table-student.sql index e69de29..295cf85 100644 --- a/sql/04-load-table-student.sql +++ b/sql/04-load-table-student.sql @@ -0,0 +1,16 @@ +INSERT INTO student.student values (1510, 'Jordan', 9); +INSERT INTO student.student values (1689, 'Gabriel', 9); +INSERT INTO student.student values (1381, 'Tiffany', 9); +INSERT INTO student.student values (1709, 'Cassandra', 9); +INSERT INTO student.student values (1101, 'Haley', 10); +INSERT INTO student.student values (1782, 'Andrew', 10); +INSERT INTO student.student values (1468, 'Kris', 10); +INSERT INTO student.student values (1641, 'Brittany', 10); +INSERT INTO student.student values (1274, 'Alexis', 11); +INSERT INTO student.student values (1316, 'Austin', 11); +INSERT INTO student.student values (1911, 'Gabriel', 11); +INSERT INTO student.student values (1501, 'Jessica', 11); +INSERT INTO student.student values (1304, 'Jordan', 12); +INSERT INTO student.student values (1025, 'John', 12); +INSERT INTO student.student values (1934, 'Kyle', 12); +INSERT INTO student.student values (1661, 'Logan', 12); diff --git a/sql/05-load-table-friend.sql b/sql/05-load-table-friend.sql index e69de29..007f3d2 100644 --- a/sql/05-load-table-friend.sql +++ b/sql/05-load-table-friend.sql @@ -0,0 +1,20 @@ +INSERT INTO student.friend values (1510, 1381); + INSERT INTO student.friend values (1510, 1689); + INSERT INTO student.friend values (1689, 1709); + INSERT INTO student.friend values (1381, 1247); + INSERT INTO student.friend values (1709, 1247); + INSERT INTO student.friend values (1689, 1782); + INSERT INTO student.friend values (1782, 1468); + INSERT INTO student.friend values (1782, 1316); + INSERT INTO student.friend values (1782, 1304); + INSERT INTO student.friend values (1468, 1101); + INSERT INTO student.friend values (1468, 1641); + INSERT INTO student.friend values (1101, 1641); + INSERT INTO student.friend values (1247, 1911); + INSERT INTO student.friend values (1247, 1501); + INSERT INTO student.friend values (1911, 1501); + INSERT INTO student.friend values (1501, 1934); + INSERT INTO student.friend values (1316, 1934); + INSERT INTO student.friend values (1934, 1304); + INSERT INTO student.friend values (1304, 1661); + INSERT INTO student.friend values (1661, 1025); \ No newline at end of file diff --git a/sql/06-load-table-like.sql b/sql/06-load-table-like.sql index e69de29..60f0d05 100644 --- a/sql/06-load-table-like.sql +++ b/sql/06-load-table-like.sql @@ -0,0 +1,10 @@ + INSERT INTO student.student_like values (1689, 1709); + INSERT INTO student.student_like values (1709, 1689); + INSERT INTO student.student_like values (1782, 1709); + INSERT INTO student.student_like values (1911, 1247); + INSERT INTO student.student_like values (1247, 1468); + INSERT INTO student.student_like values (1641, 1468); + INSERT INTO student.student_like values (1316, 1304); + INSERT INTO student.student_like values (1501, 1934); + INSERT INTO student.student_like values (1934, 1501); + INSERT INTO student.student_like values (1025, 1101); \ No newline at end of file From db8605d98fc8b10305a58cf94e1c68e66c27114f Mon Sep 17 00:00:00 2001 From: Aaron Villanueva Date: Wed, 8 Mar 2017 17:56:23 -0800 Subject: [PATCH 04/12] friends of a gabriel --- sql/07-query-friends-gabriel.sql | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sql/07-query-friends-gabriel.sql b/sql/07-query-friends-gabriel.sql index e69de29..88e9aed 100644 --- a/sql/07-query-friends-gabriel.sql +++ b/sql/07-query-friends-gabriel.sql @@ -0,0 +1,4 @@ +select name from student.student +JOIN student.friend +ON student.student.id = student.friend.id1 +where id2 = 1911 or id2 = 1689; From 6dc3a5aef10000ff4de0954419a086ac435d0ac5 Mon Sep 17 00:00:00 2001 From: Aaron Villanueva Date: Thu, 9 Mar 2017 09:09:47 -0800 Subject: [PATCH 05/12] 13 - popular students --- sql/08-query-likes-grade-two-or-more.sql | 57 ++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/sql/08-query-likes-grade-two-or-more.sql b/sql/08-query-likes-grade-two-or-more.sql index e69de29..2338455 100644 --- a/sql/08-query-likes-grade-two-or-more.sql +++ b/sql/08-query-likes-grade-two-or-more.sql @@ -0,0 +1,57 @@ +select * from + +(select name +from student.student +where id = (id )) + +-- I need to understand how to select using subqueries +-- how to join better + + + + + + + + +select name from student.student_like +join student.student +on student.student_like.liker_id = student.student.id +and student.studnet_like.likee_id = student.student.id; + +where + +liker_id, likee_id +1689, 1709 +1709, 1689 +1782, 1709 +1911, 1247 +1247, 1468 +1641, 1468 +1316, 1304 +1501, 1934 +1934, 1501 +1025, 1101 + +id,name,grade +1510,"Jordan",9 +1689,"Gabriel",9 +1381,"Tiffany",9 +1709,"Cassandra",9 +1101,"Haley",10 +1782,"Andrew",10 +1468,"Kris",10 +1641,"Brittany",10 +1247,"Alexis",11 +1316,"Austin",11 +1911,"Gabriel",11 +1501,"Jessica",11 +1304,"Jordan",12 +1025,"John",12 +1934,"Kyle",12 +1661,"Logan",12 + +select name from student.student +JOIN student.student +ON student.student.id = student.friend.id1 +where id2 = 1911 or id2 = 1689; From 9e450bd70a1b60a91ca808e9cacc90765baf354a Mon Sep 17 00:00:00 2001 From: Aaron Villanueva Date: Thu, 9 Mar 2017 09:16:12 -0800 Subject: [PATCH 06/12] popular students --- sql/13-popular-students.sql | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sql/13-popular-students.sql b/sql/13-popular-students.sql index e69de29..2a65720 100644 --- a/sql/13-popular-students.sql +++ b/sql/13-popular-students.sql @@ -0,0 +1,6 @@ +select name +from student.student_like +join student.student +on student.student.id = student.student_like.likee_id +group by name +having count(likee_id) >= 2; \ No newline at end of file From ca0c6fcc1f4b6ce67413f78d0703df4ed373c85b Mon Sep 17 00:00:00 2001 From: Aaron Villanueva Date: Fri, 10 Mar 2017 15:01:29 -0800 Subject: [PATCH 07/12] notLikedStudents --- sql/01-create-table-student.sql | 38 ++++++++++------------ sql/02-create-table-friend.sql | 32 ++++--------------- sql/03-create-table-like.sql | 20 ++++-------- sql/04-load-table-student.sql | 32 +++++++++---------- sql/05-load-table-friend.sql | 40 ++++++++++++------------ sql/06-load-table-like.sql | 20 ++++++------ sql/07-query-friends-gabriel.sql | 9 +++--- sql/08-query-likes-grade-two-or-more.sql | 7 ++++- sql/10-not-liked.sql | 5 +++ sql/13-popular-students.sql | 12 +++---- 10 files changed, 96 insertions(+), 119 deletions(-) diff --git a/sql/01-create-table-student.sql b/sql/01-create-table-student.sql index dd66051..e45df77 100644 --- a/sql/01-create-table-student.sql +++ b/sql/01-create-table-student.sql @@ -1,23 +1,17 @@ -createdb sql-exercises; -psql sql-exercises; --allows me to swithc in terminal -create schema student; - create table student.student ( - id INT NOT NULL, first_name TEXT, grade INT +createdb sql-exercises; -- only need to create data table once + +psql sql-exercises; -- allows me to switch in terminal + +CREATE SCHEMA student; -- only need to create schema once + +CREATE TABLE student ( + + id INT NOT NULL, + + first_name TEXT, + + grade INT + ); - \d student.student; --allows me to see the table in terminal -INSERT INTO student.student values (1510, 'Jordan', 9); -INSERT INTO student.student values (1689, 'Gabriel', 9); -INSERT INTO student.student values (1381, 'Tiffany', 9); -INSERT INTO student.student values (1709, 'Cassandra', 9); -INSERT INTO student.student values (1101, 'Haley', 10); -INSERT INTO student.student values (1782, 'Andrew', 10); -INSERT INTO student.student values (1468, 'Kris', 10); -INSERT INTO student.student values (1641, 'Brittany', 10); -INSERT INTO student.student values (1274, 'Alexis', 11); -INSERT INTO student.student values (1316, 'Austin', 11); -INSERT INTO student.student values (1911, 'Gabriel', 11); -INSERT INTO student.student values (1501, 'Jessica', 11); -INSERT INTO student.student values (1304, 'Jordan', 12); -INSERT INTO student.student values (1025, 'John', 12); -INSERT INTO student.student values (1934, 'Kyle', 12); -INSERT INTO student.student values (1661, 'Logan', 12); + +\d student.student; -- allows me to see the table in terminal \ No newline at end of file diff --git a/sql/02-create-table-friend.sql b/sql/02-create-table-friend.sql index 33ee9c0..ccc2384 100644 --- a/sql/02-create-table-friend.sql +++ b/sql/02-create-table-friend.sql @@ -1,31 +1,11 @@ psql sql-exercises; --allows me to switch in terminal - create table student.friend ( + +CREATE TABLE friend ( + id1 INT NOT NULL, + id2 INT NOT NULL - ); - \d student.friend; --allows me to see the table in terminal - - INSERT INTO student.friend values (1510, 1381); - INSERT INTO student.friend values (1510, 1689); - INSERT INTO student.friend values (1689, 1709); - INSERT INTO student.friend values (1381, 1247); - INSERT INTO student.friend values (1709, 1247); - INSERT INTO student.friend values (1689, 1782); - INSERT INTO student.friend values (1782, 1468); - INSERT INTO student.friend values (1782, 1316); - INSERT INTO student.friend values (1782, 1304); - INSERT INTO student.friend values (1468, 1101); - INSERT INTO student.friend values (1468, 1641); - INSERT INTO student.friend values (1101, 1641); - INSERT INTO student.friend values (1247, 1911); - INSERT INTO student.friend values (1247, 1501); - INSERT INTO student.friend values (1911, 1501); - INSERT INTO student.friend values (1501, 1934); - INSERT INTO student.friend values (1316, 1934); - INSERT INTO student.friend values (1934, 1304); - INSERT INTO student.friend values (1304, 1661); - INSERT INTO student.friend values (1661, 1025); - select * from student.friend; + ); - \ No newline at end of file +\d student.friend; --allows me to see the table in terminal \ No newline at end of file diff --git a/sql/03-create-table-like.sql b/sql/03-create-table-like.sql index d1f0b06..425c0b1 100644 --- a/sql/03-create-table-like.sql +++ b/sql/03-create-table-like.sql @@ -1,19 +1,11 @@ psql sql-exercises; --allows me to switch in terminal - create table student.student_like ( + +CREATE TABLE student_like ( + liker_id INT NOT NULL, + likee_id INT NOT NULL + ); - \d student.student_like; --allows me to see the table in terminal - - INSERT INTO student.student_like values (1689, 1709); - INSERT INTO student.student_like values (1709, 1689); - INSERT INTO student.student_like values (1782, 1709); - INSERT INTO student.student_like values (1911, 1247); - INSERT INTO student.student_like values (1247, 1468); - INSERT INTO student.student_like values (1641, 1468); - INSERT INTO student.student_like values (1316, 1304); - INSERT INTO student.student_like values (1501, 1934); - INSERT INTO student.student_like values (1934, 1501); - INSERT INTO student.student_like values (1025, 1101); - select * from student.student_like; \ No newline at end of file +\d student.student_like; --allows me to see the table in terminal \ No newline at end of file diff --git a/sql/04-load-table-student.sql b/sql/04-load-table-student.sql index 295cf85..2307a22 100644 --- a/sql/04-load-table-student.sql +++ b/sql/04-load-table-student.sql @@ -1,16 +1,16 @@ -INSERT INTO student.student values (1510, 'Jordan', 9); -INSERT INTO student.student values (1689, 'Gabriel', 9); -INSERT INTO student.student values (1381, 'Tiffany', 9); -INSERT INTO student.student values (1709, 'Cassandra', 9); -INSERT INTO student.student values (1101, 'Haley', 10); -INSERT INTO student.student values (1782, 'Andrew', 10); -INSERT INTO student.student values (1468, 'Kris', 10); -INSERT INTO student.student values (1641, 'Brittany', 10); -INSERT INTO student.student values (1274, 'Alexis', 11); -INSERT INTO student.student values (1316, 'Austin', 11); -INSERT INTO student.student values (1911, 'Gabriel', 11); -INSERT INTO student.student values (1501, 'Jessica', 11); -INSERT INTO student.student values (1304, 'Jordan', 12); -INSERT INTO student.student values (1025, 'John', 12); -INSERT INTO student.student values (1934, 'Kyle', 12); -INSERT INTO student.student values (1661, 'Logan', 12); +INSERT INTO student VALUES (1510, 'Jordan', 9); +INSERT INTO student VALUES (1689, 'Gabriel', 9); +INSERT INTO student VALUES (1381, 'Tiffany', 9); +INSERT INTO student VALUES (1709, 'Cassandra', 9); +INSERT INTO student VALUES (1101, 'Haley', 10); +INSERT INTO student VALUES (1782, 'Andrew', 10); +INSERT INTO student VALUES (1468, 'Kris', 10); +INSERT INTO student VALUES (1641, 'Brittany', 10); +INSERT INTO student VALUES (1274, 'Alexis', 11); +INSERT INTO student VALUES (1316, 'Austin', 11); +INSERT INTO student VALUES (1911, 'Gabriel', 11); +INSERT INTO student VALUES (1501, 'Jessica', 11); +INSERT INTO student VALUES (1304, 'Jordan', 12); +INSERT INTO student VALUES (1025, 'John', 12); +INSERT INTO student VALUES (1934, 'Kyle', 12); +INSERT INTO student VALUES (1661, 'Logan', 12); \ No newline at end of file diff --git a/sql/05-load-table-friend.sql b/sql/05-load-table-friend.sql index 007f3d2..5a76be2 100644 --- a/sql/05-load-table-friend.sql +++ b/sql/05-load-table-friend.sql @@ -1,20 +1,20 @@ -INSERT INTO student.friend values (1510, 1381); - INSERT INTO student.friend values (1510, 1689); - INSERT INTO student.friend values (1689, 1709); - INSERT INTO student.friend values (1381, 1247); - INSERT INTO student.friend values (1709, 1247); - INSERT INTO student.friend values (1689, 1782); - INSERT INTO student.friend values (1782, 1468); - INSERT INTO student.friend values (1782, 1316); - INSERT INTO student.friend values (1782, 1304); - INSERT INTO student.friend values (1468, 1101); - INSERT INTO student.friend values (1468, 1641); - INSERT INTO student.friend values (1101, 1641); - INSERT INTO student.friend values (1247, 1911); - INSERT INTO student.friend values (1247, 1501); - INSERT INTO student.friend values (1911, 1501); - INSERT INTO student.friend values (1501, 1934); - INSERT INTO student.friend values (1316, 1934); - INSERT INTO student.friend values (1934, 1304); - INSERT INTO student.friend values (1304, 1661); - INSERT INTO student.friend values (1661, 1025); \ No newline at end of file +INSERT INTO student.friend VALUES (1510, 1381); +INSERT INTO student.friend VALUES (1510, 1689); +INSERT INTO student.friend VALUES (1689, 1709); +INSERT INTO student.friend VALUES (1381, 1247); +INSERT INTO student.friend VALUES (1709, 1247); +INSERT INTO student.friend VALUES (1689, 1782); +INSERT INTO student.friend VALUES (1782, 1468); +INSERT INTO student.friend VALUES (1782, 1316); +INSERT INTO student.friend VALUES (1782, 1304); +INSERT INTO student.friend VALUES (1468, 1101); +INSERT INTO student.friend VALUES (1468, 1641); +INSERT INTO student.friend VALUES (1101, 1641); +INSERT INTO student.friend VALUES (1247, 1911); +INSERT INTO student.friend VALUES (1247, 1501); +INSERT INTO student.friend VALUES (1911, 1501); +INSERT INTO student.friend VALUES (1501, 1934); +INSERT INTO student.friend VALUES (1316, 1934); +INSERT INTO student.friend VALUES (1934, 1304); +INSERT INTO student.friend VALUES (1304, 1661); +INSERT INTO student.friend VALUES (1661, 1025); \ No newline at end of file diff --git a/sql/06-load-table-like.sql b/sql/06-load-table-like.sql index 60f0d05..3fa2bcd 100644 --- a/sql/06-load-table-like.sql +++ b/sql/06-load-table-like.sql @@ -1,10 +1,10 @@ - INSERT INTO student.student_like values (1689, 1709); - INSERT INTO student.student_like values (1709, 1689); - INSERT INTO student.student_like values (1782, 1709); - INSERT INTO student.student_like values (1911, 1247); - INSERT INTO student.student_like values (1247, 1468); - INSERT INTO student.student_like values (1641, 1468); - INSERT INTO student.student_like values (1316, 1304); - INSERT INTO student.student_like values (1501, 1934); - INSERT INTO student.student_like values (1934, 1501); - INSERT INTO student.student_like values (1025, 1101); \ No newline at end of file +INSERT INTO student_like VALUES (1689, 1709); +INSERT INTO student_like VALUES (1709, 1689); +INSERT INTO student_like VALUES (1782, 1709); +INSERT INTO student_like VALUES (1911, 1247); +INSERT INTO student_like VALUES (1247, 1468); +INSERT INTO student_like VALUES (1641, 1468); +INSERT INTO student_like VALUES (1316, 1304); +INSERT INTO student_like VALUES (1501, 1934); +INSERT INTO student_like VALUES (1934, 1501); +INSERT INTO student_like VALUES (1025, 1101); \ No newline at end of file diff --git a/sql/07-query-friends-gabriel.sql b/sql/07-query-friends-gabriel.sql index 88e9aed..e9be211 100644 --- a/sql/07-query-friends-gabriel.sql +++ b/sql/07-query-friends-gabriel.sql @@ -1,4 +1,5 @@ -select name from student.student -JOIN student.friend -ON student.student.id = student.friend.id1 -where id2 = 1911 or id2 = 1689; +SELECT name +FROM student.student +JOIN student.friend +ON student.student.id = student.friend.id1 +WHERE id2 = 1911 or id2 = 1689; diff --git a/sql/08-query-likes-grade-two-or-more.sql b/sql/08-query-likes-grade-two-or-more.sql index 2338455..45a0bff 100644 --- a/sql/08-query-likes-grade-two-or-more.sql +++ b/sql/08-query-likes-grade-two-or-more.sql @@ -1,4 +1,9 @@ -select * from +SELECT name, grade +FROM student.student +JOIN student.student_like +ON student.student.id = student.student_like.liker_id; + + (select name from student.student diff --git a/sql/10-not-liked.sql b/sql/10-not-liked.sql index e69de29..72d7b8e 100644 --- a/sql/10-not-liked.sql +++ b/sql/10-not-liked.sql @@ -0,0 +1,5 @@ +SELECT first_name, grade +FROM student +WHERE (id NOT IN (SELECT liker_id FROM student_like)) +AND (id NOT IN (SELECT likee_id FROM student_like)); + diff --git a/sql/13-popular-students.sql b/sql/13-popular-students.sql index 2a65720..8411465 100644 --- a/sql/13-popular-students.sql +++ b/sql/13-popular-students.sql @@ -1,6 +1,6 @@ -select name -from student.student_like -join student.student -on student.student.id = student.student_like.likee_id -group by name -having count(likee_id) >= 2; \ No newline at end of file +SELECT name +FROM student.student_like +JOIN student.student +ON student.student.id = student.student_like.likee_id +GROUP BY name +HAVING count(likee_id) >= 2; \ No newline at end of file From a1c069c7d462d8c625f85dcab129fa9dd641aa2f Mon Sep 17 00:00:00 2001 From: Aaron Villanueva Date: Fri, 10 Mar 2017 17:41:12 -0800 Subject: [PATCH 08/12] almost done --- sql/01-create-table-student.sql | 21 ++---- sql/02-create-table-friend.sql | 14 ++-- sql/03-create-table-like.sql | 14 ++-- sql/04-load-table-student.sql | 4 +- sql/07-query-friends-gabriel.sql | 42 +++++++++++- sql/08-query-likes-grade-two-or-more.sql | 83 ++++++------------------ sql/09-mutual-likes.sql | 11 ++++ sql/10-not-liked.sql | 1 + 8 files changed, 90 insertions(+), 100 deletions(-) diff --git a/sql/01-create-table-student.sql b/sql/01-create-table-student.sql index e45df77..2638eac 100644 --- a/sql/01-create-table-student.sql +++ b/sql/01-create-table-student.sql @@ -1,17 +1,6 @@ -createdb sql-exercises; -- only need to create data table once - -psql sql-exercises; -- allows me to switch in terminal - -CREATE SCHEMA student; -- only need to create schema once - +DROP TABLE IF EXISTS student; CREATE TABLE student ( - - id INT NOT NULL, - - first_name TEXT, - - grade INT - - ); - -\d student.student; -- allows me to see the table in terminal \ No newline at end of file + id SERIAL PRIMARY KEY, + name VARCHAR(255), + grade INTEGER +); \ No newline at end of file diff --git a/sql/02-create-table-friend.sql b/sql/02-create-table-friend.sql index ccc2384..7b3bf77 100644 --- a/sql/02-create-table-friend.sql +++ b/sql/02-create-table-friend.sql @@ -1,11 +1,5 @@ -psql sql-exercises; --allows me to switch in terminal - +DROP TABLE IF EXISTS friend; CREATE TABLE friend ( - - id1 INT NOT NULL, - - id2 INT NOT NULL - - ); - -\d student.friend; --allows me to see the table in terminal \ No newline at end of file + id1 INTEGER REFERENCES student(id), + id2 INTEGER REFERENCES student(id), +); \ No newline at end of file diff --git a/sql/03-create-table-like.sql b/sql/03-create-table-like.sql index 425c0b1..fbfaa04 100644 --- a/sql/03-create-table-like.sql +++ b/sql/03-create-table-like.sql @@ -1,11 +1,5 @@ -psql sql-exercises; --allows me to switch in terminal - +DROP TABLE IF EXISTS student_like; CREATE TABLE student_like ( - - liker_id INT NOT NULL, - - likee_id INT NOT NULL - - ); - -\d student.student_like; --allows me to see the table in terminal \ No newline at end of file + liker_id INTEGER REFERENCES student(id), + likee_id INTEGER REFERENCES student(id), +); \ No newline at end of file diff --git a/sql/04-load-table-student.sql b/sql/04-load-table-student.sql index 2307a22..4bf20a6 100644 --- a/sql/04-load-table-student.sql +++ b/sql/04-load-table-student.sql @@ -13,4 +13,6 @@ INSERT INTO student VALUES (1501, 'Jessica', 11); INSERT INTO student VALUES (1304, 'Jordan', 12); INSERT INTO student VALUES (1025, 'John', 12); INSERT INTO student VALUES (1934, 'Kyle', 12); -INSERT INTO student VALUES (1661, 'Logan', 12); \ No newline at end of file +INSERT INTO student VALUES (1661, 'Logan', 12); + +--laod it through the path \ No newline at end of file diff --git a/sql/07-query-friends-gabriel.sql b/sql/07-query-friends-gabriel.sql index e9be211..11e76e7 100644 --- a/sql/07-query-friends-gabriel.sql +++ b/sql/07-query-friends-gabriel.sql @@ -2,4 +2,44 @@ SELECT name FROM student.student JOIN student.friend ON student.student.id = student.friend.id1 -WHERE id2 = 1911 or id2 = 1689; +WHERE id2 = 1911 OR id2 = 1689; + + +--Alternative + +select +student2.name +from +student +join +friend +on +student.id=friend.id1 +join +student +as +student2 +on +student2.id = friend.id2 +where +student.name='Gabriel's + +--alternative 2 + +select +s2.name +from +friend +join +student +as +s1.id = friend.id1 +join +student +as +s2 +on +s2.id=friend.id2 +where +s1.name='Gabriel' + diff --git a/sql/08-query-likes-grade-two-or-more.sql b/sql/08-query-likes-grade-two-or-more.sql index 45a0bff..37fa168 100644 --- a/sql/08-query-likes-grade-two-or-more.sql +++ b/sql/08-query-likes-grade-two-or-more.sql @@ -1,62 +1,21 @@ -SELECT name, grade -FROM student.student -JOIN student.student_like -ON student.student.id = student.student_like.liker_id; - - - -(select name -from student.student -where id = (id )) - --- I need to understand how to select using subqueries --- how to join better - - - - - - - - -select name from student.student_like -join student.student -on student.student_like.liker_id = student.student.id -and student.studnet_like.likee_id = student.student.id; - -where - -liker_id, likee_id -1689, 1709 -1709, 1689 -1782, 1709 -1911, 1247 -1247, 1468 -1641, 1468 -1316, 1304 -1501, 1934 -1934, 1501 -1025, 1101 - -id,name,grade -1510,"Jordan",9 -1689,"Gabriel",9 -1381,"Tiffany",9 -1709,"Cassandra",9 -1101,"Haley",10 -1782,"Andrew",10 -1468,"Kris",10 -1641,"Brittany",10 -1247,"Alexis",11 -1316,"Austin",11 -1911,"Gabriel",11 -1501,"Jessica",11 -1304,"Jordan",12 -1025,"John",12 -1934,"Kyle",12 -1661,"Logan",12 - -select name from student.student -JOIN student.student -ON student.student.id = student.friend.id1 -where id2 = 1911 or id2 = 1689; +SELECT + student_who_likes.name, + student_who_likes.grade, + student_who_is_liked.named + student_who_is_liked.grade +FROM + student +AS + student_who_likes +JOIN + student_like +ON + student_who_likes.id = student_like.liker_id +JOIN + student +AS + friends +ON + student_who_is_liked.id = student_like.likee_id +WHERE + student_who_likes.grade - student_who_is_liked.grade >= 1 \ No newline at end of file diff --git a/sql/09-mutual-likes.sql b/sql/09-mutual-likes.sql index e69de29..b9d6d47 100644 --- a/sql/09-mutual-likes.sql +++ b/sql/09-mutual-likes.sql @@ -0,0 +1,11 @@ +SELECT first_name, grade +FROM student +JOIN student_like +ON student.id = student_like.liker_id +WHERE (id IN (SELECT liker_id FROM student_like)) + +--Attempted, but not really + +--09: For every pair of students who both like each other, return the name +--and grade of both students. Include each pair only once, with the two names in alphabetical order. +--Write the SQL in the file sql/09-mutual-likes.sql \ No newline at end of file diff --git a/sql/10-not-liked.sql b/sql/10-not-liked.sql index 72d7b8e..728d121 100644 --- a/sql/10-not-liked.sql +++ b/sql/10-not-liked.sql @@ -3,3 +3,4 @@ FROM student WHERE (id NOT IN (SELECT liker_id FROM student_like)) AND (id NOT IN (SELECT likee_id FROM student_like)); +--attempted not sure if it's right From 503169eafc067d958473c4e67d9228a58f3d7cab Mon Sep 17 00:00:00 2001 From: Aaron Villanueva Date: Sat, 11 Mar 2017 09:54:05 -0800 Subject: [PATCH 09/12] almost done --- sql/07-query-friends-gabriel.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/07-query-friends-gabriel.sql b/sql/07-query-friends-gabriel.sql index 11e76e7..1c63b46 100644 --- a/sql/07-query-friends-gabriel.sql +++ b/sql/07-query-friends-gabriel.sql @@ -22,7 +22,7 @@ student2 on student2.id = friend.id2 where -student.name='Gabriel's +student.name='Gabriel' --alternative 2 From 6350922e4ea16bb8963983869ea3064fcea1b882 Mon Sep 17 00:00:00 2001 From: Aaron Villanueva Date: Sat, 11 Mar 2017 12:51:58 -0800 Subject: [PATCH 10/12] brushing up --- sql/05-load-table-friend.sql | 40 +++++------ sql/07-query-friends-gabriel.sql | 56 ++++----------- sql/08-query-likes-grade-two-or-more.sql | 4 +- sql/09-mutual-likes.sql | 88 +++++++++++++++++++++--- sql/10-not-liked.sql | 22 ++++-- sql/11-liked-but-does-not-like.sql | 3 + sql/12-find-friends-in-common.sql | 3 + sql/13-popular-students.sql | 18 +++-- 8 files changed, 147 insertions(+), 87 deletions(-) diff --git a/sql/05-load-table-friend.sql b/sql/05-load-table-friend.sql index 5a76be2..0c65314 100644 --- a/sql/05-load-table-friend.sql +++ b/sql/05-load-table-friend.sql @@ -1,20 +1,20 @@ -INSERT INTO student.friend VALUES (1510, 1381); -INSERT INTO student.friend VALUES (1510, 1689); -INSERT INTO student.friend VALUES (1689, 1709); -INSERT INTO student.friend VALUES (1381, 1247); -INSERT INTO student.friend VALUES (1709, 1247); -INSERT INTO student.friend VALUES (1689, 1782); -INSERT INTO student.friend VALUES (1782, 1468); -INSERT INTO student.friend VALUES (1782, 1316); -INSERT INTO student.friend VALUES (1782, 1304); -INSERT INTO student.friend VALUES (1468, 1101); -INSERT INTO student.friend VALUES (1468, 1641); -INSERT INTO student.friend VALUES (1101, 1641); -INSERT INTO student.friend VALUES (1247, 1911); -INSERT INTO student.friend VALUES (1247, 1501); -INSERT INTO student.friend VALUES (1911, 1501); -INSERT INTO student.friend VALUES (1501, 1934); -INSERT INTO student.friend VALUES (1316, 1934); -INSERT INTO student.friend VALUES (1934, 1304); -INSERT INTO student.friend VALUES (1304, 1661); -INSERT INTO student.friend VALUES (1661, 1025); \ No newline at end of file +INSERT INTO friend VALUES (1510, 1381); +INSERT INTO friend VALUES (1510, 1689); +INSERT INTO friend VALUES (1689, 1709); +INSERT INTO friend VALUES (1381, 1247); +INSERT INTO friend VALUES (1709, 1247); +INSERT INTO friend VALUES (1689, 1782); +INSERT INTO friend VALUES (1782, 1468); +INSERT INTO friend VALUES (1782, 1316); +INSERT INTO friend VALUES (1782, 1304); +INSERT INTO friend VALUES (1468, 1101); +INSERT INTO friend VALUES (1468, 1641); +INSERT INTO friend VALUES (1101, 1641); +INSERT INTO friend VALUES (1247, 1911); +INSERT INTO friend VALUES (1247, 1501); +INSERT INTO friend VALUES (1911, 1501); +INSERT INTO friend VALUES (1501, 1934); +INSERT INTO friend VALUES (1316, 1934); +INSERT INTO friend VALUES (1934, 1304); +INSERT INTO friend VALUES (1304, 1661); +INSERT INTO friend VALUES (1661, 1025); \ No newline at end of file diff --git a/sql/07-query-friends-gabriel.sql b/sql/07-query-friends-gabriel.sql index 1c63b46..72570fb 100644 --- a/sql/07-query-friends-gabriel.sql +++ b/sql/07-query-friends-gabriel.sql @@ -1,45 +1,13 @@ -SELECT name -FROM student.student -JOIN student.friend -ON student.student.id = student.friend.id1 -WHERE id2 = 1911 OR id2 = 1689; - - ---Alternative - -select -student2.name -from -student -join -friend -on -student.id=friend.id1 -join -student -as -student2 -on -student2.id = friend.id2 -where -student.name='Gabriel' - ---alternative 2 - -select -s2.name -from -friend -join -student -as -s1.id = friend.id1 -join -student -as -s2 -on -s2.id=friend.id2 -where -s1.name='Gabriel' +SELECT + name +FROM + student.student +JOIN + student.friend +ON + student.student.id = student.friend.id1 +WHERE + id2 = 1911 +OR + id2 = 1689; diff --git a/sql/08-query-likes-grade-two-or-more.sql b/sql/08-query-likes-grade-two-or-more.sql index 37fa168..ca83918 100644 --- a/sql/08-query-likes-grade-two-or-more.sql +++ b/sql/08-query-likes-grade-two-or-more.sql @@ -14,8 +14,8 @@ ON JOIN student AS - friends + student_who_is_liked ON student_who_is_liked.id = student_like.likee_id WHERE - student_who_likes.grade - student_who_is_liked.grade >= 1 \ No newline at end of file + student_who_likes.grade - student_who_is_liked.grade >= 1; \ No newline at end of file diff --git a/sql/09-mutual-likes.sql b/sql/09-mutual-likes.sql index b9d6d47..aca0d7a 100644 --- a/sql/09-mutual-likes.sql +++ b/sql/09-mutual-likes.sql @@ -1,11 +1,81 @@ -SELECT first_name, grade -FROM student -JOIN student_like -ON student.id = student_like.liker_id -WHERE (id IN (SELECT liker_id FROM student_like)) +SELECT DISTINCT + student1.name, + student1.grade, + student2.name, + student2.grade +FROM + student +AS + student1 +JOIN + student_like +ON + student1.id = student_like.liker_id +JOIN + student +AS + student2 +ON + student_like.likee_id = student2.id +WHERE + student1.id IN ( + SELECT likee_id FROM student_like + ) +ORDER BY + student1.name; ---Attempted, but not really ---09: For every pair of students who both like each other, return the name ---and grade of both students. Include each pair only once, with the two names in alphabetical order. ---Write the SQL in the file sql/09-mutual-likes.sql \ No newline at end of file + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +SELECT +first_name, grade +FROM +student +JOIN +student_like +AS +a_student_who_likes_b_student +ON +student.id = a_student_who_likes_b_student.liker_id +JOIN +student_like +AS +b_student_who_likes_a_student +ON +student.id = a_student_who_likes_b_student.likee_id + +WHERE +(id +IN (SELECT +liker_id +FROM +student_like)) \ No newline at end of file diff --git a/sql/10-not-liked.sql b/sql/10-not-liked.sql index 728d121..3fd5a95 100644 --- a/sql/10-not-liked.sql +++ b/sql/10-not-liked.sql @@ -1,6 +1,16 @@ -SELECT first_name, grade -FROM student -WHERE (id NOT IN (SELECT liker_id FROM student_like)) -AND (id NOT IN (SELECT likee_id FROM student_like)); - ---attempted not sure if it's right +SELECT + name, grade +FROM + student +WHERE + (id NOT +IN (SELECT + liker_id +FROM + student_like)) +AND + (id NOT +IN (SELECT + likee_id +FROM + student_like)); diff --git a/sql/11-liked-but-does-not-like.sql b/sql/11-liked-but-does-not-like.sql index e69de29..5443a09 100644 --- a/sql/11-liked-but-does-not-like.sql +++ b/sql/11-liked-but-does-not-like.sql @@ -0,0 +1,3 @@ +--11: For every situation where student A likes student B, but we have no information about +--whom B likes (that is, B's id does not appear in the liker_id column of the like table), +--return A and B's names and grades. Write the SQL in the file sql/11-liked-but-does-not-like.sql diff --git a/sql/12-find-friends-in-common.sql b/sql/12-find-friends-in-common.sql index e69de29..70a2cfc 100644 --- a/sql/12-find-friends-in-common.sql +++ b/sql/12-find-friends-in-common.sql @@ -0,0 +1,3 @@ +--12: For each student A who likes a student B where the two are not friends, find if they have a friend C in common +--(who can introduce them!). For all such trios, return the name and grade of A, B, and C. +--Write the SQL in the file sql/12-find-friends-in-common.sql \ No newline at end of file diff --git a/sql/13-popular-students.sql b/sql/13-popular-students.sql index 8411465..6bb5544 100644 --- a/sql/13-popular-students.sql +++ b/sql/13-popular-students.sql @@ -1,6 +1,12 @@ -SELECT name -FROM student.student_like -JOIN student.student -ON student.student.id = student.student_like.likee_id -GROUP BY name -HAVING count(likee_id) >= 2; \ No newline at end of file +SELECT + name +FROM + student.student_like +JOIN + student.student +ON + student.student.id = student.student_like.likee_id +GROUP BY + name +HAVING + count(likee_id) >= 2; \ No newline at end of file From 7ab91e6ca28d7bb0e906ac3a6c8eecc543cfb2de Mon Sep 17 00:00:00 2001 From: Aaron Villanueva Date: Sat, 11 Mar 2017 16:50:47 -0800 Subject: [PATCH 11/12] exercise 11 --- sql/04-load-table-student.sql | 4 +--- sql/11-liked-but-does-not-like.sql | 28 +++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/sql/04-load-table-student.sql b/sql/04-load-table-student.sql index 4bf20a6..2307a22 100644 --- a/sql/04-load-table-student.sql +++ b/sql/04-load-table-student.sql @@ -13,6 +13,4 @@ INSERT INTO student VALUES (1501, 'Jessica', 11); INSERT INTO student VALUES (1304, 'Jordan', 12); INSERT INTO student VALUES (1025, 'John', 12); INSERT INTO student VALUES (1934, 'Kyle', 12); -INSERT INTO student VALUES (1661, 'Logan', 12); - ---laod it through the path \ No newline at end of file +INSERT INTO student VALUES (1661, 'Logan', 12); \ No newline at end of file diff --git a/sql/11-liked-but-does-not-like.sql b/sql/11-liked-but-does-not-like.sql index 5443a09..0c60f54 100644 --- a/sql/11-liked-but-does-not-like.sql +++ b/sql/11-liked-but-does-not-like.sql @@ -1,3 +1,25 @@ ---11: For every situation where student A likes student B, but we have no information about ---whom B likes (that is, B's id does not appear in the liker_id column of the like table), ---return A and B's names and grades. Write the SQL in the file sql/11-liked-but-does-not-like.sql +SELECT DISTINCT + student_a_who_likes_student_b.name, + student_a_who_likes_student_b.grade, + student_b_who_likes_no_one.name, + student_b_who_likes_no_one.grade +FROM + student +AS + student_a_who_likes_student_b +JOIN + student_like +ON + student_a_who_likes_student_b.id = student_like.liker_id +JOIN + student +AS + student_b_who_likes_no_one +ON + student_like.likee_id = student_b_who_likes_no_one.id +WHERE + student_b_who_likes_no_one.id NOT IN ( + SELECT liker_id FROM student_like + ) +ORDER BY + student_a_who_likes_student_b.name; \ No newline at end of file From 50372d6c37cf1aff4c13d0894a690656f42f6306 Mon Sep 17 00:00:00 2001 From: Aaron Villanueva Date: Mon, 13 Mar 2017 08:43:59 -0700 Subject: [PATCH 12/12] final commit --- sql/12-find-friends-in-common.sql | 41 ++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/sql/12-find-friends-in-common.sql b/sql/12-find-friends-in-common.sql index 70a2cfc..ef295b5 100644 --- a/sql/12-find-friends-in-common.sql +++ b/sql/12-find-friends-in-common.sql @@ -1,3 +1,38 @@ ---12: For each student A who likes a student B where the two are not friends, find if they have a friend C in common ---(who can introduce them!). For all such trios, return the name and grade of A, B, and C. ---Write the SQL in the file sql/12-find-friends-in-common.sql \ No newline at end of file +SELECT DISTINCT + student_a.name, + student_a.grade, + student_b.name, + student_b.grade, + student_c.name, + student_c.grade +FROM + student +JOIN + student_like +ON + student.id = student_like.liker_id +JOIN + friend +ON + student_like.likee_id = friend.id1 +JOIN + student +AS + student_a +ON + student_a.id = student_like.liker_id +JOIN + student +AS + student_b +ON + student_b.id = student_like.likee_id +JOIN + student +AS + student_c +ON + student_c.id = friend.id1 +ORDER BY + student_a.first_name +LIMIT 2