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/README.md b/README.md index 2c1466c..1a5a0d5 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 + - [ ] sql/12-find-friends-in-common.sql + - [ ] 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. diff --git a/sql/01-create-table-student.sql b/sql/01-create-table-student.sql index e69de29..a2c3e04 100644 --- a/sql/01-create-table-student.sql +++ b/sql/01-create-table-student.sql @@ -0,0 +1,5 @@ +CREATE TABLE student ( + 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 e69de29..8363993 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) +); 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 e69de29..db76c65 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) +); 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 e69de29..95bf564 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..9ae62db 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..cbd1725 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..7ffa582 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..a758f82 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);