Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. select distinct deptname
  2. from Dept d,(select deptno from proj
  3. where prstdate< CURDATE())p
  4. where d.deptno=p.deptno;
  5. ----------------------
  6. Find the name, start date and duration in
  7. months of projects that have the earliest end
  8. date
  9.  
  10. select projname,prstdate,
  11. TIMESTAMPDIFF(MONTH,prstdate,prenddate) AS duration
  12. FROM Proj
  13. Where prendate=(SELECT MIN(prendate) FROM Proj);
  14. ----------------------------------------------
  15. Produce a list of all female employees whose education level is higher than the
  16. average education level of all employees in their departments. Display employee
  17. number, first name and last name
  18.  
  19. select empno,firstname,lastname
  20. FROM Emp e1
  21. where SEX= 'F' AND edlevel >
  22. (SELECT AVG(edlevel) from EMP where workdept=e1.workdept);
  23. ---------------------------------------------------------
  24. Find the lastname, job and salary of employees who do not work on any project
  25.  
  26. select lastname,job,salary
  27. FROM EMP
  28. where empno <> ALL(SELECT empno from Pworks);
  29. ---------------------------------------------------------
  30. List all employees who have a higher education level than all designers. Display the
  31. first name, last name and edlevel of the employee. Assume that designers are
  32. indicated by job = ‘Designer’.
  33.  
  34. select fristname,lastname,edlevel
  35. FROM Emp
  36. Where edlevel > ALL (SELECT edlevel FROM emp where JOB='Designer');
  37. ---------------------------------------------------------
  38. Get the names of departments that are currently responsible for only one project each.
  39.  
  40. select deptname
  41. from Proj,Dept
  42. where Proj.deptno=Dept.deptno
  43. GROUP BY deptname
  44. HAVING COUNT(deptname)=1;
  45.  
  46. select deptname
  47. from Proj JOIN dept ON Proj.deptno=Dept.deptno
  48. GROUP BY deptname
  49. HAVING COUNT(deptname)=1;
  50. -------------------------------------------
  51. In the PROJ table, a row with a null value in the MAJPROJ column indicates that the
  52. project represented by that row is not a sub-project of any other. Display the project
  53. number, name and end date of all projects along with a remark of ‘Sub-project’ if the
  54. MAJPROJ value is not null and ‘Not a sub-project’ otherwise. Order the result by
  55. project end date
  56. ---------------------------------
  57. (select projno,projname,prendate, 'Sub-Project' AS remark
  58. from proj
  59. where majproj is NOT NULL)
  60. UNION
  61. (select projno,projname,prendate, 'Not a sub-Project' AS remark
  62. from proj
  63. where majproj is NULL)
  64. Order by prendate;
  65. The select_expression should select the appropriate table, and the results are stored in
  66. Emp2, instead of being displayed and discarded.
  67. The employee name in Emp2 should be in the format <lastname>,<first initial>. For
  68. example, Eileen Henderson becomes Henderson,E. Note also that the table contains
  69. the age of the employee, not the birthdate, so you will need to calculate that.
  70.  
  71. ----------------------------------------------
  72.  
  73. CREATE TABLE Emp2
  74. SELECT empno AS eid,
  75. CONCAT(lastname,',',substr(firstname,1,1)) AS Ename,
  76. TIMESTAMPDIFF(year,birthdate,CURDATE()) AS age,sex,salary FROM emp;
  77. ------------------------------------------------
  78. ALTER TABLE Emp2 ADD PRIMARY KEY(eid);
  79. -----------------------------------------------
  80. Insert data into Dept2 from the Emp and Dept tables of Practical 2. Take the budget
  81. of each department as 20% more than the total salary of all employees in the
  82. department. Use an insert statement of the same form as above. Note the renaming of
  83. some attributes.
  84. --------------------------------------------------
  85. INSERT INTO Dept2
  86. SELECT deptno AS did,deptname AS dname, SUM(salary)*1.2 AS budget,mgrno AS managerid FROM Dept,EMP
  87. WHERE deptno=workdept
  88. GROUP BY did,dname,managerid;
  89. ------------------------------------------------
  90. Use a Create table statement with a subquery to create the table Works2, assuming
  91. that the percentage of time each employee works in his/her workdept is 100.
  92. works table eid,did,pct_time
  93. ------------------------------------
  94. CREATE TABLE Works2
  95. select empno AS eid, workdept AS did, 100 as pct_time
  96. FROM emp;
  97. -----------------------------------------------------
  98.  
  99. Add an attribute called since of TIMESTAMP type to Works2, using an ALTER
  100. TABLE statement. Give CURRENT_TIMESTAMP as the default value for this
  101. attribute. Display the rows of the table to check the changes.
  102. Hint: Look up the TIMESTAMPE type and default values.
  103.  
  104. ALTER TABLE works2 ADD since TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
  105. -------------------------------------------------
  106. Delete all managers of departments from the Works2 table
  107.  
  108.  
  109. select * FROM works2
  110. where eid IN (select managerid from dept2)
  111. ----------------------------------------------
  112. Update the since column of Works2 with the hiredate of each employee from the
  113. EMP table. In the update statement of the form: UPDATE tablename AS corr_var
  114. SET columnname = expression, use a correlated subquery as the expression to assign
  115. the corresponding hiredate of Emp table to each Works2 tuple.
  116. -------------------------------------------
  117.  
  118. UPDATE works2 AS w
  119. set since =(
  120. select hiredate from emp
  121. where w.eid=empno);
  122.  
  123. -----------------------------------------
  124. ALTER TABLE WORKs2 ADD PRIMARY KEY (eid,did);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement