Select * from employees where (salary,nvl(commission_pct) ) in(
Select salary,nvl(commission_pct) from employees where department_id=10 )
(34) 查询部门人数大于10的部门的员工信息。
Select * from employees where department_id in(select department_id from employees group by department_id having count(*)>10);
(35) 查询所有员工工资都大于10000元的部门的信息
Select * from department where department_id in (select department_id from employees group by department_id having min(salary)>10000)
(36) 查询所有员工工资都大于5000元的部门的信息及其员工信息。 Select e.*,d.*
From employees e join departments d On e.department_id=d.department_id Where department_id in (
Select department_id from employees group by department_id having min(salary)>5000) )
(37) 查询所有员工工资都在4000元~8000元之间的部门的信息。 Select * from departments where department_id in(
Select department_id from employees group by department_id having min(salary)>=4000 and max(salary)<=8000)
(38) 查询人数最多的部门信息。
Select * from department_id where department_id in(
Select department_id from employees group by department_id having Count(*)>=all(
select count(*) from employees group by department_id ) )
(39) 查询30号部门中工资排序前3名的员工信息。
Select * from employee where department_id=30 and salary is not null and rownum<=3 order by salary desc
(40) 查询所有员工中工资排序在5~10名之间的员工信息。 Select * from (
Select rownum rn,employee_id,salary from (
Select employee_id,salary from employees where salary is not null order by salary desc) e1 )e2
Where rn between 5 and 10
(41) 将各部门员工的工资修改为该员工所在部门平均工资加1000。 update employees e set salary=(select avg(Salary) from employees where department_id=e.department_id)+1000;
(42) 查询各月倒数第2天入职的员工信息。 Select * from employees where hire_date=last(hire_date)-1’ (43) 查询工龄大于或等于10年的员工信息。
select * from hr.employees where (sysdate-hire_date)/365>=10;
(44) 查询员工信息,要求以首字母大写的方式显示所有员工姓(last_name)和员工名
(first_name)。
select initcap(first_name),initcap(last_name) from hr.employees; (45) 查询员工名(first_name)正好为6个字符的员工的信息。 select * from hr.employees where length(first_name)=6;
(46) 查询员工名(first_name)的第2个字母为“M”的员工信息。 select first_name from hr.employees where first_name like '_M%';
(47) 查询所有员工名(first_name),如果包含字母“s”,则用“S”替换。 select replace(first_name,'S','s') from hr.employees; (48) 查询在2月份入职的所有员工信息。
Select * from employees where extract(mm from hire_date)=2;
2.选择题
(1) B (2) B D (3) A (4) A (5) C (6) E (7) B (8) D (9) B C (10) D (11) C (12) BD (13) C (14) B D (15) A (16) C
第9章PL/SQL语言基础
1.实训题
(1) declare
cursor c_emp is select * from employees; begin
for v_emp in c_emp loop
dbms_output.put_line(v_emp.first_name||' '||v_emp.last_name||' '|| v_emp.employee_id||' '||v_emp.salary||' '||v_emp.department_id); end loop; end; (2) declare
v_avgsal employees.salary%type; begin
for v_emp in (select * from employees) loop
select avg(salary) into v_avgsal from employees where department_id=v_emp.department_id; if v_emp.salary>v_avgsal then
dbms_output.put_line(v_emp.first_name||' '||v_emp.last_name||' '|| v_emp.employee_id||' '||v_emp.salary||' '||v_emp.department_id); end if; end loop; end;
(3) declare
cursor c_emp is
select e.employee_id eid,e.last_name ename,
e.department_id edid,m.employee_id mid,m.last_name mname from employees e join employees m on e.manager_id=m.employee_id; v_emp c_emp%rowtype; begin
open c_emp; loop
fetch c_emp into v_emp; exit when c_emp%notfound;
dbms_output.put_line(v_emp.eid||' '||v_emp.ename||' '|| v_emp.edid||' '||v_emp.mid||' '||v_emp.mname); end loop; close c_emp; end;
(4) declare
v_emp employees%rowtype; begin
select * into v_emp from employees where last_name='Smith'; dbms_output.put_line(v_emp.employee_id||' '||
v_emp.first_name||' '||v_emp.last_name||' '|| v_emp.salary||' '||v_emp.department_id); exception
when no_data_found then
insert into employees(employee_id,last_name,salary,email,hire_date, job_id,department_id)
values(2010,'Smith',7500,'smith@neusoft.edu.cn', to_date('2000-10-5','yyyy-mm-dd'),'AD_VP',50); when too_many_rows then
for v_emp in(select * from employees where last_name='Smith')loop dbms_output.put_line(v_emp.employee_id||' '||
v_emp.first_name||' '||v_emp.last_name||' '|| v_emp.salary||' '||v_emp.department_id); end loop; end;
(5)
declare
cursor c_emp is select * from employees; v_increment employees.salary%type; begin
for v_emp in c_emp loop case
when v_emp.job_id='AD_PRES'OR v_emp.job_id='AD_VP' OR v_emp.job_id='AD_ASST' THEN v_increment:=1000;
when v_emp.job_id='FI_MGR' OR v_emp.job_id='FI_ACCOUNT'THEN v_increment:=800;
WHEN v_emp.job_id='AC_MGR' OR v_emp.job_id='AC_ACCOUNT' THEN v_increment:=700;
WHEN v_emp.job_id='SA_MAN' OR v_emp.job_id='SA_REP' THEN v_increment:=600;
WHEN v_emp.job_id='PU_MAN' OR v_emp.job_id='PU_CLERK' THEN v_increment:=500;
WHEN v_emp.job_id='ST_MAN' OR v_emp.job_id='ST_CLERK' OR v_emp.job_id='SH_CLERK' THEN v_increment:=400; WHEN v_emp.job_id='IT_PROG' OR v_emp.job_id='MK_MAN' OR v_emp.job_id='MK_REP' THEN v_increment:=300; ELSE v_increment:=200; end case;
update employees set salary=salary+v_increment where employee_id=v_emp.employee_id; end loop; end;
(6) declare
v_lowsal jobs.min_salary%type; v_highsal jobs.max_salary%type; e exception; begin
update employees set salary=8000 where employee_id=201; select min_salary,max_salary into v_lowsal,v_highsal
from jobs where job_id=(select job_id from employees where employee_id=201);
if 8000 not between v_lowsal and v_highsal then raise e; end if; exception when e then
raise_application_error(-20001,'beyond limit'); rollback; end;