From f50e12760571e3ea75827d663f3efda835650b60 Mon Sep 17 00:00:00 2001 From: Kamila Lambert Date: Thu, 27 Apr 2017 10:26:25 -0700 Subject: [PATCH 01/10] created student table --- sql/01-create-table-student.sql | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sql/01-create-table-student.sql b/sql/01-create-table-student.sql index e69de29..4064ffe 100644 --- a/sql/01-create-table-student.sql +++ b/sql/01-create-table-student.sql @@ -0,0 +1,5 @@ +CREATE TABLE student ( + id INT PRIMARY KEY, + name TEXT, + grade INT +); From 5e52ecfdc9baff9750076b85bf2574bee459bf71 Mon Sep 17 00:00:00 2001 From: Kamila Lambert Date: Thu, 27 Apr 2017 11:10:27 -0700 Subject: [PATCH 02/10] created friends table --- sql/02-create-table-friend.sql | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sql/02-create-table-friend.sql b/sql/02-create-table-friend.sql index e69de29..c146e43 100644 --- a/sql/02-create-table-friend.sql +++ b/sql/02-create-table-friend.sql @@ -0,0 +1,4 @@ +CREATE TABLE friend ( + id1 INT REFERENCES STUDENT(ID), + id2 INT REFERENCES STUDENT(ID) +); From cad12e6c032d81a145c50c3f2b38e5bbcd5026d9 Mon Sep 17 00:00:00 2001 From: Kamila Lambert Date: Thu, 27 Apr 2017 11:21:39 -0700 Subject: [PATCH 03/10] created like table --- .DS_Store | Bin 0 -> 6148 bytes sql/03-create-table-like.sql | 4 ++++ 2 files changed, 4 insertions(+) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..42c93e74986fb26e1fb4039dff969d29f881789d GIT binary patch literal 6148 zcmeHKyJ`bL3>?K*7}B^*xxe5)Sd8-p{(%?_X^exD*!5NUu6$ZX4+8c%kS0wSfi$~w z(k?f}=>))5r`-}*0GQJq@!?@={@i_Hcf~lGpK;#29`-MX=iE>7ZxLQQV23TQsA!@_yXyU7>@t| literal 0 HcmV?d00001 diff --git a/sql/03-create-table-like.sql b/sql/03-create-table-like.sql index e69de29..9166a8f 100644 --- a/sql/03-create-table-like.sql +++ b/sql/03-create-table-like.sql @@ -0,0 +1,4 @@ +CREATE TABLE student_like ( + liker_id INT references student(ID), + likee_id INT references student(ID) +); From c37b5f9a85f1a5f6abae399164f11659a449685a Mon Sep 17 00:00:00 2001 From: Kamila Lambert Date: Thu, 27 Apr 2017 11:23:22 -0700 Subject: [PATCH 04/10] created like table --- sql/03-create-table-like.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/03-create-table-like.sql b/sql/03-create-table-like.sql index 9166a8f..84d6cd7 100644 --- a/sql/03-create-table-like.sql +++ b/sql/03-create-table-like.sql @@ -1,4 +1,4 @@ CREATE TABLE student_like ( liker_id INT references student(ID), likee_id INT references student(ID) -); +); From f65309878c848b53938cce9de2e25f2d89e0ef86 Mon Sep 17 00:00:00 2001 From: Kamila Lambert Date: Fri, 28 Apr 2017 10:03:19 -0700 Subject: [PATCH 05/10] all queries properly loading required data --- .DS_Store | Bin 6148 -> 0 bytes sql/07-query-friends-gabriel.sql | 4 ++++ sql/08-query-likes-grade-two-or-more.sql | 7 +++++++ sql/09-mutual-likes.sql | 13 +++++++++++++ sql/10-not-liked.sql | 8 ++++++++ sql/11-liked-but-does-not-like.sql | 6 ++++++ 6 files changed, 38 insertions(+) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 42c93e74986fb26e1fb4039dff969d29f881789d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKyJ`bL3>?K*7}B^*xxe5)Sd8-p{(%?_X^exD*!5NUu6$ZX4+8c%kS0wSfi$~w z(k?f}=>))5r`-}*0GQJq@!?@={@i_Hcf~lGpK;#29`-MX=iE>7ZxLQQV23TQsA!@_yXyU7>@t| diff --git a/sql/07-query-friends-gabriel.sql b/sql/07-query-friends-gabriel.sql index e69de29..ad62951 100644 --- a/sql/07-query-friends-gabriel.sql +++ b/sql/07-query-friends-gabriel.sql @@ -0,0 +1,4 @@ +-- Find the names of all students who are friends with someone named Gabriel. +SELECT b.name FROM friend +JOIN student a ON a.id = friend.id1 +JOIN student b ON b.id = friend.id2 WHERE a.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 e69de29..c2dcc35 100644 --- a/sql/08-query-likes-grade-two-or-more.sql +++ b/sql/08-query-likes-grade-two-or-more.sql @@ -0,0 +1,7 @@ +-- For every student who likes someone 2 or more grades younger than themselves, return that student's name and grade, and the name and grade of the student they like. + +SELECT a.name liker_name, a.grade liker_grade, b.name likee_name, b.grade likee_grade + FROM student_like + JOIN student a ON a.id = student_like.liker_id + JOIN student b ON b.id = student_like.likee_id + WHERE (a.grade - b.grade) >= 2; diff --git a/sql/09-mutual-likes.sql b/sql/09-mutual-likes.sql index e69de29..bdb209e 100644 --- a/sql/09-mutual-likes.sql +++ b/sql/09-mutual-likes.sql @@ -0,0 +1,13 @@ +-- 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. + +SELECT s1.name liker_name, s1.grade liker_grade, s2.name likee_name, s2.grade likee_grade + FROM ( + SELECT a.liker_id, a.likee_id + FROM student_like a + JOIN student_like b + ON a.liker_id = b.likee_id AND b.liker_id = a.likee_id + ) mutual + JOIN student s1 ON s1.id = mutual.liker_id + JOIN student s2 ON s2.id = mutual.likee_id + WHERE s1.name < s2.name + ORDER BY s1.name ASC; diff --git a/sql/10-not-liked.sql b/sql/10-not-liked.sql index e69de29..770fb4a 100644 --- a/sql/10-not-liked.sql +++ b/sql/10-not-liked.sql @@ -0,0 +1,8 @@ +-- Find all students who do not appear in the like table (as a student who likes or is liked) and return their names and grades. Sort by grade, then by name within each grade. + +SELECT name, grade FROM student + WHERE id NOT IN + (SELECT likee_id FROM student_like) + AND id NOT IN + (SELECT liker_id FROM student_like) + ORDER BY grade ASC, name ASC; diff --git a/sql/11-liked-but-does-not-like.sql b/sql/11-liked-but-does-not-like.sql index e69de29..e53f7ab 100644 --- a/sql/11-liked-but-does-not-like.sql +++ b/sql/11-liked-but-does-not-like.sql @@ -0,0 +1,6 @@ +-- 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. + +SELECT a.name, a.grade, b.name, b.grade FROM student_like + JOIN student a ON a.id = student_like.liker_id + JOIN student b ON b.id = student_like.likee_id + WHERE b.id NOT IN (SELECT liker_id FROM student_like); From be013132d1a9e5a138cd51d5496643a67e233b50 Mon Sep 17 00:00:00 2001 From: Kamila Lambert Date: Fri, 28 Apr 2017 10:05:30 -0700 Subject: [PATCH 06/10] all tables created with data loaded and queries pulling correct data --- .gitignore | 1 + sql/01-create-table-student.sql | 6 +++--- sql/02-create-table-friend.sql | 4 ++-- sql/02-create-table-friend0.sql | 4 ++++ sql/02-create-table-friend1.sql | 4 ++++ sql/03-create-table-like.sql | 6 +++--- sql/04-load-table-student.sql | 2 ++ sql/05-load-table-friend.sql | 2 ++ sql/06-load-table-like.sql | 2 ++ sql/07-query-friends-gabriel.sql | 2 +- sql/08-query-likes-grade-two-or-more.sql | 2 +- sql/09-mutual-likes.sql | 2 +- sql/10-not-liked.sql | 2 +- sql/11-liked-but-does-not-like.sql | 2 +- 14 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 .gitignore create mode 100644 sql/02-create-table-friend0.sql create mode 100644 sql/02-create-table-friend1.sql diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/sql/01-create-table-student.sql b/sql/01-create-table-student.sql index 4064ffe..a2c3e04 100644 --- a/sql/01-create-table-student.sql +++ b/sql/01-create-table-student.sql @@ -1,5 +1,5 @@ CREATE TABLE student ( - id INT PRIMARY KEY, - name TEXT, - grade INT + id SERIAL PRIMARY KEY, + name VARCHAR(255), + grade INTEGER ); diff --git a/sql/02-create-table-friend.sql b/sql/02-create-table-friend.sql index c146e43..8363993 100644 --- a/sql/02-create-table-friend.sql +++ b/sql/02-create-table-friend.sql @@ -1,4 +1,4 @@ CREATE TABLE friend ( - id1 INT REFERENCES STUDENT(ID), - id2 INT REFERENCES STUDENT(ID) + id1 INT REFERENCES student(id), + id2 INT REFERENCES student(id) ); diff --git a/sql/02-create-table-friend0.sql b/sql/02-create-table-friend0.sql new file mode 100644 index 0000000..8363993 --- /dev/null +++ b/sql/02-create-table-friend0.sql @@ -0,0 +1,4 @@ +CREATE TABLE friend ( + id1 INT REFERENCES student(id), + id2 INT REFERENCES student(id) +); diff --git a/sql/02-create-table-friend1.sql b/sql/02-create-table-friend1.sql new file mode 100644 index 0000000..8363993 --- /dev/null +++ b/sql/02-create-table-friend1.sql @@ -0,0 +1,4 @@ +CREATE TABLE friend ( + id1 INT REFERENCES student(id), + id2 INT REFERENCES student(id) +); diff --git a/sql/03-create-table-like.sql b/sql/03-create-table-like.sql index 84d6cd7..db76c65 100644 --- a/sql/03-create-table-like.sql +++ b/sql/03-create-table-like.sql @@ -1,4 +1,4 @@ CREATE TABLE student_like ( - liker_id INT references student(ID), - likee_id INT references student(ID) -); + liker_id INT REFERENCES student(ID), + likee_id INT REFERENCES student(ID) +); diff --git a/sql/04-load-table-student.sql b/sql/04-load-table-student.sql index e69de29..0dfc0e6 100644 --- a/sql/04-load-table-student.sql +++ b/sql/04-load-table-student.sql @@ -0,0 +1,2 @@ +COPY student +FROM '/Users/kamilalambert/Desktop/Projects/sql-exercises/data/students.csv' DELIMITER ',' CSV HEADER; diff --git a/sql/05-load-table-friend.sql b/sql/05-load-table-friend.sql index e69de29..7a83c1f 100644 --- a/sql/05-load-table-friend.sql +++ b/sql/05-load-table-friend.sql @@ -0,0 +1,2 @@ +COPY friend +FROM '/Users/kamilalambert/Desktop/Projects/sql-exercises/data/friends.csv' DELIMITER ',' CSV HEADER; diff --git a/sql/06-load-table-like.sql b/sql/06-load-table-like.sql index e69de29..128a3f2 100644 --- a/sql/06-load-table-like.sql +++ b/sql/06-load-table-like.sql @@ -0,0 +1,2 @@ +COPY student_like +FROM '/Users/kamilalambert/Desktop/Projects/sql-exercises/data/likes.csv' DELIMITER ',' CSV HEADER; diff --git a/sql/07-query-friends-gabriel.sql b/sql/07-query-friends-gabriel.sql index ad62951..95bf564 100644 --- a/sql/07-query-friends-gabriel.sql +++ b/sql/07-query-friends-gabriel.sql @@ -1,4 +1,4 @@ --- Find the names of all students who are friends with someone named Gabriel. +-- Find the names of all students who are friends with someone named Gabriel. SELECT b.name FROM friend JOIN student a ON a.id = friend.id1 JOIN student b ON b.id = friend.id2 WHERE a.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 c2dcc35..9ae62db 100644 --- a/sql/08-query-likes-grade-two-or-more.sql +++ b/sql/08-query-likes-grade-two-or-more.sql @@ -1,4 +1,4 @@ --- For every student who likes someone 2 or more grades younger than themselves, return that student's name and grade, and the name and grade of the student they like. +-- For every student who likes someone 2 or more grades younger than themselves, return that student's name and grade, and the name and grade of the student they like. SELECT a.name liker_name, a.grade liker_grade, b.name likee_name, b.grade likee_grade FROM student_like diff --git a/sql/09-mutual-likes.sql b/sql/09-mutual-likes.sql index bdb209e..cbd1725 100644 --- a/sql/09-mutual-likes.sql +++ b/sql/09-mutual-likes.sql @@ -1,4 +1,4 @@ --- 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. +-- 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. SELECT s1.name liker_name, s1.grade liker_grade, s2.name likee_name, s2.grade likee_grade FROM ( diff --git a/sql/10-not-liked.sql b/sql/10-not-liked.sql index 770fb4a..7ffa582 100644 --- a/sql/10-not-liked.sql +++ b/sql/10-not-liked.sql @@ -1,4 +1,4 @@ --- Find all students who do not appear in the like table (as a student who likes or is liked) and return their names and grades. Sort by grade, then by name within each grade. +-- Find all students who do not appear in the like table (as a student who likes or is liked) and return their names and grades. Sort by grade, then by name within each grade. SELECT name, grade FROM student WHERE id NOT IN diff --git a/sql/11-liked-but-does-not-like.sql b/sql/11-liked-but-does-not-like.sql index e53f7ab..a758f82 100644 --- a/sql/11-liked-but-does-not-like.sql +++ b/sql/11-liked-but-does-not-like.sql @@ -1,4 +1,4 @@ --- 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. +-- 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. SELECT a.name, a.grade, b.name, b.grade FROM student_like JOIN student a ON a.id = student_like.liker_id From a48f158ef79cb6594757fcb9e53d64abccff7c4e Mon Sep 17 00:00:00 2001 From: Kamila Lambert Date: Fri, 28 Apr 2017 10:19:12 -0700 Subject: [PATCH 07/10] updated readme with completed check boxes --- README.md | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/README.md b/README.md index 2c1466c..853dcdd 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,69 @@ +# Day 1: SQL Basics & Advanced SQL Queries + +- [x]Complete the SQL Basics course +- [x]Complete the Advanced SQL queries course + +# Day 2: Relational Queries + +- [x]Complete the Relational Queries course +- [x]Complete the Modifying databases with SQL course + +# Day 3: Install Postgres and Complete Tutorials with psql + +- [x] Install Homebrew by following instructions listed here +- [x] Install Postgres by following instructions listed here +- [x] Learn the psql command line tool by working through the following tutorials + - [x] complete the tutorial for createdb here + - [x] complete the tutorial for selecting the database here + - [x] complete the tutorial for dropping the database here + - [x] complete the tutorial for create table here + - [x] complete the tutorial for drop table here + - [x] complete the tutorial for schema here + - [x] complete the tutorial for insert here + - [x] complete the tutorial for select here + - [x] complete the tutorial for expressions here + - [x] complete the tutorial for where clause here + - [x] complete the tutorial for and and or operators here + - [x] complete the tutorial for update queries here + - [x] complete the tutorial for delete queries here + - [x] complete the tutorial for the like clause here + - [x] complete the tutorial for the limit clause here + - [x] complete the tutorial for the order by clause here + - [x] complete the tutorial for the group by clause here + +#Day 4: Mini Project & Exercises + + - [x]Exercises 1-11 in the sql-exercises repo are complete and written to the appropriate file: + - [x] sql/01-create-table-student.sql + - [x] sql/02-create-table-friend.sql + - [x] sql/03-create-table-like.sql + - [x] sql/04-load-table-learner.sql + - [x] sql/05-load-table-friend.sql + - [x] sql/06-load-table-like.sql + - [x] sql/07-query-friends-gabriel.sql + - [x] sql/08-query-likes-grade-two-or-more.sql + - [x] sql/09-mutual-likes.sql + - [x] sql/10-not-liked.sql + - [x] sql/11-liked-but-does-not-like.sql + [Stretch] sql/12-find-friends-in-common.sql + [Stretch] sql/13-popular-students.sql + +# Day 5: Exercises on SQL Bolt + + - [x] Complete the SQL Bolt tutorial + + +# General + + - [x] All major features are added via pull requests with a clear description and concise commit messages. + - [x] SQL files are well formatted and readable. + - [x] All the SQL keywords are capitalized. + +# Stretch + + - [ ] Complete the Codeacademy course on SQL + - [ ] Complete the SQL Zoo quizzes + # SQL Exercises Exercises to help exercise the SQL muscles. From 2b1b90f55e3bf844b80bc49b5f4460a3f66c5d8a Mon Sep 17 00:00:00 2001 From: Kamila Lambert Date: Fri, 28 Apr 2017 10:22:14 -0700 Subject: [PATCH 08/10] fixed some readme formatting issues --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 853dcdd..a901e14 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # Day 1: SQL Basics & Advanced SQL Queries -- [x]Complete the SQL Basics course -- [x]Complete the Advanced SQL queries course +- [x] Complete the SQL Basics course +- [x] Complete the Advanced SQL queries course # Day 2: Relational Queries -- [x]Complete the Relational Queries course -- [x]Complete the Modifying databases with SQL course +- [x] Complete the Relational Queries course +- [x] Complete the Modifying databases with SQL course # Day 3: Install Postgres and Complete Tutorials with psql @@ -45,8 +45,8 @@ - [x] sql/09-mutual-likes.sql - [x] sql/10-not-liked.sql - [x] sql/11-liked-but-does-not-like.sql - [Stretch] sql/12-find-friends-in-common.sql - [Stretch] sql/13-popular-students.sql + - [ ] sql/12-find-friends-in-common.sql + - [ ] sql/13-popular-students.sql # Day 5: Exercises on SQL Bolt From ab8fe5ece098e3febc32e4ea06d1a5c5f43e47f1 Mon Sep 17 00:00:00 2001 From: Kamila Lambert Date: Fri, 28 Apr 2017 10:24:46 -0700 Subject: [PATCH 09/10] fixed readme formatting again --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a901e14..850975f 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ #Day 4: Mini Project & Exercises - - [x]Exercises 1-11 in the sql-exercises repo are complete and written to the appropriate file: + - [x] Exercises 1-11 in the sql-exercises repo are complete and written to the appropriate file: - [x] sql/01-create-table-student.sql - [x] sql/02-create-table-friend.sql - [x] sql/03-create-table-like.sql From a8db596a66bbea7da612e780b3f4cdf167269aee Mon Sep 17 00:00:00 2001 From: Kamila Lambert Date: Fri, 28 Apr 2017 10:26:11 -0700 Subject: [PATCH 10/10] final readme formatting complete --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 850975f..1a5a0d5 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ - [x] complete the tutorial for the order by clause here - [x] complete the tutorial for the group by clause here -#Day 4: Mini Project & Exercises +# Day 4: Mini Project & Exercises - [x] Exercises 1-11 in the sql-exercises repo are complete and written to the appropriate file: - [x] sql/01-create-table-student.sql