Advertisement
Mary_99

lab 4 sql

Oct 23rd, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.42 KB | None | 0 0
  1. --Subqueries
  2. --• Get the first and last names of employees who work in the Research department.
  3.  
  4. /*
  5. SELECT emp_fname, emp_lname,dept_name
  6. FROM employee
  7. JOIN department
  8. ON employee.dept_no = department.dept_no
  9. WHERE dept_name = 'Research'
  10. */
  11.  
  12. --• Get full details of all employees whose department is located in Dallas.
  13.  
  14. /*
  15. SELECT  employee.* , department.location
  16. FROM employee
  17. JOIN
  18. department
  19. ON employee.dept_no = department.dept_no
  20. WHERE  location  = 'Dallas'
  21. */
  22. /*
  23. SELECT *
  24. FROM employee
  25. WHERE dept_no  IN
  26. (
  27. SELECT dept_no
  28. FROM department
  29.  
  30. WHERE  location  = 'Dallas'
  31. )*/
  32.  
  33. --• Get the last names of all employees who work on the project Apollo.
  34. /*
  35. SELECT *--emp_lname
  36. FROM employee
  37. JOIN works_on
  38. ON employee.emp_no = works_on.emp_no
  39. JOIN project
  40. ON project.project_no = works_on.project_no
  41. WHERE  project_name = 'Apollo'
  42. */
  43.  
  44.  
  45. --Havings.
  46. --• Get project numbers for all projects employing fewer than four persons.
  47. /*
  48. SELECT project_no
  49. FROM employee
  50. JOIN works_on
  51. ON employee.emp_no =  works_on.
  52. GROUP BY  project_no
  53. HAVING COUNT(project_no)<4
  54. */
  55.  
  56. --• Group rows of the works_on table by job and eliminate those jobs that do not begin with the letter M.
  57.  
  58. /*
  59. SELECT job
  60. FROM works_on
  61. GROUP BY job
  62. HAVING  job NOT LIKE 'm%'
  63. */
  64.  
  65. --Order by
  66. --• Get department numbers and employee names for employees with employee numbers < 20000, in ascending order of last and first names.
  67. /*
  68. SELECT dept_no ,emp_fname ,emp_lname, emp_no
  69. FROM  employee
  70. WHERE emp_no < 20000
  71. ORDER BY emp_lname , emp_fname ASC
  72. */
  73.  
  74.  
  75. --• For each project number, get the project number and the number of all employees, in descending order of the employee number.
  76. /*
  77.  SELECT project_no, COUNT(emp_no)
  78.  FROM  works_on
  79.  GROUP BY project_no
  80.  ORDER BY  COUNT(emp_no) DESC
  81. */
  82. --Union
  83. -- Get the employee number for employees who either belong to department d1 or entered their project before 1/1/2007,
  84. --in ascending order of employee number.
  85.  
  86.  
  87. /*
  88. SELECT emp_no FROM employee
  89. WHERE dept_no = 'd1'
  90. UNION
  91. SELECT emp_no  FROM works_on
  92. WHERE enter_date < '01-01-2007'
  93. ORDER BY emp_no
  94. */
  95.  
  96. -- Get the employee number for employees who either belong to department d1 didn't
  97. --enter their project before 1/1/2007, in ascending order of employee number.
  98. /*
  99. SELECT emp_no  FROM employee
  100. WHERE dept_no = 'd1'
  101. EXCEPT
  102. SELECT emp_no   FROM works_on
  103. WHERE enter_date < '01-01-2007'
  104. ORDER BY  emp_no ASC
  105. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement