Students at your hometown high school have decided to organi

Students at your hometown high school have decided to organize their social network using databases.

So far, they have collected information about sixteen students in four grades, 9-12. Here\'s the schema: Highschooler ( ID, name, grade ): there is a high school student with unique ID and a given first name in a certain grade. Friend ( ID1, ID2 ): the student with ID1 is friends with the student with ID2. Friendship is mutual, so if (123, 456) is in the Friend table, so is (456, 123). Likes ( ID1, ID2 ): the student with ID1 likes the student with ID2. Liking someone is not necessarily mutual, so if (123, 456) is in the Likes table, there is no guarantee that (456, 123) is also present.

Create SQL Queries to Find:

1. Find the names of all students who are friends with someone named Gabriel.

2. 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. 3. Find the name and grade of all students who are liked by more than one other student.

3. 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.

Solution

1. Find the names of all students who are friends with someone named Gabriel

select name from Highschooler where id in (
   select id2 from Friend where id1 in (
   select id from Highschooler where name=\'Gabriel\'
   )
)

2. 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

select name, grade from Highschooler where id not in(
   select distinct id1 from likes
   union all
select distinct id2 from likes
   )

3a. Find the name and grade of all students who are liked by more than one other student.
select hl.name, hl.grade from Highschooler hl, likes l where l.id2 = hl.id group by l.id2 having count(l.id1) > 1

3b. 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 a.name, a.grade, b.name, b.grade from Highschooler a, Highschooler b, likes l where a.id=l.id1 and b.id=l.id2 and a.grade >= (b.grade + 2)




I have created some sql code to test the above queries:

Drop table IF EXISTS Highschooler; \\\\
create table Highschooler(
id int,
name varchar(155),
grade int
); \\\\

Drop table IF EXISTS Friend; \\\\
create table Friend(
id1 int,
id2 int
); \\\\

Drop table IF EXISTS Likes; \\\\
create table Likes (
id1 int,
id2 int
); \\\\

insert into Highschooler
values(1, \'Gabriel\', 9),
(2, \'Grabriel2\', 10),
(3, \'Grabriel3\', 11),
(4, \'Grabriel4\', 12),
(5, \'Grabriel5\', 12);
\\\\
insert into Friend
values
(4, 2),(2,4),
(3, 1),(1,3),
(3, 2),(2,3),
(4, 1),(1,4);

\\\\
insert into likes
values(1, 2),
(3, 2),(2,4),
(3, 1),(3,4),
(4, 2),
(4, 1);

\\\\

select name from Highschooler where id in (
select id2 from Friend where id1 in (
select id from Highschooler where name=\'Gabriel\'
)
)
\\\\
select name, grade from Highschooler where id not in(
select distinct id1 from likes
union all
select distinct id2 from likes
)
\\\\
select hl.name, hl.grade from Highschooler hl, likes l where l.id2 = hl.id group by l.id2 having count(l.id1) > 1
\\\\
select a.name, a.grade, b.name, b.grade from Highschooler a, Highschooler b, likes l where a.id=l.id1 and b.id=l.id2 and a.grade >= (b.grade + 2)



And the results comes to be like below: (for queries in sequence):

   name
1 Grabriel3
2 Grabriel4
Students at your hometown high school have decided to organize their social network using databases. So far, they have collected information about sixteen stude
Students at your hometown high school have decided to organize their social network using databases. So far, they have collected information about sixteen stude

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site