SQL员工基本工资表题目及答案

本文提供了一系列关于SQL员工薪资查询的问题,包括不同条件下的员工信息筛选,如基本工资范围、领导工资、部门平均收入等,并涉及薪资和津贴的调整操作。通过具体的SQL代码展示查询与操作效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

建表语句

--员工基本工资表 scott.tb_staff_salary
CREATE TABLE scott.tb_staff_salary(
    sal_month        char(6) ,            --基本工资月份,如201604 表示2016年4月
    staff_serial    number(8),             --工号
    sal              number(18,2),         --基本工资
    sal_subsidy        number(18,2),         --津贴
    sal_dt            char(8)                --发放日期,如20160505 表示2016年5月5日
);
alter table scott.TB_STAFF_SALARY
add constraint PK_STAFF_SALARY primary key (SAL_MONTH, STAFF_SERIAL);
insert into scott.tb_staff_salary(sal_month,staff_serial,sal,sal_subsidy,sal_dt) values('201706',1,7000,800,'20170710');
insert into scott.tb_staff_salary(sal_month,staff_serial,sal,sal_subsidy,sal_dt) values('201706',2,6000,800,'20170710');
insert into scott.tb_staff_salary(sal_month,staff_serial,sal,sal_subsidy,sal_dt) values('201706',3,4500,800,'20170710');
insert into scott.tb_staff_salary(sal_month,staff_serial,sal,sal_subsidy,sal_dt) values('201706',4,4600,800,'20170710');
insert into scott.tb_staff_salary(sal_month,staff_serial,sal,sal_subsidy,sal_dt) values('201706',5,4000,500,'20170710');
insert into scott.tb_staff_salary(sal_month,staff_serial,sal,sal_subsidy,sal_dt) values('201706',6,3600,500,'20170710');

--员工信息表    scott.tb_staff_info
CREATE TABLE scott.tb_staff_info(
    staff_serial    number(8)     ,        --工号
    dept             char(2)     ,        --部门
    staff_name         varchar2(10)    ,     --姓名
    sex              char(1)     ,        --性别,0表示男,1表示女
    staff_lead        number(8)             --工号, 其领导工号
);
alter table scott.tb_staff_info
add constraint PK_STAFF_INFO primary key (STAFF_SERIAL);
insert into scott.tb_staff_info(staff_serial,dept,staff_name,sex,staff_lead) values(1,'00','张三','0',0);
insert into scott.tb_staff_info(staff_serial,dept,staff_name,sex,staff_lead) values(2,'01','张四','0',1);
insert into scott.tb_staff_info(staff_serial,dept,staff_name,sex,staff_lead) values(3,'01','李四','1',2);
insert into scott.tb_staff_info(staff_serial,dept,staff_name,sex,staff_lead) values(4,'01','李五','0',2);
insert into scott.tb_staff_info(staff_serial,dept,staff_name,sex,staff_lead) values(5,'02','王四','1',1);
insert into scott.tb_staff_info(staff_serial,dept,staff_name,sex,staff_lead) values(6,'02','赵六','0',5);

题目

1. 查询2017年6月所有基本工资低于5000元的男员工或者低于4000元女员工的信息,按部门降序,工号升序排列。 显示为:
   部门        工号        姓名        性别(男/女)        基本工资        津贴        合计收入
代码

select b.dept 部门,
       b.staff_serial 工号,
       b.staff_name 姓名,
       case b.sex
         when '0' then
          '男'
         else
          '女'
       end as 性别,
       a.sal 基本工资,
       a.sal_subsidy 津贴,
       a.sal + a.sal_subsidy 合计收入
  from tb_staff_salary a, tb_staff_info b
 where a.sal_month = '201706'
   and a.staff_serial = b.staff_serial
   and ((b.sex = '0' and a.sal < 5000) or (b.sex = '1' and a.sal < 4000))
 order by b.dept desc, b.staff_serial asc;

效果

2. 查询2017年6月所有基本工资介于5000元与8000元之间的员工信息,包括
     部门        工号        姓名        领导姓名        基本工资        津贴        合计基本工资

代码

 select b.dept 部门 ,
       a.staff_serial 工号,
       b.staff_name 姓名,
       c.staff_name 领导姓名 ,
       a.sal 基本工资 ,
       a.sal_subsidy 津贴 ,
       a.sal + a.sal_subsidy  合计基本工资
  from scott.tb_staff_salary a
 inner join scott.tb_staff_info b
    on a.staff_serial = b.staff_serial
  left join scott.tb_staff_info c
    on b.staff_lead = c.staff_serial
 where a.sal_month = '201706'
   and a.sal between 5000 and 8000;

效果

     
3. 查询2017年6月其所有领导基本工资大于6000元的员工信息
     部门        工号        姓名        基本工资        津贴

代码

 select b.dept         部门,
        b.staff_serial 工号,
        b.staff_name   姓名,
        a.sal          基本工资,
        a.sal_subsidy  津贴
   from tb_staff_salary a, tb_staff_info b
  where a.sal_month = '201706'
    and a.staff_serial = b.staff_serial
    and b.staff_lead in (select staff_serial
                           from tb_staff_salary c
                          where c.sal_month = '201706'
                            and c.sal > 6000);

效果

                                                       
                
4. 查询2017年6月部门平均收入大于5000元的所在部门的员工信息
     部门        工号        姓名        基本工资        津贴

代码

 select b.dept 部门, b.staff_serial 工号, b.staff_name 姓名, a.sal 基本工资, a.sal_subsidy 津贴
   from tb_staff_salary a, tb_staff_info b
  where a.sal_month = '201706'
    and a.staff_serial = b.staff_serial
    and b.dept in (select d.dept
                     from tb_staff_salary c, tb_staff_info d
                    where c.sal_month = '201706'
                      and c.staff_serial = d.staff_serial
                    group by d.dept
                   having avg(c.sal + c.sal_subsidy) > 5000);

效果

5.发放201707月份薪资,工资部分,男性员工工资均涨3%,女性涨4%;

代码

delete from tb_staff_salary where sal_month = '201707';
insert into tb_staff_salary
  (sal_month, staff_serial, sal, sal_subsidy, sal_dt)
  select '201707',
         a.staff_serial,
         round(case
                 when b.sex = '0' then
                  a.sal * (1 + 0.03)
                 else
                  a.sal * (1 + 0.04)
               end,
               2),
         a.sal_subsidy,
         sal_dt
    from tb_staff_salary a, tb_staff_info b
   where a.sal_month = '201706'
     and a.staff_serial = b.staff_serial;

 

                
 
6.修改201707月份津贴,所有低于当月平均津贴的员工,津贴增加5%,原有津贴为0的,增加平均津贴的10%

代码

 update tb_staff_salary a
    set a.sal_subsidy =
        (select round(case
                        when a.sal_subsidy = 0 then
                         b.avg_sal_subsidy * 0.1
                        when a.sal_subsidy < b.avg_sal_subsidy then
                         b.avg_sal_subsidy * 1.05
                      end,
                      2)
           from (select avg(sal_subsidy) as avg_sal_subsidy
                   from tb_staff_salary
                  where sal_month = '201707') b)
  where a.sal_month = '201707';

效果

SELECT * FROM scott.tb_staff_salary order by SAL_MONTH desc,STAFF_SERIAL

 
 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值