Advertisement
BartekPogoda

Untitled

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