Sorry for the weirdly phrased question. I am very new to SQL and having difficulty. Here is the setup for the problem:
We provided a simple database for a social network, social.db. Here's the schema:
Student(ID int, name text, grade int) Friend(ID1 int, ID2 int) Likes(ID1 int, ID2 int) In the Student table, ID is a unique identifier for a particular student. name and grade correspond to the student's first name and grade.
In the Friend table, each (ID1, ID2) pair indicates that the student with ID1 is friends with the student with ID2 (and vice versa). The friendship is mutual, and if (ID1, ID2) is in the table, it is guaranteed that (ID2, ID1) exists in the table.
In the Likes table, each (ID1, ID2) pair indicates that the student with ID1 likes the student with ID2. The (ID1, ID2) pair in the table does not guarantee that the (ID2, ID1) pair also exists in the table.
The Question:
Write a SQL statement that returns the name and grade of all students who are liked by any student who is in a grade below them. Results should be ordered by name (A-Z), then grade (ascending). Save the query to question8.sql Hint: You can do this problem without using two SELECT statements, but doing so makes sure that all your answers are distinct. Also, future problems will require using two SELECT statements, so practicing here isn't a bad idea!
The code I have thus far is:
SELECT name, grade FROM Student INNER JOIN Likes ON Student.ID=Likes.ID2 where grade > (select grade from student inner join likes on student.id=id1);
But this seems to return student who are liked by someone in the minimum grade of the second select statement. How do i compare grade row by row? Am I at all moving in the right direction?
Thanks
Aucun commentaire:
Enregistrer un commentaire