100% found this document useful (1 vote)
610 views10 pages

HR Schema Queries and PL - SQL Programs

The document provides answers to exercises on querying and PL/SQL programs related to the HR schema in Oracle Database 11g. It includes 53 SQL queries and PL/SQL programs with answers. It also briefly describes the structure of the main tables in the HR schema including the primary keys for each table.

Uploaded by

Samburland
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
610 views10 pages

HR Schema Queries and PL - SQL Programs

The document provides answers to exercises on querying and PL/SQL programs related to the HR schema in Oracle Database 11g. It includes 53 SQL queries and PL/SQL programs with answers. It also briefly describes the structure of the main tables in the HR schema including the primary keys for each table.

Uploaded by

Samburland
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Answerstoexercisesgivenin9DEC2011batchrelatedtoHRSchemainOracleDatabase

11g
HerearetheanswerstoexcercisesrelatedtoqueriesandPL/SQLprogramsgivenin9DEC2011OracleDatabase11gbatch.
ThefollowingisthestructureofthetablesprovidedbyOracleinHumanResourceSchema(HR).

Note:ColumnsinREDcolorindicateprimarykey(s).

Queries
1. Displaydetailsofjobswheretheminimumsalaryisgreaterthan10000.
SELECT*FROMJOBSWHEREMIN_SALARY>10000
2. Displaythefirstnameandjoindateoftheemployeeswhojoinedbetween2002and2005.
SELECTFIRST_NAME,HIRE_DATEFROMEMPLOYEES
WHERETO_CHAR(HIRE_DATE,'YYYY')BETWEEN2002AND2005ORDERBYHIRE_DATE
3. DisplayfirstnameandjoindateoftheemployeeswhoiseitherITProgrammerorSalesMan.
SELECTFIRST_NAME,HIRE_DATE
FROMEMPLOYEESWHEREJOB_IDIN('IT_PROG','SA_MAN')
4. Displayemployeeswhojoinedafter1stJanuary2008.
SELECT*FROMEMPLOYEESwherehire_date>'01jan2008'
5. DisplaydetailsofemployeewithID150or160.
SELECT*FROMEMPLOYEESWHEREEMPLOYEE_IDin(150,160)
6. Displayfirstname,salary,commissionpct,andhiredateforemployeeswithsalarylessthan10000.
SELECTFIRST_NAME,SALARY,COMMISSION_PCT,HIRE_DATEFROMEMPLOYEESWHERESALARY<10000
7. DisplayjobTitle,thedifferencebetweenminimumandmaximumsalariesforjobswithmaxsalaryintherange10000to20000.
SELECTJOB_TITLE,MAX_SALARYMIN_SALARYDIFFERENCEFROMJOBSWHEREMAX_SALARYBETWEEN10000AND20000
8. Displayfirstname,salary,androundthesalarytothousands.
SELECTFIRST_NAME,SALARY,ROUND(SALARY,3)FROMEMPLOYEES
9. Displaydetailsofjobsinthedescendingorderofthetitle.

SELECT*FROMJOBSORDERBYJOB_TITLE
10. DisplayemployeeswherethefirstnameorlastnamestartswithS.
SELECTFIRST_NAME,LAST_NAMEFROMEMPLOYEESWHEREFIRST_NAMELIKE'S%'ORLAST_NAMELIKE'S%'
11. DisplayemployeeswhojoinedinthemonthofMay.
SELECT*FROMEMPLOYEESWHERETO_CHAR(HIRE_DATE,'MON')='MAY'
12. Displaydetailsoftheemployeeswherecommissionpercentageisnullandsalaryintherange5000to10000anddepartmentis30.
SELECT*FROMEMPLOYEESWHERECOMMISSION_PCTISNULLANDSALARYBETWEEN5000AND10000ANDDEPARTMENT_ID=30
13. Displayfirstnameanddateoffirstsalaryoftheemployees.
SELECTFIRST_NAME,HIRE_DATE,LAST_DAY(HIRE_DATE)+1FROMEMPLOYEES
14. Displayfirstnameandexperienceoftheemployees.
SELECTFIRST_NAME,HIRE_DATE,FLOOR((SYSDATEHIRE_DATE)/365)FROMEMPLOYEES
15. Displayfirstnameofemployeeswhojoinedin2001.
SELECTFIRST_NAME,HIRE_DATEFROMEMPLOYEESWHERETO_CHAR(HIRE_DATE,'YYYY')=2001
16. Displayfirstnameandlastnameafterconvertingthefirstletterofeachnametouppercaseandtheresttolowercase.
SELECTINITCAP(FIRST_NAME),INITCAP(LAST_NAME)FROMEMPLOYEES
17. Displaythefirstwordinjobtitle.
SELECTJOB_TITLE,SUBSTR(JOB_TITLE,1,INSTR(JOB_TITLE,'')1)FROMJOBS
18. Displaythelengthoffirstnameforemployeeswherelastnamecontaincharacterbafter3rdposition.
SELECTFIRST_NAME,LAST_NAMEFROMEMPLOYEESWHEREINSTR(LAST_NAME,'B')>3
19. Displayfirstnameinuppercaseandemailaddressinlowercaseforemployeeswherethefirstnameandemailaddressaresameirrespectiveofthe
case.
SELECTUPPER(FIRST_NAME),LOWER(EMAIL)FROMEMPLOYEESWHEREUPPER(FIRST_NAME)=UPPER(EMAIL)
20. Displayemployeeswhojoinedinthecurrentyear.
SELECT*FROMEMPLOYEESWHERETO_CHAR(HIRE_DATE,'YYYY')=TO_CHAR(SYSDATE,'YYYY')
21. Displaythenumberofdaysbetweensystemdateand1stJanuary2011.
SELECTSYSDATEto_date('01jan2011')FROMDUAL
22. Displayhowmanyemployeesjoinedineachmonthofthecurrentyear.
SELECTTO_CHAR(HIRE_DATE,'MM'),COUNT(*)FROMEMPLOYEES
WHERETO_CHAR(HIRE_DATE,'YYYY')=TO_CHAR(SYSDATE,'YYYY')GROUPBYTO_CHAR(HIRE_DATE,'MM')
23. DisplaymanagerIDandnumberofemployeesmanagedbythemanager.
SELECTMANAGER_ID,COUNT(*)FROMEMPLOYEESGROUPBYMANAGER_ID
24. DisplayemployeeIDandthedateonwhichheendedhispreviousjob.
SELECTEMPLOYEE_ID,MAX(END_DATE)FROMJOB_HISTORYGROUPBYEMPLOYEE_ID
25. Displaynumberofemployeesjoinedafter15thofthemonth.
SELECTCOUNT(*)FROMEMPLOYEESWHERETO_CHAR(HIRE_DATE,'DD')>15
26. DisplaythecountryIDandnumberofcitieswehaveinthecountry.
SELECTCOUNTRY_ID,COUNT(*)FROMLOCATIONSGROUPBYCOUNTRY_ID
27. Displayaveragesalaryofemployeesineachdepartmentwhohavecommissionpercentage.

SELECTDEPARTMENT_ID,AVG(SALARY)FROMEMPLOYEES
WHERECOMMISSION_PCTISNOTNULLGROUPBYDEPARTMENT_ID
28. DisplayjobID,numberofemployees,sumofsalary,anddifferencebetweenhighestsalaryandlowestsalaryoftheemployeesofthejob.
SELECTJOB_ID,COUNT(*),SUM(SALARY),MAX(SALARY)MIN(SALARY)SALARYFROMEMPLOYEESGROUPBYJOB_ID
29. DisplayjobIDforjobswithaveragesalarymorethan10000.
SELECTJOB_ID,AVG(SALARY)FROMEMPLOYEES
GROUPBYJOB_ID
HAVINGAVG(SALARY)>10000
30. Displayyearsinwhichmorethan10employeesjoined.
SELECTTO_CHAR(HIRE_DATE,'YYYY')FROMEMPLOYEES
GROUPBYTO_CHAR(HIRE_DATE,'YYYY')
HAVINGCOUNT(EMPLOYEE_ID)>10
31. Displaydepartmentsinwhichmorethanfiveemployeeshavecommissionpercentage.
SELECTDEPARTMENT_IDFROMEMPLOYEES
WHERECOMMISSION_PCTISNOTNULL
GROUPBYDEPARTMENT_ID
HAVINGCOUNT(COMMISSION_PCT)>5
32. DisplayemployeeIDforemployeeswhodidmorethanonejobinthepast.
SELECTEMPLOYEE_IDFROMJOB_HISTORYGROUPBYEMPLOYEE_IDHAVINGCOUNT(*)>1
33. DisplayjobIDofjobsthatweredonebymorethan3employeesformorethan100days.
SELECTJOB_IDFROMJOB_HISTORY
WHEREEND_DATESTART_DATE>100
GROUPBYJOB_ID
HAVINGCOUNT(*)>3
34. DisplaydepartmentID,year,andNumberofemployeesjoined.
SELECTDEPARTMENT_ID,TO_CHAR(HIRE_DATE,'YYYY'),COUNT(EMPLOYEE_ID)
FROMEMPLOYEES
GROUPBYDEPARTMENT_ID,TO_CHAR(HIRE_DATE,'YYYY')
ORDERBYDEPARTMENT_ID
35. Displaydepartmentswhereanymanagerismanagingmorethan5employees.
SELECTDISTINCTDEPARTMENT_ID
FROMEMPLOYEES
GROUPBYDEPARTMENT_ID,MANAGER_ID
HAVINGCOUNT(EMPLOYEE_ID)>5
36. Changesalaryofemployee115to8000iftheexistingsalaryislessthan6000.
UPDATEEMPLOYEESSETSALARY=8000WHEREEMPLOYEE_ID=115ANDSALARY<6000
37. Insertanewemployeeintoemployeeswithalltherequireddetails.
INSERTINTOEMPLOYEES(EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,PHONE_NUMBER,HIRE_DATE,JOB_ID,SALARY,DEPARTMENT_ID)
VALUES(207,'ANGELA','SNYDER','ANGELA','2152534737',SYSDATE,'SA_MAN',12000,80)
38. Deletedepartment20.
DELETEFROMDEPARTMENTSWHEREDEPARTMENT_ID=20
39. ChangejobIDofemployee110toIT_PROGiftheemployeebelongstodepartment10andtheexistingjobIDdoesnotstartwithIT.
UPDATEEMPLOYEESSETJOB_ID='IT_PROG'
WHEREEMPLOYEE_ID=110ANDDEPARTMENT_ID=10ANDNOTJOB_IDLIKE'IT%'
40. InsertarowintodepartmentstablewithmanagerID120andlocationIDinanylocationIDforcityTokyo.
INSERTINTODEPARTMENTS(150,'SPORTS',120,1200)
41. Displaydepartmentnameandnumberofemployeesinthedepartment.
SELECTDEPARTMENT_NAME,COUNT(*)FROMEMPLOYEESNATURALJOINDEPARTMENTSGROUPBYDEPARTMENT_NAME

42. Displayjobtitle,employeeID,numberofdaysbetweenendingdateandstartingdateforalljobsindepartment30fromjobhistory.
SELECTEMPLOYEE_ID,JOB_TITLE,END_DATESTART_DATEDAYS
FROMJOB_HISTORYNATURALJOINJOBS
WHEREDEPARTMENT_ID=30
43. Displaydepartmentnameandmanagerfirstname.
SELECTDEPARTMENT_NAME,FIRST_NAMEFROMDEPARTMENTSDJOINEMPLOYEESEON(D.MANAGER_ID=E.EMPLOYEE_ID)
44. Displaydepartmentname,managername,andcity.

SELECTDEPARTMENT_NAME,FIRST_NAME,CITYFROMDEPARTMENTSDJOINEMPLOYEESEON(D.MANAGER_ID=E.EMPLOYEE_ID)JOINLOCATIONSLUSING(LOCAT
45. Displaycountryname,city,anddepartmentname.
SELECTCOUNTRY_NAME,CITY,DEPARTMENT_NAME
FROMCOUNTRIESJOINLOCATIONSUSING(COUNTRY_ID)
JOINDEPARTMENTSUSING(LOCATION_ID)
46. Displayjobtitle,departmentname,employeelastname,startingdateforalljobsfrom2000to2005.
SELECTJOB_TITLE,DEPARTMENT_NAME,LAST_NAME,START_DATE
FROMJOB_HISTORYJOINJOBSUSING(JOB_ID)JOINDEPARTMENTS
USING(DEPARTMENT_ID)JOINEMPLOYEESUSING(EMPLOYEE_ID)
WHERETO_CHAR(START_DATE,'YYYY')BETWEEN2000AND2005
47. Displayjobtitleandaveragesalaryofemployees
SELECTJOB_TITLE,AVG(SALARY)FROMEMPLOYEES
NATURALJOINJOBSGROUPBYJOB_TITLE
48. Displayjobtitle,employeename,andthedifferencebetweenmaximumsalaryforthejobandsalaryoftheemployee.
SELECTJOB_TITLE,FIRST_NAME,MAX_SALARYSALARYDIFFERENCEFROMEMPLOYEESNATURALJOINJOBS
49. Displaylastname,jobtitleofemployeeswhohavecommissionpercentageandbelongstodepartment30.
SELECTJOB_TITLE,FIRST_NAME,MAX_SALARYSALARYDIFFERENCEFROMEMPLOYEESNATURALJOINJOBSWHEREDEPARTMENT_ID=30
50. Displaydetailsofjobsthatweredonebyanyemployeewhoiscurrentlydrawingmorethan15000ofsalary.
SELECTJH.*
FROMJOB_HISTORYJHJOINEMPLOYEESEON(JH.EMPLOYEE_ID=E.EMPLOYEE_ID)
WHERESALARY>15000
51. Displaydepartmentname,managername,andsalaryofthemanagerforallmanagerswhoseexperienceismorethan5years.
SELECTDEPARTMENT_NAME,FIRST_NAME,SALARY
FROMDEPARTMENTSDJOINEMPLOYEESEON(D.MANAGER_ID=E.MANAGER_ID)
WHERE(SYSDATEHIRE_DATE)/365>5
52. Displayemployeenameiftheemployeejoinedbeforehismanager.
SELECTFIRST_NAMEFROMEMPLOYEESE1JOINEMPLOYEESE2ON(E1.MANAGER_ID=E2.EMPLOYEE_ID)
WHEREE1.HIRE_DATE<E2.HIRE_DATE
53. Displayemployeename,jobtitleforthejobsemployeedidinthepastwherethejobwasdonelessthansixmonths.

SELECTFIRST_NAME,JOB_TITLEFROMEMPLOYEESEJOINJOB_HISTORYJHON(JH.EMPLOYEE_ID=E.EMPLOYEE_ID)JOINJOBSJON(JH.JOB_ID=J.JOB_
WHEREMONTHS_BETWEEN(END_DATE,START_DATE)<6
54. Displayemployeenameandcountryinwhichheisworking.
SELECTFIRST_NAME,COUNTRY_NAMEFROMEMPLOYEESJOINDEPARTMENTSUSING(DEPARTMENT_ID)
JOINLOCATIONSUSING(LOCATION_ID)
JOINCOUNTRIESUSING(COUNTRY_ID)
55. Displaydepartmentname,averagesalaryandnumberofemployeeswithcommissionwithinthedepartment.

SELECTDEPARTMENT_NAME,AVG(SALARY),COUNT(COMMISSION_PCT)
FROMDEPARTMENTSJOINEMPLOYEESUSING(DEPARTMENT_ID)
GROUPBYDEPARTMENT_NAME
56. Displaythemonthinwhichmorethan5employeesjoinedinanydepartmentlocatedinSydney.
SELECTTO_CHAR(HIRE_DATE,'MONYY')

FROMEMPLOYEESJOINDEPARTMENTSUSING(DEPARTMENT_ID)JOINLOCATIONSUSING(LOCATION_ID)
WHERECITY='Seattle'
GROUPBYTO_CHAR(HIRE_DATE,'MONYY')
HAVINGCOUNT(*)>5
57. Displaydetailsofdepartmentsinwhichthemaximumsalaryismorethan10000.
SELECT*FROMDEPARTMENTSWHEREDEPARTMENT_IDIN
(SELECTDEPARTMENT_IDFROMEMPLOYEES
GROUPBYDEPARTMENT_ID
HAVINGMAX(SALARY)>10000)
58. DisplaydetailsofdepartmentsmanagedbySmith.
SELECT*FROMDEPARTMENTSWHEREMANAGER_IDIN
(SELECTEMPLOYEE_IDFROMEMPLOYEESWHEREFIRST_NAME='SMITH')
59. Displayjobsintowhichemployeesjoinedinthecurrentyear.
SELECT*FROMJOBSWHEREJOB_IDIN
(SELECTJOB_IDFROMEMPLOYEESWHERETO_CHAR(HIRE_DATE,'YYYY')=TO_CHAR(SYSDATE,'YYYY'))
60. Displayemployeeswhodidnotdoanyjobinthepast.
SELECT*FROMEMPLOYEESWHEREEMPLOYEE_IDNOTIN
(SELECTEMPLOYEE_IDFROMJOB_HISTORY)
61. Displayjobtitleandaveragesalaryforemployeeswhodidajobinthepast.
SELECTJOB_TITLE,AVG(SALARY)FROMJOBSNATURALJOINEMPLOYEES
GROUPBYJOB_TITLE
WHEREEMPLOYEE_IDIN
(SELECTEMPLOYEE_IDFROMJOB_HISTORY)
62. Displaycountryname,city,andnumberofdepartmentswheredepartmenthasmorethan5employees.
SELECTCOUNTRY_NAME,CITY,COUNT(DEPARTMENT_ID)
FROMCOUNTRIESJOINLOCATIONSUSING(COUNTRY_ID)JOINDEPARTMENTSUSING(LOCATION_ID)
WHEREDEPARTMENT_IDIN
(SELECTDEPARTMENT_IDFROMEMPLOYEES

GROUPBYDEPARTMENT_ID

HAVINGCOUNT(DEPARTMENT_ID)>5)
GROUPBYCOUNTRY_NAME,CITY
63. Displaydetailsofmanagerwhomanagesmorethan5employees.
SELECTFIRST_NAMEFROMEMPLOYEES
WHEREEMPLOYEE_IDIN
(SELECTMANAGER_IDFROMEMPLOYEES
GROUPBYMANAGER_ID
HAVINGCOUNT(*)>5)

64. Displayemployeename,jobtitle,startdate,andenddateofpastjobsofallemployeeswithcommissionpercentagenull.
SELECTFIRST_NAME,JOB_TITLE,START_DATE,END_DATE
FROMJOB_HISTORYJHJOINJOBSJUSING(JOB_ID)JOINEMPLOYEESEON(JH.EMPLOYEE_ID=E.EMPLOYEE_ID)
WHERECOMMISSION_PCTISNULL
65. Displaythedepartmentsintowhichnoemployeejoinedinlasttwoyears.
SELECT*FROMDEPARTMENTS
WHEREDEPARTMENT_IDNOTIN
(SELECTDEPARTMENT_IDFROMEMPLOYEESWHEREFLOOR((SYSDATEHIRE_DATE)/365)<2)
66. Displaythedetailsofdepartmentsinwhichthemaxsalaryisgreaterthan10000foremployeeswhodidajobinthepast.
SELECT*FROMDEPARTMENTS
WHEREDEPARTMENT_IDIN
(SELECTDEPARTMENT_IDFROMEMPLOYEES
WHEREEMPLOYEE_IDIN(SELECTEMPLOYEE_IDFROMJOB_HISTORY)
GROUPBYDEPARTMENT_ID
HAVINGMAX(SALARY)>10000)
67. DisplaydetailsofcurrentjobforemployeeswhoworkedasITProgrammersinthepast.
SELECT*FROMJOBS
WHEREJOB_IDIN
(SELECTJOB_IDFROMEMPLOYEESWHEREEMPLOYEE_IDIN
(SELECTEMPLOYEE_IDFROMJOB_HISTORYWHEREJOB_ID='IT_PROG'))

68. Displaythedetailsofemployeesdrawingthehighestsalaryinthedepartment.
SELECTDEPARTMENT_ID,FIRST_NAME,SALARYFROMEMPLOYEESOUTERWHERESALARY=
(SELECTMAX(SALARY)FROMEMPLOYEESWHEREDEPARTMENT_ID=OUTER.DEPARTMENT_ID)
69. DisplaythecityofemployeewhoseemployeeIDis105.
SELECTCITYFROMLOCATIONSWHERELOCATION_ID=
(SELECTLOCATION_IDFROMDEPARTMENTSWHEREDEPARTMENT_ID=
(SELECTDEPARTMENT_IDFROMEMPLOYEESWHEREEMPLOYEE_ID=105)

)
70. Displaythirdhighestsalaryofallemployees
selectsalary
fromemployeesmain
where2=(selectcount(distinctsalary)
fromemployees
wheresalary>main.salary)

PL/SQLPrograms
1. Writeaprogramtointerchangethesalariesofemployee120and122.
Declare
V_salary_120employees.salary%type
Begin
Selectsalaryintov_salary_120
Fromemployeeswhereemployee_id=120
Updateemployeessetsalary=(selectsalaryfromemployeeswhereemployee_id=122)
Whereemployee_id=120
Updateemployeessetsalary=v_salary_120Whereemployee_id=122
Commit
End
2. Increasethesalaryofemployee115basedonthefollowingconditions:Ifexperienceismorethan10years,increasesalaryby20%Ifexperienceis
greaterthan5years,increasesalaryby10%Otherwise5%CasebyExpression:

declare
v_expnumber(2)
v_hikenumber(5,2)
begin
selectfloor((sysdatehire_date)/365)intov_exp
fromemployees
whereemployee_id=115

v_hike:=1.05

case
whenv_exp>10then
v_hike:=1.20
whenv_exp>5then
v_hike:=1.10
endcase

updateemployeessetsalary=salary*v_hike
whereemployee_id=115
end

3. ChangecommissionpercentageasfollowsforemployeewithID=150.Ifsalaryismorethan10000thencommissionis0.4%,ifSalaryislessthan10000
butexperienceismorethan10yearsthen0.35%,ifsalaryislessthan3000thencommissionis0.25%.Intheremainingcasescommissionis0.15%.
declare
v_salaryemployees.salary%type
v_expnumber(2)
v_cpnumber(5,2)
begin
selectv_salary,floor((sysdatehire_date)/365)intov_salary,v_exp
fromemployees
whereemployee_id=150

ifv_salary>10000then
v_cp:=0.4
elsifv_exp>10then
v_cp:=0.35
elsifv_salary<3000then
v_cp:=0.25
else
v_cp:=0.15


endif

updateemployeessetcommission_pct=v_cp
whereemployee_id=150
end
4. Findoutthenameoftheemployeeandnameofthedepartmentfortheemployeewhoismanagingforemployee103.
declare
v_nameemployees.first_name%type
v_deptnamedepartments.department_name%type
begin
selectfirst_name,department_nameintov_name,v_deptname
fromemployeesjoindepartmentsusing(department_id)
whereemployee_id=(selectmanager_idfromemployeeswhereemployee_id=103)

dbms_output.put_line(v_name)
dbms_output.put_line(v_deptname)

end

5. DisplaymissingemployeeIDs.
declare
v_minnumber(3)
v_maxnumber(3)
v_cnumber(1)
begin
selectmin(employee_id),max(employee_id)intov_min,v_max
fromemployees
foriinv_min+1..v_max1
loop
selectcount(*)intov_c
fromemployees
whereemployee_id=i

ifv_c=0then
dbms_output.put_line(i)
endif
endloop

end
6. Displaytheyearinwhichmaximumnumberofemployeesjoinedalongwithhowmanyjoinedineachmonthinthatyear.
declare
v_yearnumber(4)
v_cnumber(2)
begin
selectto_char(hire_date,'yyyy')intov_year
fromemployees
groupbyto_char(hire_date,'yyyy')
havingcount(*)=
(selectmax(count(*))
fromemployees
groupbyto_char(hire_date,'yyyy'))

dbms_output.put_line('Year:'||v_year)
formonthin1..12
loop
selectcount(*)intov_c
fromemployees
whereto_char(hire_date,'mm')=monthandto_char(hire_date,'yyyy')=v_year

dbms_output.put_line('Month:'||to_char(month)||'Employees:'||to_char(v_c))
endloop
end

7. Changesalaryofemployee130tothesalaryoftheemployeewithfirstnameJoe.IfJoeisnotfoundthentakeaveragesalaryofallemployees.Ifmore
thanoneemployeewithfirstnameJoeisfoundthentaketheleastsalaryoftheemployeeswithfirstnameJoe.
declare
v_salaryemployees.salary%type
begin
selectsalaryintov_salary
fromemployeeswherefirst_name='Joe'

updateemployeessetsalary=v_salary
whereemployee_id=130

exception
whenno_data_foundthen
updateemployeessetsalary=(selectavg(salary)fromemployees)
whereemployee_id=130
end
8. DisplayJobTitleandNameoftheEmployeewhojoinedthejobfirstday.
declare
cursorjobscurisselectjob_id,job_titlefromjobs

v_nameemployees.first_name%type
begin
forjobrecinjobscur

loop

selectfirst_nameintov_name
fromemployees
wherehire_date=(selectmin(hire_date)fromemployeeswherejob_id=jobrec.job_id)

andjob_id=jobrec.job_id

dbms_output.put_line(jobrec.job_title||''||v_name)

endloop
end

9. Display5thand10themployeesinEmployeestable.
declare
cursorempcuris
selectemployee_id,first_name
fromemployees

begin
foremprecinempcur
loop
ifempcur%rowcount>4then
dbms_output.put_line(emprec.first_name)
exitwhenempcur%rowcount>10
endif
endloop

end
10. Updatesalaryofanemployeebasedondepartmentandcommissionpercentage.Ifdepartmentis40increasesalaryby10%.Ifdepartmentis70then
15%,ifcommissionismorethan.3%then5%otherwise10%.
declare
cursorempcuris
selectemployee_id,department_id,commission_pct
fromemployees

v_hikenumber(2)
begin
foremprecinempcur
loop
ifemprec.department_id=40then
v_hike:=10
elsifemprec.department_id=70then
v_hike:=15
elsifemprec.commission_pct>0.30then
v_hike:=5
else
v_hike:=10
endif
updateemployeessetsalary=salary+salary*v_hike/100
whereemployee_id=emprec.employee_id

endloop
end
11. CreateafunctionthattakesdepartmentIDandreturnsthenameofthemanagerofthedepartment.
createorreplacefunctionget_dept_manager_name(deptidnumber)
returnvarcharis
v_nameemployees.first_name%type
begin
selectfirst_nameintov_name
fromemployees
whereemployee_id=(selectmanager_idfromdepartmentswheredepartment_id=deptid)
returnv_name
end

12. CreateafunctionthattakesemployeeIDandreturnthenumberofjobsdonebytheemployeeinthepast.
createorreplacefunctionget_no_of_jobs_done(empidnumber)
returnnumberis
v_countnumber(2)
begin
selectcount(*)intov_count
fromjob_history
whereemployee_id=empid
returnv_count
end
13. CreateaprocedurethattakesdepartmentIDandchangesthemanagerIDforthedepartmenttotheemployeeinthedepartmentwithhighestsalary.(Use
Exceptions).
createorreplaceprocedurechange_dept_manager(deptidnumber)
is
v_empidemployees.employee_id%type
begin
selectemployee_idintov_empid
fromemployees
wheresalary=(selectmax(salary)fromemployeeswheredepartment_id=deptid)
anddepartment_id=deptid
updatedepartmentssetmanager_id=v_empid
wheredepartment_id=deptid
end
14. CreateafunctionthattakesamanagerIDandreturnthenamesofemployeeswhoreporttothismanager.Thenamesmustbereturnedasastringwith
commaseparatingnames.
createorreplacefunctionget_employees_for_manager(managernumber)
returnvarchar2
is
v_employeesvarchar2(1000):=''
cursorempcuris
selectfirst_namefromemployees
wheremanager_id=manager
begin
foremprecinempcur
loop
v_employees:=v_employees||','||emprec.first_name
endloop
removeextra,atthebeginning
returnltrim(v_employees,',')
end
15. EnsurenochangescanbemadetoEMPLOYEEStablebefore6amandafter10pminaday.
createorreplacetriggertrg_employees_time_check
beforeupdateorinsertordelete
onemployees
foreachrow
begin
ifto_char(sysdate,'hh24')<6orto_char(sysdate,'hh24')>10then
raise_application_error(20111,'Sorry!Nochangecanbemadebefore6AMandafter10PM')
endif
end
16. CreateaTriggertoensurethesalaryoftheemployeeisnotdecreased.
createorreplacetriggertrg_employees_salary_check
beforeupdate
onemployees
foreachrow
begin
if:old.salary>:new.salarythen
raise_application_error(20111,'Sorry!Salarycannotbedecreased!')
endif
end

17. Createatriggertoensuretheemployeeandmanagerbelongstothesamedepartment.
Note:Thistriggerneedtoreadtherowthatisbeingmodified,whichcausesmutatingproblem.Thesolutiontomutatingproblemis
explainedat:WorkaroundformutatingprobleminOracleTriggers.Pleasecheckitout.
18. Wheneverthejobischangedforanemployeewritethefollowingdetailsintojobhistory.EmployeeID,oldjobID,olddepartmentID,hiredateofthe
employeeforstartdate,systemdateforenddate.Butifarowisalreadypresentforemployeejobhistorythenthestartdateshouldbetheenddateof
thatrow+1.
createorreplacetriggertrg_log_job_change
afterupdateofjob_id

onemployees
foreachrow
declare
v_enddatedate
v_startdatedate
begin
findoutwhethertheemployeehasanyrowinjob_historytable
selectmax(end_date)intov_enddate
fromjob_history
whereemployee_id=:old.employee_id
ifv_enddateisnullthen
v_startdate:=:old.hire_date
else
v_startdate:=v_enddate+1
endif
insertintojob_historyvalues(:old.employee_id,v_startdate,sysdate,:old.job_id,:old.department_id)
end

Note:Beforetestingtheabovetrigger,youneedtodisableUPDATE_JOB_HISTORYtrigger,whichisalreadypresentinHRaccount,asitdoes

You might also like