Advertisement
BartekPogoda

BD2 final

Mar 15th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SELECT * FROM locations;
  2. /* 1. Write a query to display the last name, department number, and department name for all employees.  */
  3.  
  4. SELECT
  5.   e.last_name,
  6.   department_id,
  7.   d.department_name
  8. FROM
  9.   employees e left join
  10.   departments d USING(department_id);
  11.  
  12. /* 2. Create a unique listing of all jobs that are in department 80. Include the location of department 80 in the
  13. output. */
  14.  
  15. SELECT DISTINCT job_id, d.location_id
  16. FROM
  17.   employees e join
  18.   departments d USING(department_id)
  19. WHERE
  20.   department_id = 80;
  21.  
  22. /* 3. Write a query to display the employee last name, department name, location ID, and city of all employees
  23. who earn a commission. */
  24.  
  25. SELECT
  26.   e.last_name,
  27.   d.department_name,
  28.   location_id,
  29.   l.city
  30. FROM
  31.   employees e join
  32.   departments d USING(department_id) join
  33.   locations l USING(location_id)
  34. WHERE
  35.   e.commission_pct IS NOT NULL;
  36.  
  37. /* 4.Display the employee last name and department name for all employees who have an a (lowercase) in
  38. their last names. */
  39.  
  40. SELECT
  41.   e.last_name,
  42.   d.department_name
  43. FROM
  44.   employees e left join
  45.   departments d USING(department_id)
  46. WHERE
  47.   e.last_name LIKE '%a%';
  48.  
  49. /* 5. Write a query to display the last name, job, department number, and department name for all employees
  50. who work in Toronto. Use JOIN .. ON. */
  51.  
  52. SELECT  
  53.   e.last_name,
  54.   j.job_id,
  55.   d.department_id,
  56.   d.department_name
  57. FROM
  58.   employees e join
  59.   jobs j ON (e.job_id = j.job_id) join
  60.   departments d ON (e.department_id = d.department_id) join
  61.   locations l ON (d.location_id = l.location_id)
  62. WHERE
  63.   l.city = 'Toronto';
  64.  
  65. /* 6. Create a query that displays employee last names, department numbers, and all the employees who work
  66. in the same department as a given employee. Give each column an appropriate label. Use JOIN … ON. */
  67.  
  68. SELECT
  69.   e.department_id "DEPARTMENT", e.last_name "EMPLOYEE", e1.last_name "COLLEAGUE"
  70. FROM
  71.   employees e join
  72.   employees e1 ON (e.department_id = e1.department_id AND e.employee_id <> e1.employee_id)
  73. ORDER BY
  74.   department ASC;
  75.  
  76. /* 7. Create a query to display the name and hire date of any employee hired after employee Davies.
  77. Join a table to itself (self join). You can not use WHERE HIRE_DATE>’ 97/01/29’. */
  78.  
  79. SELECT
  80.   e.last_name,
  81.   e.hire_date
  82. FROM
  83.   employees e inner join
  84.   employees e2 ON e2.last_name = 'Davies' AND  
  85.   e.hire_date > e2.hire_date
  86. ORDER BY
  87.   e.hire_date ASC;
  88.  
  89. /* 8. Display the names and hire dates for all employees who were hired before their managers, along with their
  90. manager’s names and hire dates. Label the columns Employee, Emp Hired, Manager, and Mgr
  91. Hired, respectively.
  92. Join a table to itself (self join) */
  93.  
  94. SELECT
  95.   e.last_name "Employee",
  96.   e.hire_date "Emp Hired",
  97.   m.last_name "Manager",
  98.   m.hire_date "Mgr Hired"
  99. FROM
  100.   employees e inner join
  101.   employees m ON
  102.   e.hire_date < m.hire_date AND
  103.   e.manager_id = m.employee_id;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement