/* 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