Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.66 KB | None | 0 0
  1. --1 For each student give the number of subjects in which he or she got grades
  2. /*
  3. select st.STUDENT_NAME, count (distinct g.SUBJECT_ID)
  4. from students st, grades g
  5. where st.STUDENT_ID = g.STUDENT_ID
  6. group by st.STUDENT_NAME, st.STUDENT_ID
  7. */
  8. --2 a) Give the ids and the names of projects which are undertaken by at most three employees.
  9. /*
  10. select p.PROJECT_ID, p.PROJECT_NAME
  11. from projects p, employees e, allocations a
  12. where a.EMPLOYEE_ID = e.EMPLOYEE_ID and a.PROJECT_ID = p.PROJECT_ID
  13. group by p.PROJECT_ID, p.PROJECT_NAME
  14. having  count(distinct a.EMPLOYEE_ID) <= 3
  15. */
  16. --2 b)  Give the names of projects which are undertaken by the least number of employees
  17. /*
  18. select p.PROJECT_NAME
  19. from projects p, employees e, allocations a
  20. where a.EMPLOYEE_ID = e.EMPLOYEE_ID and a.PROJECT_ID = p.PROJECT_ID
  21. group by p.PROJECT_ID, p.PROJECT_NAME
  22. having  count(distinct a.EMPLOYEE_ID) <= all (
  23.     select count(distinct a.EMPLOYEE_ID)
  24.     from projects p, employees e, allocations a
  25.     where a.EMPLOYEE_ID = e.EMPLOYEE_ID and a.PROJECT_ID = p.PROJECT_ID
  26. )
  27. */
  28. --3 a) Give the ids and the names of male students, who are older than at least one employee from SOFTWARE team.
  29. /*
  30. select s.STUDENT_ID, s.STUDENT_NAME
  31. from students s
  32. where s.GENDER = 'M' and s.DATE_OF_BIRTH < (
  33.   select max(e.DATE_OF_BIRTH)
  34.   from employees e, teams t
  35.   where e.TEAM_ID = t.TEAM_ID and t.TEAM_NAME = 'SOFTWARE'
  36. )
  37. */
  38. --3 b)Give the ids and the names of COMPUTER SCIENCE students, who got grades higher than every ROBOTICS student in DATABASES subject.
  39. /*
  40. select s.STUDENT_ID, s.STUDENT_NAME
  41. from students s, grades g
  42. where s.STUDENT_ID = g.STUDENT_ID and s.MAJOR_ID = 3 and (g.SUBJECT_ID = 1 or g.SUBJECT_ID = 2) and g.GRADE > (
  43.     select max(g.GRADE)
  44.     from grades g, students s
  45.     where g.STUDENT_ID = s.STUDENT_ID and s.MAJOR_ID = 6 and (g.SUBJECT_ID = 1 or g.SUBJECT_ID = 2)
  46. )
  47. */
  48. --3 c) Check if each value of foreign key manager id from PROJECTS table matches to record from ALLOCATIONS table.
  49. /*
  50. select count( distinct p.PROJECT_ID) = 45
  51. from projects p, allocations a
  52. where p.MANAGER_ID = a.EMPLOYEE_ID and a.POSITION_ID = 'MNG'
  53. */
  54. --4 a) Give the ids and the names of students who haven’t got any grade yet.
  55. /*
  56. select s.STUDENT_ID, s.STUDENT_NAME
  57. from students s
  58. where s.STUDENT_ID not in  (select g.STUDENT_ID
  59. from grades g)
  60. */
  61. --4 b) Give the names of employees who haven’t been assigned to any projects but received payment(s)
  62. /*
  63. --select e.EMP_NAME, e.EMPLOYEE_ID
  64. --from employees e, salaries s
  65. --where  e.EMPLOYEE_ID = s.EMPLOYEE_ID and e.EMPLOYEE_ID not in (
  66. --  select a.EMPLOYEE_ID
  67. --  from allocations a
  68. --  where a.PROJECT_ID = s.PROJECT_ID
  69. --)
  70. */
  71. --5  Give the ids, names of projects and the number of employees who are assigned to these projects.
  72. --Include also projects with no employees and pay attention to the possibility of getting money more
  73. --than once for the same project by employee.
  74. /*
  75. select p.PROJECT_ID, p.PROJECT_NAME,  count(distinct a.EMPLOYEE_ID)
  76. from allocations a, projects p
  77. where a.PROJECT_ID = p.PROJECT_ID
  78. group by p.PROJECT_ID, p.PROJECT_NAME
  79. */
  80. --6. a)Create the view with the ids, names of projects and the total income from these projects. Give
  81. --the reason why you can (or can’t) update the data in this view. On successful completion of this task, remove created view.
  82. /*
  83. create view myView as
  84. select p.project_id, project_name, sum(amount)
  85. from projects p join salaries s on P.PROJECT_ID=S.PROJECT_ID
  86. group by PROJECT_ID, PROJECT_name
  87. */
  88. --we couldnt update the view because i was connected with more than one table
  89.  
  90. --6. b)For each team give an average, minimum and maximum of employees’ earnings. Do it in two steps:
  91. /*
  92. create view myView1 as
  93. select t.TEAM_ID, t.TEAM_NAME, e.EMPLOYEE_ID, e.EMP_NAME, sum(amount) as sumSal
  94. from teams t
  95. join employees e on t.TEAM_ID=e.TEAM_ID
  96. join salaries s on e.EMPLOYEE_ID=s.EMPLOYEE_ID
  97. group by EMPLOYEE_ID, EMP_NAME,TEAM_ID,TEAM_NAME
  98.  
  99. create view myView2
  100. as select m.TEAM_ID, avg(m.sumSal), min(m.sumSal), max(m.sumSal)
  101. from myView1 m
  102. group by TEAM_ID
  103. */
  104. --7. For each team give the names of the youngest employees.
  105. /*
  106. select teams.TEAM_ID, teams.TEAM_NAME, employees.EMP_NAME
  107. from teams
  108. join employees on teams.TEAM_ID=employees.TEAM_ID
  109. where employees.DATE_OF_BIRTH = any
  110. (
  111.     select min(employees.DATE_OF_BIRTH)
  112.     from employees
  113.     join teams on employees.TEAM_ID = teams.TEAM_ID
  114.     group by teams.TEAM_ID
  115. )
  116. */
  117. --9 Give the names of employees who were undertaking all projects with ids in range 1-50
  118.  
  119. select e.EMP_NAME, p.PROJECT_ID
  120. from employees e
  121. join teams t on e.TEAM_ID=t.TEAM_ID
  122. join projects p on t.MANAGER_ID=p.MANAGER_ID
  123. where p.PROJECT_ID between 1 and 50
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement