Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.74 KB | None | 0 0
  1. --1. Write a query to display the last name, department number, and department name for all employees.
  2.  
  3. SELECT e.last_name, e.department_id, d.department_name
  4. FROM employees e LEFT JOIN departments d
  5. ON e.department_id = d.department_id;
  6.  
  7.  
  8. --2. Create a unique listing of all jobs that are in department 80. Include the location of department 80 in the output.
  9.  
  10. SELECT DISTINCT e.job_id, d.location_id
  11. FROM employees e JOIN departments d
  12. ON (e.department_id=d.department_id)
  13. WHERE e.department_id=80;
  14.  
  15.  
  16. --3. Write a query to display the employee last name, department name, location ID, and city of all employees who earn a commission.
  17.  
  18. SELECT e.last_name, d.department_name, d.location_id, l.city
  19. FROM employees e
  20. JOIN departments d
  21. ON (e.department_id = d.department_id)
  22. JOIN locations l
  23. ON (d.location_id=l.location_id)
  24. WHERE e.commission_pct IS NOT NULL;
  25.  
  26.  
  27. --4.Display the employee last name and department name for all employees who have an a (lowercase) in their last names.
  28.  
  29. SELECT e.last_name, d.department_name
  30. FROM employees e
  31. JOIN departments d
  32. ON (e.department_id = d.department_id)
  33. WHERE e.last_name LIKE '%a%';
  34.  
  35.  
  36. --5. Write a query to display the last name, job, department number, and department name for all employees who work in Toronto. Use JOIN .. ON.
  37.  
  38.  
  39. SELECT e.last_name, e.job_id, e.department_id, d.department_name
  40. FROM employees e
  41. JOIN departments d
  42. ON (e.department_id=d.department_id)
  43. JOIN locations l
  44. ON (d.location_id=l.location_id)
  45. WHERE l.city= 'Toronto';
  46.  
  47.  
  48. --6. Create a query that displays employee last names, department numbers, and all the employees who work in the same department as a given employee. Give each column an appropriate label. Use JOIN … ON.
  49.  
  50.  
  51. SELECT e.last_name AS "Employee", e.department_id AS "Department", c.last_name AS "Colleague" FROM employees e
  52. JOIN employees c
  53. ON (e.department_id=c.department_id)
  54. WHERE (e.employee_id != c.employee_id)
  55.  
  56.  
  57. --7. Create a query to display the name and hire date of any employee hired after employee Davies.
  58. --Join a table to itself (self join). You can not use WHERE HIRE_DATE>’ 97/01/29’.
  59.  
  60. SELECT e.last_name, e.hire_date
  61. FROM employees e
  62. JOIN employees davies
  63. ON (davies.last_name='Davies')
  64. WHERE davies.hire_date < e.hire_date;
  65.  
  66.  
  67. --8. Display the names and hire dates for all employees who were hired before their managers, along with their manager’s names and hire dates. Label the columns Employee, Emp Hired, Manager, and Mgr Hired, respectively.
  68. --Join a table to itself (self join).
  69.  
  70. SELECT e.last_name AS "Employee", e.hire_date AS "Emp Hired",
  71. m.last_name AS "Manager", m.hire_date AS "Mgr Hired"
  72. FROM employees e JOIN employees m
  73. ON (e.manager_id=m.employee_id)
  74. WHERE e.hire_date < m.hire_date;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement