Answers To Exercises Given in 9-DEC-2011 Batch Related To HR Schema in Oracle Database 11g
Answers To Exercises Given in 9-DEC-2011 Batch Related To HR Schema in Oracle Database 11g
11g
Here are the answers to excercises related to queries and PL/SQL programs given in 9-DEC-2011 Oracle Database 11g batch.
The following is the structure of the tables provided by Oracle in Human Resource Schema (HR).
Queries
1. Display details of jobs where the minimum salary is greater than 10000.
10000.
2. Display the first name and join date of the employees who joined between 2002 and 2005.
2005.
3. Display first name and join date of the employees who is either IT Programmer or Sales Man.
SELECT * FROM
FROM EMPLOYEES where hire_date
hire_date > '01-jan-2008'
'01-jan-2008'
6. Display first name, salary, commission pct, and hire date for employees with salary less than 10000.
10000.
SELECT FIRST_NAME, SALARY, COMMISSION_PCT, HIRE_DATE FROM EMPLOYEES WHERE SALARY < 10000
7. Display job Title, the difference between minimum and maximum salaries for jobs with max salary in the range 10000
10000 to 20000
20000..
SELECT JOB_TITLE, MAX_SALARY-MIN_SALARY DIFFERENCE FROM JOBS WHERE MAX_SALARY BETWEEN 10000 AND 20000
10. Display employees where the first name or last name starts with S.
SELECT FIRST_NAME, LAST_NAME FROM EMPLOYEES WHERE FIRST_NAME LIKE 'S%' OR LAST_NAME LIKE 'S%'
12. Display details of the employees where commission percentage is null and salary in the r ange 5000 to 10000 and department is 30.
SELECT * FROM EMPLOYEES WHERE COMMISSION_PCT IS NULL AND SALARY BETWEEN 5000 AND 10000 AND DEPARTMENT_ID=30
13. Display first name and date of first salary of the employees.
16. Display first name and last name after converting the first letter of each name to upper case and the rest to lower case.
18. Display the length of fir st name for employees where last name contain character ‘b’ after 3rd position.
19. Display first name in upper case and email address in lower case for employees where the fi rst name and email address are same irrespective of the
case.
21. Display the number of days between system date and 1st January 2011.
22. Display how many employees joined in each month of the current year.
24. Display employee ID and the date on which he ended his previous job.
26. Display the country ID and number of cities we have in the country.
27. Display average salary of employees in each department who have commission percentage.
SELECT DEPARTMENT_ID, AVG(SALARY) FROM EMPLOYEES
WHERE COMMISSION_PCT IS NOT NULL GROUP BY DEPARTMENT_ID
28. Display job ID, number of employees, sum of salary, and difference between highest salary and lowest salary of the employees of the job.
SELECT JOB_ID, COUNT(*), SUM(SALARY), MAX(SALARY)-MIN(SALARY) SALARY FROM EMPLOYEES GROUP BY JOB_ID
29. Display job ID for jobs with average salary more than 10000.
31. Display departments in which more than five employees have commission percentage.
32. Display employee ID for employees who did more than one job in the past.
33. Display job ID of jobs that were done by more than 3 employees for more than 100 days.
35. Display departments where any manager is managing more than 5 employees.
36. Change salary of employee 115 to 8000 if the existing salary is le ss than 6000.
UPDATE EMPLOYEES SET SALARY = 8000 WHERE EMPLOYEE_ID = 115 AND SALARY < 6000
37. Insert a new employee into employees with all the required details.
INSERT INTO EMPLOYEES (EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, HIRE_DATE,JOB_ID, SALARY, DEPARTMENT_ID)
VALUES (207, 'ANGELA', 'SNYDER','ANGELA','215 253 4737', SYSDATE, 'SA_MAN', 12000, 80)
39. Change job ID of employee 110 to IT_PROG if the employee belongs to department 10 and the existing job ID does not start with IT.
40. Insert a row into departments table with manager ID 120 and location ID in any location ID for city Tokyo.
SELECT DEPARTMENT_NAME, COUNT(*) FROM EMPLOYEES NATURAL JOIN DEPARTMENTS GROUP BY DEPARTMENT_NAME
42. Display job title, employee ID, number of days between ending date and starting date for all j obs in department 30 from job history.
SELECT DEPARTMENT_NAME, FIRST_NAME, CITY FROM DEPARTMENTS D JOIN EMPLOYEES E ON (D.MANAGER_ID=E.EMPLOYEE_ID) JOIN LOCATIONS L USING
46. Display job title, department name, employee last name, starting date for all j obs from 2000 to 2005.
48. Display job title, employee name, and the difference between maximum salary for the job and salary of the employee.
SELECT JOB_TITLE, FIRST_NAME, MAX_SALARY-SALARY DIFFERENCE FROM EMPLOYEES NATURAL JOIN JOBS
49. Display last name, job title of employees who have commission percentage and belongs to department 30.
SELECT JOB_TITLE, FIRST_NAME, MAX_SALARY-SALARY DIFFERENCE FROM EMPLOYEES NATURAL JOIN JOBS WHERE DEPARTMENT_ID = 30
50. Display details of jobs that were done by any employee who is currently drawing more than 15000 of salary.
SELECT JH.*
FROM JOB_HISTORY JH JOIN EMPLOYEES E ON (JH.EMPLOYEE_ID = E.EMPLOYEE_ID)
WHERE SALARY > 15000
51. Display department name, manager name, and salary of the manager for all managers whose experience is more than 5 years.
52. Display employee name if the employee joined before his manager.
53. Display employee name, job title for the jobs employee did in the past where the job was done less than six months.
SELECT FIRST_NAME, JOB_TITLE FROM EMPLOYEES E JOIN JOB_HISTORY JH ON (JH.EMPLOYEE_ID = E.EMPLOYEE_ID) JOIN JOBS J ON( JH.JOB_ID =
WHERE MONTHS_BETWEEN(END_DATE,START_DATE) < 6
55. Display department name, average salary and number of employees with commission within the department.
56. Display the month in which more than 5 employees joined in any department located in Sydney.
SELECT TO_CHAR(HIRE_DATE,'MON-YY')
on employees
for each row
declare
v_enddate date;
v_startdate date;
begin
-- find out whether the employee has any row in job_history table
select max(end_date) into v_enddate
from job_history
where employee_id = :old.employee_id;
Note: Before testing the above trigger, you need to disable UPDATE_JOB_HISTORY trigger, which is already present in HR account, as