BartekPogoda

BD4 home

Apr 5th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* 1.  Write a query to display the last name and hire date of any employee in the same
  2. department as Zlotkey. Exclude Zlotkey.  */
  3.  
  4. SELECT
  5.   last_name, hire_date
  6. FROM
  7.   employees
  8. WHERE
  9.   department_id = (
  10.                   SELECT
  11.                     department_id
  12.                   FROM
  13.                     employees
  14.                   WHERE
  15.                     last_name = 'Zlotkey'
  16.                   )
  17.   AND last_name <> 'Zlotkey';
  18.  
  19. /* 2. Create a query to display the employee numbers and last names of all employees who earn
  20. more than the
  21. average salary. Sort the results in ascending order of salary.  */
  22.  
  23. SELECT
  24.   employee_id, last_name
  25. FROM
  26.   employees
  27. WHERE
  28.   salary > (
  29.             SELECT
  30.               AVG(salary)
  31.             FROM
  32.               employees
  33.             )
  34. ORDER BY
  35.   salary ASC;
  36.  
  37. /* 3. Write a query that displays the employee numbers and last names of all employees who
  38. work in a department with any employee whose last name contains a “u”.  */
  39.          
  40. SELECT
  41.   employee_id, last_name
  42. FROM
  43.   employees
  44. WHERE
  45.   department_id IN (
  46.             SELECT
  47.               department_id
  48.             FROM
  49.               employees
  50.             WHERE
  51.               last_name LIKE '%u%'
  52.             );
  53.            
  54. /* 4. Display the last name, department number, and job ID of all employees whose department
  55. location ID is 1700. */
  56. -- it could be done w/o subquerry (join), same for some other querries
  57. SELECT
  58.   last_name, department_id, job_id
  59. FROM
  60.   employees
  61. WHERE
  62.   department_id IN (
  63.                   SELECT
  64.                     department_id
  65.                   FROM
  66.                     departments
  67.                   WHERE
  68.                     location_id = 1700
  69.                   );
  70.                  
  71. /* 5. Display the last name and salary of every employee whose manager is King.  */
  72.  
  73. SELECT
  74.   last_name, salary
  75. FROM
  76.   employees
  77. WHERE
  78.   manager_id IN (
  79.                 SELECT
  80.                   employee_id
  81.                 FROM
  82.                   employees
  83.                 WHERE
  84.                   last_name = 'King'
  85.                 );
  86.                  
  87. /* 6. Display the department number, last name, and job ID for every employee in the
  88. department named “Executive”.  */
  89.  
  90. SELECT
  91.   department_id, last_name, job_id
  92. FROM
  93.   employees
  94. WHERE
  95.   department_id = (
  96.                   SELECT
  97.                     department_id
  98.                   FROM
  99.                     departments
  100.                   WHERE
  101.                     department_name = 'Executive'
  102.                   );
  103.                  
  104. /* 7. Display all the employees who earn more than “Abel” */
  105.  
  106. SELECT
  107.   employee_id, last_name
  108. FROM
  109.   employees
  110. WHERE
  111.   salary > (
  112.             SELECT
  113.               salary
  114.             FROM
  115.               employees
  116.             WHERE
  117.               last_name = 'Abel'
  118.             );
  119.  
  120. /* 8 Display the employees whose job ID is the same as that of employee 141. */
  121.  
  122. SELECT
  123.   employee_id, last_name
  124. FROM
  125.   employees
  126. WHERE
  127.   job_id = (
  128.             SELECT
  129.               job_id
  130.             FROM
  131.               employees
  132.             WHERE
  133.               employee_id = 141
  134.             );
  135.  
  136.  
  137. /* 9. Display employee last name, job ID, and salary of all employees whose salary
  138. is equal to the minimum salary  */
  139.                  
  140. SELECT
  141.   last_name, job_id, salary
  142. FROM
  143.   employees
  144. WHERE
  145.   salary = (
  146.             SELECT
  147.               MIN(salary)
  148.             FROM
  149.               employees
  150.             );      
  151.            
  152. /* 10. displays all the departments that have a minimum salary greater than that of
  153. department 50. */
  154.  
  155.  
  156. SELECT
  157.   department_id, MIN(salary)
  158. FROM
  159.   employees
  160. WHERE
  161.   department_id IS NOT NULL
  162. GROUP BY
  163.   department_id
  164. HAVING
  165.   MIN(salary) > (
  166.                 SELECT
  167.                   MIN(salary)
  168.                 FROM
  169.                   employees
  170.                 WHERE
  171.                   department_id = 50
  172.                 );
  173.  
  174. /* 11. Find the employees who earn the same salary as the minimum salary for each department.
  175. Hint ! The main query would look like the following:
  176. SELECT last_name, salary, department_id
  177. FROM employees
  178. WHERE salary IN (2500, 4200, 4400, 6000, 7000, 8300, 8600, 1 7000);
  179. So, convert it in to subquerie.  */
  180.  
  181. SELECT
  182.   last_name, salary, department_id
  183. FROM
  184.   employees
  185. WHERE
  186.   salary IN (
  187.             SELECT
  188.               MIN(salary)
  189.             FROM
  190.               employees
  191.             GROUP BY
  192.               department_id
  193.             );
Advertisement
Add Comment
Please, Sign In to add comment