Instantly share code, notes, and snippets.
Last active
September 14, 2015 21:49
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save tengpeng/d56dbfec0e5f387ba289 to your computer and use it in GitHub Desktop.
My answers to SQL exercises for db-class.org /Part 2/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Delete the tables if they already exist */ | |
drop table if exists Highschooler; | |
drop table if exists Friend; | |
drop table if exists Likes; | |
/* Create the schema for our tables */ | |
create table Highschooler(ID int, name text, grade int); | |
create table Friend(ID1 int, ID2 int); | |
create table Likes(ID1 int, ID2 int); | |
/* Populate the tables with our data */ | |
insert into Highschooler values (1510, 'Jordan', 9); | |
insert into Highschooler values (1689, 'Gabriel', 9); | |
insert into Highschooler values (1381, 'Tiffany', 9); | |
insert into Highschooler values (1709, 'Cassandra', 9); | |
insert into Highschooler values (1101, 'Haley', 10); | |
insert into Highschooler values (1782, 'Andrew', 10); | |
insert into Highschooler values (1468, 'Kris', 10); | |
insert into Highschooler values (1641, 'Brittany', 10); | |
insert into Highschooler values (1247, 'Alexis', 11); | |
insert into Highschooler values (1316, 'Austin', 11); | |
insert into Highschooler values (1911, 'Gabriel', 11); | |
insert into Highschooler values (1501, 'Jessica', 11); | |
insert into Highschooler values (1304, 'Jordan', 12); | |
insert into Highschooler values (1025, 'John', 12); | |
insert into Highschooler values (1934, 'Kyle', 12); | |
insert into Highschooler values (1661, 'Logan', 12); | |
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); | |
insert into Friend select ID2, ID1 from Friend; | |
insert into Likes values(1689, 1709); | |
insert into Likes values(1709, 1689); | |
insert into Likes values(1782, 1709); | |
insert into Likes values(1911, 1247); | |
insert into Likes values(1247, 1468); | |
insert into Likes values(1641, 1468); | |
insert into Likes values(1316, 1304); | |
insert into Likes values(1501, 1934); | |
insert into Likes values(1934, 1501); | |
insert into Likes values(1025, 1101); | |
.mode column | |
.headers ON | |
/*----------- 1. SQL Social-Network Query Exercises -----------*/ | |
-- Q1 Find the names of all students who are friends with someone named Gabriel. | |
select distinct name from Highschooler where ID in (select ID1 from Friend where ID2 in (select ID from Highschooler where name="Gabriel")); | |
-- Q2 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 distinct sName, sGrade, lName, lGrade | |
from (select h1.name as sName, h1.grade sGrade, h2.name as lName, h2.grade as lGrade, h1.grade-h2.grade as gradeDiff | |
from Highschooler h1, Likes, Highschooler h2 | |
where h1.ID=ID1 and h2.ID=ID2) | |
where gradeDiff>1; | |
-- Q3 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 h1.name, h1.grade, h2.name, h2.grade from Likes l1, Likes l2, Highschooler h1, Highschooler h2 | |
where l1.ID1=l2.ID2 and l2.ID1=l1.ID2 and l1.ID1=h1.ID and l1.ID2=h2.ID and h1.name<h2.name; | |
-- Q4 Find all students who do not appear in the Likes 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 Highschooler where ID not in (select ID1 from Likes union select ID2 from Likes) order by grade, name; | |
-- Q5 For every situation where student A likes student B, but we have no information about whom B likes (that is, B does not appear as an ID1 in the Likes table), return A and B's names and grades. | |
select distinct H1.name, H1.grade, H2.name, H2.grade | |
from Highschooler H1, Likes, Highschooler H2 | |
where H1.ID = Likes.ID1 and Likes.ID2 = H2.ID and H2.ID not in (select ID1 from Likes); | |
-- Q6 Find names and grades of students who only have friends in the same grade. Return the result sorted by grade, then by name within each grade. | |
select name, grade from Highschooler | |
where ID not in ( | |
select ID1 from Highschooler H1, Friend, Highschooler H2 | |
where H1.ID = Friend.ID1 and Friend.ID2 = H2.ID and H1.grade <> H2.grade) | |
order by grade, name; | |
-- Q7 For each student A who likes a student B where the two are not friends, find if they have a friend C in common (who can introduce them!). For all such trios, return the name and grade of A, B, and C. | |
select distinct H1.name, H1.grade, H2.name, H2.grade, H3.name, H3.grade | |
from Highschooler H1, Likes, Highschooler H2, Highschooler H3, Friend F1, Friend F2 | |
where H1.ID = Likes.ID1 and Likes.ID2 = H2.ID and | |
H2.ID not in (select ID2 from Friend where ID1 = H1.ID) and | |
H1.ID = F1.ID1 and F1.ID2 = H3.ID and | |
H3.ID = F2.ID1 and F2.ID2 = H2.ID; | |
-- Q8 Find the difference between the number of students in the school and the number of different first names. | |
select st.sNum-nm.nNum from | |
(select count(*) as sNum from Highschooler) as st, | |
(select count(distinct name) as nNum from Highschooler) as nm; | |
-- Q9 Find the name and grade of all students who are liked by more than one other student. | |
select name, grade | |
from (select ID2, count(ID2) as numLiked from Likes group by ID2), Highschooler | |
where numLiked>1 and ID2=ID; | |
/*----------- Social Network Modification exercises -----------*/ | |
-- Q1 It's time for the seniors to graduate. Remove all 12th graders from Highschooler. | |
delete from Highschooler | |
where grade = 12; | |
-- Q2 If two students A and B are friends, and A likes B but not vice-versa, remove the Likes tuple. | |
delete from Likes | |
where ID2 in (select ID2 from Friend where Likes.ID1 = ID1) and | |
ID2 not in (select L.ID1 from Likes L where Likes.ID1 = L.ID2); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment