|
| 1 | +--1. Display the first promotion year for each employee. |
| 2 | +Select emp.first_name, emp.employee_id,to_char (min(end_date),'YYYY') FPY |
| 3 | +from hr.employees emp left join hr.job_history jb on emp.employee_id=jb.employee_id group by emp.first_name, emp.employee_id; |
| 4 | +--2. Display location, city and department name of employees who have been promoted more than |
| 5 | +--once. |
| 6 | +Select dep.location_id, loc.city, dep.department_name from hr.employees emp inner join hr.departments dep on emp.department_id=dep.department_id |
| 7 | +inner join hr.locations loc on dep.location_id=loc.location_id where emp.employee_id in( |
| 8 | +Select employee_id from hr.job_history jh group by employee_id having count(*)>1); |
| 9 | +--3. Display minimum and maximum “hire_date” of employees work in IT and HR departments. |
| 10 | +Select min(hire_date),max(hire_date) from hr.employees emp left join hr.departments dep on dep.department_id=emp.department_id |
| 11 | +where department_name in ('Human Resources', 'IT') |
| 12 | +group by department_name; |
| 13 | +--4. Find difference between current date and hire dates of employees after sorting them by hire |
| 14 | +--date, then show difference in days, months and years. |
| 15 | +select first_name||' '||last_name name, hire_date, sysdate, |
| 16 | +extract(year from sysdate) - extract(year from hire_date) year_diff, |
| 17 | +round(MONTHS_BETWEEN(SYSDATE, hire_date), 2) month_diff, |
| 18 | +round(sysdate-hire_date, 2) day_diff |
| 19 | +from hr.employees; |
| 20 | + |
| 21 | +--5. Find which departments used to hire earliest/latest. |
| 22 | +Select distinct department_name from hr.departments dep left join hr.employees emp on dep.department_id=emp.department_id where hire_date = (select min(hire_date) from hr.employees) or |
| 23 | + hire_date = (select max(hire_date) from hr.employees); |
| 24 | + |
| 25 | +--6. Find the number of departments with no employee for each city. |
| 26 | +select loc.city, count(employee_id) no_employee |
| 27 | +from hr.locations loc |
| 28 | +left join hr.departments dep on loc.location_id = dep.location_id |
| 29 | +left join hr.employees emp on dep.department_id = emp.department_id |
| 30 | +group by loc.city |
| 31 | +having count(employee_id)=0; |
| 32 | +--7. Create a category called “seasons” and find in which season most employees were hired. |
| 33 | +select case when extract(month from hire_date) in(12, 1, 2) then 'Winter' |
| 34 | + when extract(month from hire_date) in(3, 4, 5) then 'Spring' |
| 35 | + when extract(month from hire_date) in(6, 7, 8) then 'Summer' |
| 36 | + when extract(month from hire_date) in(9, 10, 11) then 'Autumn' |
| 37 | + else 'N/A' end as seasons, |
| 38 | + count(distinct employee_id) |
| 39 | +from hr.employees |
| 40 | +group by case when extract(month from hire_date) in(12, 1, 2) then 'Winter' |
| 41 | + when extract(month from hire_date) in(3, 4, 5) then 'Spring' |
| 42 | + when extract(month from hire_date) in(6, 7, 8) then 'Summer' |
| 43 | + when extract(month from hire_date) in(9, 10, 11) then 'Autumn' |
| 44 | + else 'N/A' end ; |
| 45 | + |
| 46 | +--8. Find the cities of employees with average salary more than 5000. |
| 47 | +Select loc.city,emp.first_name from hr.employees emp inner join hr.departments dep on emp.department_id=dep.department_id |
| 48 | +inner join hr.locations loc on dep.location_id=loc.location_id where avg(salary)>5000; |
| 49 | + |
0 commit comments