-
Notifications
You must be signed in to change notification settings - Fork 0
forgetful-elephant - 178 core sql solo #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: empty
Are you sure you want to change the base?
Changes from all commits
c181b82
14ca9bd
0961c4f
14eeab2
db8605d
6dc3a5a
9e450bd
ca0c6fc
a1c069c
503169e
6350922
7ab91e6
50372d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| ); |
| 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), | ||
| ); |
| 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), | ||
| ); |
| 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); | ||
| 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); |
| 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); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| SELECT | ||
| name | ||
| FROM | ||
| student.student | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here you could go s.id = f.id1 |
||
| WHERE | ||
| id2 = 1911 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
|
||
| 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; |
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
| 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)); |
| 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; |
| 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 |
| 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; |
There was a problem hiding this comment.
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.