Advertisement
VandanRogheliya

DBMS Prac7

Sep 28th, 2020
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 4.67 KB | None | 0 0
  1. CREATE DATABASE prac7
  2.  
  3. CREATE TABLE PROFESSOR (SSN INT PRIMARY KEY, name VARCHAR(20), age INT, research VARCHAR(20), rnk VARCHAR(20))
  4.  
  5. CREATE TABLE PROJECT (p_no INT PRIMARY KEY, spon_no INT, starting DATE, ending DATE, budget INT, prin_inv INT FOREIGN KEY REFERENCES PROFESSOR)
  6.  
  7. CREATE TABLE DEPARTMENT (dept_no INT PRIMARY KEY, dname VARCHAR(20), main_office VARCHAR(20), chairman INT FOREIGN KEY REFERENCES PROFESSOR)
  8.  
  9. CREATE TABLE PROF_WORKS_ON_PROJ (prof INT FOREIGN KEY REFERENCES PROFESSOR, proj INT FOREIGN KEY REFERENCES PROJECT, CONSTRAINT pwppk PRIMARY KEY (prof, proj))
  10.  
  11. CREATE TABLE prec_time_given (prof INT FOREIGN KEY REFERENCES PROFESSOR, dept_no INT FOREIGN KEY REFERENCES DEPARTMENT, time_per INT, CONSTRAINT ptgpk PRIMARY KEY (prof, dept_no) )
  12.  
  13. CREATE TABLE STUDENT (SSN INT PRIMARY KEY, name VARCHAR(20), age INT, deg VARCHAR(20), majors_in INT FOREIGN KEY REFERENCES DEPARTMENT)
  14.  
  15. CREATE TABLE STU_PROJ_PROF (stu INT FOREIGN KEY REFERENCES STUDENT, proj INT FOREIGN KEY REFERENCES PROJECT, prof INT FOREIGN KEY REFERENCES PROFESSOR, CONSTRAINT spppk PRIMARY KEY(stu, proj))
  16.  
  17.  
  18. -- Questions. I had some doubts while completing it, there can be few mistakes.
  19. -- 1.    Details of faculty who is specialized in ML
  20. SELECT * FROM PROFESSOR p WHERE research = 'ML'
  21.  
  22. -- 2.    Details of the project with budget more than 2 crores
  23. SELECT * FROM PROJECT p WHERE budget > 15000
  24.  
  25. -- 3.    Details of the professors who are managing the project alpha
  26. (SELECT * FROM PROFESSOR p WHERE SSN IN (SELECT prof FROM PROF_WORKS_ON_PROJ pwop WHERE p_no = (SELECT p_no FROM PROJECT p2 WHERE pname = 'Alpha'))
  27.  
  28.  
  29. -- 4.    Details of the professors who are working with alpha project.
  30. SELECT * FROM PROFESSOR p WHERE SSN IN (SELECT prof FROM PROF_WORKS_ON_PROJ pwop WHERE p_no = (SELECT p_no FROM PROJECT p2 WHERE pname = 'Alpha'))
  31.  
  32. -- 5.    Details of the faculty who is managing more than 1 project.
  33. SELECT * FROM PROFESSOR p WHERE SSN IN (SELECT prin_inv FROM PROJECT p2 GROUP BY prin_inv HAVING COUNT(*) > 1 )
  34.  
  35. -- 6.    Details of the faculty who is not managing more than 1 project.
  36. SELECT * FROM PROFESSOR p WHERE SSN IN (SELECT prin_inv FROM PROJECT p2 GROUP BY prin_inv HAVING COUNT(*) = 1)
  37. UNION
  38. SELECT * FROM PROFESSOR p WHERE SSN NOT IN (SELECT prin_inv FROM PROJECT p2 GROUP BY prin_inv)
  39.  
  40. -- 7.    Details of the student  who is working on beta project.
  41. SELECT * FROM STUDENT s WHERE SSN = (SELECT stu FROM STU_PROJ_PROF spp WHERE p_no = (SELECT p_no FROM PROJECT p WHERE pname = 'Beta'))
  42.  
  43. -- 8.    Display projectno ,student name and professor(supervisor) of all the projects.
  44. SELECT s.name, p2.name, spp.p_no FROM STU_PROJ_PROF spp, STUDENT s, PROFESSOR p2 WHERE s.SSN = spp.stu AND p2.SSN = spp.prof
  45.  
  46. -- 9.    Details of the students  who are working in  atleast one project.
  47. SELECT * FROM STUDENT s WHERE SSN IN (SELECT stu FROM STU_PROJ_PROF spp)
  48.  
  49. -- 10.    Display professor_no , dept name  and percentage of time allotted to each department.
  50. SELECT ptg.prof, d.dname ,ptg.time_per FROM per_time_given ptg, DEPARTMENT d WHERE ptg.dept_no = d.dept_no
  51.  
  52. -- 11.    Details of the students  who are not associated with any project.
  53. SELECT * FROM STUDENT s WHERE SSN NOT IN (SELECT stu FROM STU_PROJ_PROF spp)
  54.  
  55. -- 12.    Display projectno, no of professors working on each project.
  56. SELECT p_no, COUNT(*) FROM PROF_WORKS_ON_PROJ pwop GROUP BY p_no
  57.  
  58. -- 13.    Display the details of the faculty who is working on maximum number of projects.
  59. SELECT * FROM PROFESSOR p2 WHERE SSN IN (SELECT prof FROM PROF_WORKS_ON_PROJ pwop2 GROUP BY prof
  60.     HAVING COUNT(*) = (SELECT MAX(y.cnt) FROM ( SELECT prof, COUNT(*) 'cnt' FROM PROF_WORKS_ON_PROJ pwop
  61.         GROUP BY prof) AS y))
  62.  
  63. -- 14.    Display the name of the graduate  student  and  name of his  corresponding senior graduate student.
  64. SELECT s.name 'JUN', s2.name 'SEN' FROM STUDENT s, STUDENT s2 WHERE s.majors_in = s2.majors_in AND s.age < s2.age
  65.        
  66. -- 15.    Details of the project where minimum no of professors are working on it.
  67. SELECT * FROM PROJECT p WHERE p_no IN
  68.     (SELECT p_no FROM PROF_WORKS_ON_PROJ pwop GROUP BY p_no HAVING COUNT(*) =
  69.         (SELECT MAX(y.cnt) FROM (SELECT COUNT(*) 'cnt' FROM PROF_WORKS_ON_PROJ pwop2 GROUP BY p_no) AS y ))
  70.  
  71. -- 16.    Details of the project where no student is involved.
  72. SELECT * FROM PROJECT p WHERE p_no NOT IN (SELECT p_no FROM STU_PROJ_PROF spp)
  73.        
  74. -- 17.     Find the name of the youngest professor.
  75. SELECT name FROM PROFESSOR p WHERE age = (SELECT MIN(age) FROM PROFESSOR p2)
  76.  
  77. -- 18.    Display projectno , project guide  where budget is  between 10000 and 200000 and has commenced in the year 2016.
  78. SELECT p_no FROM PROJECT p WHERE budget BETWEEN 10000 AND 20000 AND YEAR(starting) = 2020
  79.  
  80.  
  81.  
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement