Advertisement
didito33

1st one

May 14th, 2022
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.21 KB | None | 0 0
  1. CREATE VIEW coachInfo(Coach, Sport, Day)
  2. AS
  3. Select coaches.name, sports.name, sportGroups.dayOfWeek
  4. from coaches join sportGroups
  5. on coaches.id = sportGroups.coach_id
  6. join sports on sports.id = sportGroups.sport_id;
  7.  
  8.  
  9.  
  10. select * from coachInfo;
  11.  
  12. Drop View coachInfo;
  13.  
  14. -- 1. Изведете имената, класовете и телефоните на всички ученици, които тренират футбол.
  15.  
  16. SELECT students.name, students.class, students.phone, sports.name
  17. FROM students
  18. JOIN student_sport
  19. on students.id = student_sport.student_id
  20. JOIN sportGroups
  21. ON student_sport.sportGroup_id = sportGroups.id
  22. JOIN sports
  23. ON sportGroups.sport_id = sports.id
  24. AND sports.name = "Football";
  25.  
  26. -- 2. Изведете имената на всички треньори по волейбол.
  27.  
  28. SELECT coaches.name, sports.name
  29. FROM coaches
  30. JOIN sportGroups
  31. ON coaches.id = sportGroups.coach_id
  32. JOIN sports
  33. ON sportGroups.sport_id = sports.id
  34. AND sports.name = "Volleyball";
  35.  
  36. -- 3. Изведете името на треньора и спорта, който тренира ученик с име Илиян Иванов.
  37.  
  38. SELECT distinct coaches.name, sports.name, students.name
  39. FROM coaches
  40. JOIN sportGroups
  41. ON coaches.id = sportGroups.coach_id
  42. JOIN sports
  43. ON sports.id = sportGroups.sport_id
  44. JOIN student_sport
  45. ON student_sport.sportGroup_id = sportGroups.id
  46. JOIN students
  47. ON students.id = student_sport.student_id
  48. AND students.name = "Iliyan Ivanov";
  49.  
  50. -- 4. Изведете сумите от платените през годините такси на учениците по месеци, но само за
  51. -- ученици с такси по месеци над 700 лева и с треньор с ЕГН 7509041245.
  52.  
  53. select students.name, SUM(taxesPayments.paymentAmount) as sumPayment, taxesPayments.year
  54. from taxesPayments
  55. JOIN students
  56. on taxesPayments.student_id = students.id
  57. JOIN sportGroups
  58. on sportGroups.id = taxesPayments.group_id
  59. join coaches
  60. on coaches.id = sportGroups.coach_id
  61. and coaches.egn = '7509041245'
  62. group by taxesPayments.student_id, taxesPayments.year
  63. having SUM(paymentAmount) > 700;
  64.  
  65. -- 5. Изведете броя на студентите във всяка една от групите.
  66.  
  67. SELECT count(students.id) as numberOfStudents, sportGroups.id as Group_ID
  68. from students
  69. join student_sport
  70. on student_sport.student_id = students.id
  71. right JOIN sportGroups
  72. ON student_sport.sportGroup_id = sportGroups.id
  73. group by sportGroups.id;
  74.  
  75. -- 6. Определете двойки ученици на базата на спортната група, в която тренират, като
  76. -- двойките не се повтарят. В двойките да участват само ученици, трениращи футбол.
  77. -- Учениците от една двойка трябва да тренират в една и съща група.
  78. SELECT firstpl.name as firstPlayer, secondpl.name as secondPlayer, sports.name as sportName
  79. FROM students as firstpl JOIN students as secondpl
  80. ON firstpl.id > secondpl.id
  81. JOIN sports ON (
  82. secondpl.id IN(
  83. SELECT student_id
  84. FROM student_sport
  85. WHERE sportGroup_id IN(
  86. SELECT id
  87. FROM sportgroups
  88. WHERE sport_id = sports.id
  89. )
  90. AND sports.name = 'Football'
  91. )
  92. AND (firstPl.id IN
  93. ( SELECT student_id
  94. FROM student_sport
  95. WHERE sportGroup_id IN(
  96. SELECT id
  97. FROM sportgroups
  98. WHERE sport_id = sports.id)
  99. )
  100. )
  101. )
  102. WHERE firstPL.id IN(
  103. SELECT student_id
  104. FROM student_sport
  105. WHERE sportGroup_id IN(
  106. SELECT sportGroup_id
  107. FROM student_sport
  108. WHERE student_id = secondPl.id
  109. )
  110. )
  111. ORDER BY firstPlayer;
  112.  
  113.  
  114. -- 7. Изведете имената на учениците, класовете им, местата на тренировки и името на
  115. -- треньорите за тези ученици, чийто тренировки започват в 8.00 часа. Създайте виртуална
  116. -- таблица с този селект.
  117.  
  118. CREATE VIEW `Virtual_Table` AS
  119. SELECT students.name as Student_Name, students.class, sportGroups.location, coaches.name as Coach_Name, sportGroups.hourOfTraining
  120. from students
  121. JOIN student_sport
  122. on students.id = student_sport.student_id
  123. JOIN sportGroups
  124. ON student_sport.sportGroup_id = sportGroups.id
  125. AND sportGroups.hourOfTraining = '08:00:00'
  126. JOIN coaches
  127. on coaches.id = sportGroups.coach_id;
  128.  
  129. select * from Virtual_Table;
  130.  
  131. -- 8. Използвайте базата данни transaction_test.
  132. -- Създайте трансакция, с която прехвърляте 50000 лв. от сметката в лева на Stoyan Pavlov Pavlov в сметката в лева на Ivan Petrov Iordanov
  133. -- при подадени само име на титуляр и вид валута.
  134.  
  135. BEGIN;
  136. update customer_accounts
  137. set amount = amount - 50000
  138. where id = 3 and customer_accounts.currency = "BGN";
  139.  
  140. update customer_accounts
  141. set amount = amount + 50000
  142. where id = 1 and customer_accounts.currency = "BGN";
  143. COMMIT;
  144.  
  145. select * from customer_accounts;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement