Skip to content
Open
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 added .DS_Store
Binary file not shown.
6 changes: 6 additions & 0 deletions sql/01-create-table-student.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DROP TABLE IF EXISTS student;
CREATE TABLE student (
id SERIAL PRIMARY KEY,
name VARCHAR(255),
grade INTEGER
);
5 changes: 5 additions & 0 deletions sql/02-create-table-friend.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DROP TABLE IF EXISTS friend;
CREATE TABLE friend (
id1 INTEGER REFERENCES student(id),
id2 INTEGER REFERENCES student(id),
);
5 changes: 5 additions & 0 deletions sql/03-create-table-like.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DROP TABLE IF EXISTS student_like;
CREATE TABLE student_like (
liker_id INTEGER REFERENCES student(id),
likee_id INTEGER REFERENCES student(id),
);
16 changes: 16 additions & 0 deletions sql/04-load-table-student.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
INSERT INTO student VALUES (1510, 'Jordan', 9);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a workaround from loading the CSV file directly. Different database systems have different ways of doing this (copy, load data infile), but it'll be good for you to learn the methods because sometimes you'll end up having to work with big files, or you will be automating things and so you won't want to write the code by hand.

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);
20 changes: 20 additions & 0 deletions sql/05-load-table-friend.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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);
10 changes: 10 additions & 0 deletions sql/06-load-table-like.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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);
13 changes: 13 additions & 0 deletions sql/07-query-friends-gabriel.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
SELECT
name
FROM
student.student
Copy link

@ghost ghost Mar 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part looks fine, just wanted to comment that often in industry people would alias their tables here. E.g. "student.student s" would alias the table "s"

JOIN
student.friend
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be aliased too (for example f, or t2)

ON
student.student.id = student.friend.id1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here you could go s.id = f.id1

WHERE
id2 = 1911
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spirit of this task is to query everyone who is friends with someone named Gabriel, so you would probably want the where clause to be on the name. Hardcoding it for the two student IDs of people named Gabriel is manual (so it's actually more work), and your query doesn't reflect the spirit of the question, and it will break if there are any new people named Gabriel. Ideally, someone would be able to tell what you're trying to do by reading your query.

OR
id2 = 1689;

21 changes: 21 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,21 @@
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
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;
81 changes: 81 additions & 0 deletions sql/09-mutual-likes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
SELECT DISTINCT
student1.name,
student1.grade,
student2.name,
student2.grade
FROM
student
AS
student1
Copy link

@ghost ghost Mar 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine! Just a comment - It's okay to make short aliases, like s1, or s2, etc. to make typing easier.

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;

































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))
16 changes: 16 additions & 0 deletions sql/10-not-liked.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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));
25 changes: 25 additions & 0 deletions sql/11-liked-but-does-not-like.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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;
38 changes: 38 additions & 0 deletions sql/12-find-friends-in-common.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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
12 changes: 12 additions & 0 deletions sql/13-popular-students.sql
Original file line number Diff line number Diff line change
@@ -0,0 +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;