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
29 changes: 6 additions & 23 deletions sql/01-create-table-student.sql
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
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
);
\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);
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),

Choose a reason for hiding this comment

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

A clearer naming structure for a join table would be something like:

student_friends (includes a reference to the student table in the table name)
   id (the primary key of the student)
   friend_id (the primary key of another student)

It might seem a little redundant to differentiate between the two student ids, but it would allow you to select all of a student's friends using something like:

SELECT friend_id FROM student_friends WHERE id = (studentId)

);
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);
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
JOIN
student.friend
ON
student.student.id = student.friend.id1
WHERE
id2 = 1911
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
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;















Choose a reason for hiding this comment

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

This much whitespace doesn't make it any clearer why you're breaking up the queries, so it isn't really helpful - A better way to separate them would be with a comment line to explain the difference, or by making 09-mutual-likes a directory and separating the queries into different sql files inside that directory.



















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;