Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
6 changes: 3 additions & 3 deletions sql/01-create-table-student.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE student (
id INT PRIMARY KEY,
name TEXT,
grade INT
id SERIAL PRIMARY KEY,
name VARCHAR(255),
grade INTEGER
);
4 changes: 2 additions & 2 deletions sql/02-create-table-friend.sql
Original file line number Diff line number Diff line change
@@ -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)
);
4 changes: 4 additions & 0 deletions sql/02-create-table-friend0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE friend (
id1 INT REFERENCES student(id),
id2 INT REFERENCES student(id)
);
4 changes: 4 additions & 0 deletions sql/02-create-table-friend1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE friend (
id1 INT REFERENCES student(id),
id2 INT REFERENCES student(id)
);
6 changes: 3 additions & 3 deletions sql/03-create-table-like.sql
Original file line number Diff line number Diff line change
@@ -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)
);
2 changes: 2 additions & 0 deletions sql/04-load-table-student.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
COPY student
FROM '/Users/kamilalambert/Desktop/Projects/sql-exercises/data/students.csv' DELIMITER ',' CSV HEADER;
2 changes: 2 additions & 0 deletions sql/05-load-table-friend.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
COPY friend
FROM '/Users/kamilalambert/Desktop/Projects/sql-exercises/data/friends.csv' DELIMITER ',' CSV HEADER;
2 changes: 2 additions & 0 deletions sql/06-load-table-like.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
COPY student_like
FROM '/Users/kamilalambert/Desktop/Projects/sql-exercises/data/likes.csv' DELIMITER ',' CSV HEADER;
4 changes: 4 additions & 0 deletions sql/07-query-friends-gabriel.sql
Original file line number Diff line number Diff line change
@@ -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';
7 changes: 7 additions & 0 deletions sql/08-query-likes-grade-two-or-more.sql
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 13 additions & 0 deletions sql/09-mutual-likes.sql
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 8 additions & 0 deletions sql/10-not-liked.sql
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 additions & 0 deletions sql/11-liked-but-does-not-like.sql
Original file line number Diff line number Diff line change
@@ -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);