Advertisement
snovvblind

CS145 - Stanford - SQL Social-Network Modification Exercises

Nov 6th, 2011
2,807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. DATABASE: http://www.db-class.org/course/resources/index?page=socialdata
  2.  
  3. 1. It's time for the seniors to graduate. Remove all 12th graders from Highschooler.
  4.  
  5. DELETE FROM Highschooler
  6. WHERE Grade = '12'
  7.  
  8. 2. If two students A and B are friends, and A likes B but not vice-versa, remove the Likes tuple.
  9.  
  10. DELETE FROM Likes
  11. WHERE ID1 IN (SELECT L.ID1
  12. FROM Friend F,Likes L
  13. WHERE F.ID1 = L.ID1 AND F.ID2 = L.ID2
  14. AND F.ID1 NOT IN (SELECT ID2 FROM Likes
  15. WHERE ID1=L.ID2 AND ID2=L.ID1))
  16.  
  17. PART II BELOW:
  18.  
  19. 1. Find the names of all students who are friends with someone named Gabriel.
  20.  
  21. Select H1.Name
  22. From Highschooler H1, Highschooler H2, Friend
  23. Where H1.ID = Friend.ID1 AND H2.name= 'Gabriel' AND H2.ID = Friend.ID2
  24.  
  25. 2. 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.
  26.  
  27. SELECT H1.Name, H1.Grade, H2.Name, H2.Grade
  28. FROM Likes, Highschooler H1, Highschooler H2
  29. WHERE H1.ID = Likes.ID1 AND H2.ID = Likes.ID2 AND H1.grade - H2.grade >=2
  30.  
  31. 3. 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.
  32.  
  33. Select h1name,h1grade,h2name,h2grade
  34.  
  35. from (
  36. Select h1.name as h1name,h1.grade as h1grade,h2.name as h2name,h2.grade as h2grade
  37. From Likes L1, Highschooler h1,Highschooler h2
  38. Where h1.ID = L1.ID1
  39. AND h2.ID = L1.ID2
  40. AND L1.ID1 in (Select ID2 From Likes Where ID2 = L1.ID1 AND ID1 = L1.ID2)
  41. )
  42. where h1name < h2name
  43.  
  44. 4. 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.
  45.  
  46. Select name,grade
  47.  
  48. From Highschooler h1
  49. Where h1.ID not in (Select ID1 from Likes )
  50. AND h1.ID not in (Select ID2 from Likes)
  51.  
  52. 5. 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.
  53.  
  54. Select h1.name,h1.grade,h2.name,h2.grade
  55. From Highschooler h1,Highschooler h2,Likes
  56. Where h1.ID = Likes.ID1
  57. AND h2.ID = Likes.ID2
  58. AND h2.ID not in (Select ID1 from Likes )
  59.  
  60. 6. 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.
  61.  
  62. Select distinct h1.name,h1.grade
  63. From Highschooler h1,Highschooler h2,Friend
  64. Where h1.ID = Friend.ID1
  65. AND h2.ID = Friend.ID2
  66. AND h1.grade = h2.grade
  67.  
  68. EXCEPT
  69. Select distinct h1.name,h1.grade
  70. From Highschooler h1,Highschooler h2,Friend
  71. Where h1.ID = Friend.ID1
  72. AND h2.ID = Friend.ID2
  73. AND h1.grade <> h2.grade
  74. order by h1.grade,h1.name
  75.  
  76. 7. 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.
  77.  
  78. Select h1.name,h1.grade,h2.name,h2.grade,h3.name,h3.grade
  79. From Highschooler h1,Highschooler h2,Highschooler h3,Likes L
  80. Where h1.ID = L.ID1 AND h2.ID = L.ID2
  81. and
  82. L.ID1 not in (Select ID1 FROM Friend where (ID1=h1.ID and ID2=h2.ID) OR (ID1=h2.ID and ID2=h1.ID) )
  83. AND h3.ID in
  84. (
  85. Select F1.ID2 from Friend F1,Friend F2
  86. where
  87. ((F1.ID1=h1.ID AND F1.ID2 = h3.ID)
  88. OR (F1.ID1=h3.ID AND F1.ID2 = h1.ID ))
  89. AND
  90. ((F2.ID1=h2.ID AND F2.ID2 = h3.ID )
  91. OR (F2.ID1=h3.ID AND F2.ID2 = h2.ID))
  92. )
  93.  
  94. 8. Find the difference between the number of students in the school and the number of different first names.
  95.  
  96. select count(ID) - count(distinct name)
  97. from Highschooler
  98.  
  99. 9. Find the name and grade of all students who are liked by more than one other student.
  100.  
  101. Select h1.name,h1.grade
  102. From Highschooler h1,Likes
  103. Where h1.ID = Likes.ID2
  104. group by Likes.ID2
  105. having count(Likes.ID1) >1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement