Advertisement
Guest User

Untitled

a guest
Jul 18th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 6.46 KB | None | 0 0
  1. 1. Pokaż identyfikatory i nazwy przedmiotów prowadzonych na 1. roku studiów. Uporządkuj
  2. alfabetycznie według nazw przedmiotów.
  3.  
  4. select cno, cname
  5. from courses   
  6. where studyear=1
  7. order by cname
  8.  
  9. 2. Ilu studentów ma nazwiska rozpoczynające się na literę A?
  10.  
  11. select count(*)
  12. from students
  13. where sname like "A%"
  14.  
  15. 3. Dla każdego miasta podaj liczbę studentów, którzy pochodzą z tego miasta.
  16.  
  17. select city, count(*)
  18. from students
  19. group by city
  20.  
  21. 4. Dla każdego nauczyciela (identyfikator, tytuł i nazwisko) podaj przedmioty (identyfikatory i
  22. nazwy), które prowadzi.
  23.  
  24. select distinct teachers.tno, title, tname, cno, cname
  25. from tsc,teachers,courses
  26. where tsc.tno=teachers.tno and tsc.cno=teachers.cno
  27.  
  28. 5. Dla każdej pary (ocena, rok studiów) podaj liczbę wystawionych takich ocen.
  29.  
  30. select grade,studyear,count(grade)
  31. from courses,tsc
  32. where tsc.cno=courses.cno
  33. group by grade,studyear
  34. order by 1
  35.  
  36. 6. Pokaż identyfikatory tych nauczycieli. którzy nie prowadzą zajęć na 1. roku.
  37.  
  38. select tno
  39. from teachers
  40. where tno not in (select tsc.tno
  41.                   from tsc, courses
  42.                   where tsc.cno=courses.cno and tsc.tno=teachers.tno and courses.studyear=1)
  43.  
  44. 7. Na którym roku studiów jest najwięcej przedmiotów?
  45.  
  46. select top 1 studyear,count(*)
  47. from courses
  48. group by studyear
  49. order by 2
  50.  
  51. 8. Na którym roku jest największa średnia ocen?
  52.  
  53. select top 1 studyear,avg(grade)
  54. from courses, tsc
  55. where tsc.cno=courses.cno
  56. group by studyear
  57. order by 2 desc
  58.  
  59. 9. Ilu nauczycieli nie miało w ogóle zajęć?
  60.  
  61. select count(*)
  62. from teachers
  63. where tno not in (select tno from tsc)
  64.  
  65. 10. Dla każdego roku studiów pokaż sumę godzin zajęć prowadzonych na tym roku dla
  66. studentów. Wynik uporządkuj według lat studiów.
  67.  
  68. select studyear, sum(hours)
  69. from tsc,courses
  70. where tsc.cno=courses.cno
  71. group by studyear
  72. order by 2
  73.  
  74. 11. Który student (identyfikator i nazwisko) ma największą średnią ocen?
  75.  
  76. select top 1 students.sno, sname ,AVG(grade)
  77. from tsc,students
  78. where tsc.sno=students.sno
  79. group by students.sno,sname
  80. order by 2 desc
  81.  
  82. 12. Pokaż listę wszystkich studentów (identyfikatory i nazwiska) uporządkowaną według
  83. malejących średnich ocen.
  84.  
  85. select students.sno, sname ,AVG(grade)
  86. from tsc,students
  87. where tsc.sno=students.sno
  88. group by students.sno,sname
  89. order by 3 desc
  90.  
  91. 13. Pokaż nauczyciela (identyfikator, tytuł, nazwisko), który ma najwięcej podwładnych.
  92.  
  93. select t1.tno, t1.title, t1.tname
  94. from teachers t1
  95. where t1.tno = (select top 1 supno
  96.                 from teachers
  97.                 group by supno
  98.                 order by COUNT(*) desc)
  99.  
  100. 14. Pokaż nauczyciela (identyfikator, tytuł, nazwisko), który nie ma szefa.
  101.  
  102. select t1.tno, t1.title, t1.tname
  103. from teachers t1
  104. where supno is null
  105.  
  106. 15. Którzy nauczyciele (identyfikatory i nazwiska) prowadzą więcej niż 3 przedmioty i ile jest tych
  107. przedmiotów?
  108.  
  109. select tsc.tno,tname
  110. from tsc,teachers
  111. where teachers.tno=tsc.tno
  112. group by tsc.tno,tname
  113. having COUNT(*)>3  
  114.  
  115. 16. Podaj identyfikatory i nazwiska studentów, którzy otrzymali oceny 5.0 z jakiegokolwiek
  116. przedmiotu.
  117.  
  118. select distinct students.sno,sname
  119. from students,tsc
  120. where students.sno=tsc.sno and grade=5
  121.  
  122. 17. Podaj identyfikatory i nazwiska studentów, którzy z jakiegokolwiek przedmiotu otrzymali ocenę
  123. równą ocenie maksymalnej otrzymanej przez studentów. Następnie pokaż, ilu jest takich
  124. studentów.
  125.  
  126. select distinct students.sno,sname
  127. from students,tsc
  128. where students.sno=tsc.sno and grade=(select top 1 grade from tsc order by grade desc)
  129.  
  130. select COUNT(*)
  131. from tsc
  132. where grade=(select top 1 grade from tsc order by grade desc)
  133.  
  134. 18. Ilu studentów uczyło się matematyki na drugim roku studiów?
  135.  
  136. select COUNT(*)
  137. from tsc,courses
  138. where cname='Mathematics' and studyear=2 and tsc.cno=courses.cno
  139.  
  140. 19. Utwórz widok zawierający wszystkie informacje z tablicy TSC rozszerzone i nazwiska
  141. studentów, tytuły i nazwiska nauczycieli oraz nazwy przedmiotów.
  142.  
  143. create view tscex as
  144. select tsc.*, sname, title, tname, cname
  145. from tsc,courses,students,teachers
  146. where tsc.cno=courses.cno and tsc.sno=students.sno and tsc.tno=teachers.tno
  147.  
  148. 20. Dla każdego miasta podaj łączną liczbę studentów i nauczycieli, którzy pochodzą z tego
  149. miasta.
  150.  
  151. select city,COUNT(city)
  152. from (select city from teachers
  153.       union all
  154.       select city from students) cs
  155. group by city
  156.  
  157. 21. Pokaż nauczyciela (identyfikator i nazwisko), który postawił studentom najgorsze oceny (tzn.
  158. wystawiona przezeń średnia ocen jest najniższa).
  159.  
  160. select teachers.tno,tname
  161. from tsc,teachers
  162. where teachers.tno=tsc.tno
  163. group by teachers.tno,tname
  164. having AVG(grade)= (select top 1 AVG(t2.grade)
  165.                     from tsc t2
  166.                     group by t2.tno
  167.                     order by 1 asc)
  168.  
  169. 22. Utwórz archiwum dla tablicy TSC. Archiwum ma zawierać: identyfikatory i nazwiska studentów
  170. i nauczycieli, identyfikatory i nazwy przedmiotów oraz oceny. Następnie przepisz do tego
  171. archiwum dane z tablicy TSC, po czym usuń dane z TSC.
  172.  
  173. drop tscold
  174.  
  175. select tsc.sno ,sname, tsc.tno, tname, tsc.cno, cname, grade
  176. into tscold
  177. from tsc,courses,students,teachers
  178. where tsc.cno=courses.cno and tsc.sno=students.sno and tsc.tno=teachers.tno
  179.  
  180. delete from tsc
  181.  
  182. select * from tscold
  183.  
  184. 23. Dla każdego przedmiotu oblicz średnią ocen. Następnie pokaż te przedmioty (identyfikatory i
  185. nazwy), z których średnia ocena przekracza średnią z tych średnich.
  186.  
  187. create view t_avg as
  188. select tsc.cno,cname,AVG(grade) sr
  189. from tsc,courses
  190. where tsc.cno=courses.cno
  191. group by tsc.cno, cname
  192.  
  193. select * from t_avg
  194.  
  195. select cno,cname
  196. from t_avg
  197. where sr>(select AVG(sr) from t_avg)
  198.  
  199. drop view t_avg
  200.  
  201. 24. Pokaż tych nauczycieli (identyfikatory, tytuły i nazwiska), którzy na pierwszym roku studiów
  202. uczyli tych studentów, którzy pochodzą z tego samego miasta co oni.
  203.  
  204. select tsc.tno,title,tname
  205. from teachers,tsc,students,courses
  206. where tsc.tno=teachers.tno and tsc.sno=students.sno and tsc.cno=courses.cno
  207.       and studyear=1 and teachers.city=students.city
  208.  
  209. 25. Pokaż przedmiot (identyfikator, nazwa), na który uczęszczało najmniej studentów.
  210.  
  211. select top 1 tsc.cno,cname
  212. from tsc,courses
  213. where tsc.cno=courses.cno
  214. group by tsc.cno,cname
  215. order by COUNT(sno) asc
  216.  
  217. 26. Dla każdego roku rozpoczęcia studiów podaj średnią ocen studentów, którzy rozpoczęli studia
  218. w tym roku. Wynik uporządkuj malejącą według średnich ocen.
  219.  
  220. select syear,AVG(grade)
  221. from students, tsc,courses
  222. where tsc.sno=students.sno and tsc.cno=courses.cno
  223. group by syear
  224. order by 2 desc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement