0
点赞
收藏
分享

微信扫一扫

day3 多表操作 (部门薪资)案例 :

  create table dept (
  deptno numeric(2),--部门编号
  dname varchar(14),--部门名称
  loc varchar(13) --地点
);

   insert into dept values (10, 'ACCOUNTING', 'NEW YORK');
   insert into dept values (20, 'RESEARCH', 'DALLAS');
   insert into dept values (30, 'SALES', 'CHICAGO');
   insert into dept values (40, 'OPERATIONS', 'BOSTON');

 

    create table salgrade (
    grade numeric,--等级
    losal numeric,--最低等级
    hisal numeric --最高等级
  );

  insert into salgrade values (1, 700, 1200);
  insert into salgrade values (2, 1201, 1400);
  insert into salgrade values (3, 1401, 2000);
  insert into salgrade values (4, 2001, 3000);
  insert into salgrade values (5, 3001, 9999);
    create table emp (
    empno numeric(4) not null,--员工号
    ename varchar(10),--员工姓名
    job varchar(9),--工作
    mgr numeric(4),--上级编号
    hiredate datetime,--受雇日期
    sal numeric(7, 2),--薪金
    comm numeric(7, 2),--佣金
    deptno numeric(2) --部门编号
);

    insert into emp values (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, null, 20);
    insert into emp values (7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20', 1600, 300, 30);
    insert into emp values (7521, 'WARD', 'SALESMAN', 7698, '1981-02-22', 1250, 500, 30);
    insert into emp values (7566, 'JONES', 'MANAGER', 7839, '1981-04-02', 2975, null, 20);
    insert into emp values (7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28', 1250, 1400, 30);
    insert into emp values (7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01', 2850, null, 30);
    insert into emp values (7782, 'CLARK', 'MANAGER', 7839, '1981-06-09', 2450, null, 10);
    insert into emp values (7788, 'SCOTT', 'ANALYST', 7566, '1982-12-09', 3000, null, 20);
    insert into emp values (7839, 'KING', 'PRESIDENT', null, '1981-11-17', 5000, null, 10);
    insert into emp values (7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08', 1500, 0, 30);
    insert into emp values (7876, 'ADAMS', 'CLERK', 7788, '1983-01-12', 1100, null, 20);
    insert into emp values (7900, 'JAMES', 'CLERK', 7698, '1981-12-03', 950, null, 30);
    insert into emp values (7902, 'FORD', 'ANALYST', 7566, '1981-12-03', 3000, null, 20);
    insert into emp values (7934, 'MILLER', 'CLERK', 7782, '1982-01-23', 1300, null, 10);

 

    select  * from emp 
       where (deptno = 10 and job='MANAGER')
    or 
       (deptno = 20 and job='SALESMAN');
select  *, (sal+ifnull(comm,0)) as earing //没工资选项,所以加一起
   from emp 
       order by earing desc ,hiredate asc;
select  job,
    count(1) as cnt
   from emp 
      where 
       sal > 1500
       group by job;
            selsct ename from emp
                where job = 'SALESMAN';
    select  * from emp 
    where ename like "S%"

    union all

    select  *  from emp 
    where ename like "%S"

    union all

    select  *  from emp
    where ename like "%S%"

    union all

    select  *   from emp 
    where ename like "_L%";

 第二种写法

   select *  from emp where  ename like "S%"
   or  ename like "%S%" or  ename like "_L%";

 

    select max(hisal) as max_hisal ,min(losal) as min_losal, count(1) as cnt,//统计人数
    from salgrade group by job;
select
  a.empno,
  a.ename,
  a.mgr,
  a.earings,
  b.ename,
  case 
  when a.earings >= 700 and a.earings<=1200 then 1 
  when a.earings >= 1201 and a.earings<=1400 then 2
  when a.earings >= 1401 and a.earings<=2000 then 3 
  when a.earings >= 2001 and a.earings<=3000 then 4
  when a.earings >= 3001 and a.earings<=9999 then 5 
  end as earings_level
from
  (
    select
      empno,
      ename,
      mgr,
      (sal + ifnull(comm, 0)) as earings
    from
      emp
    where
      sal > (
        select
          avg(sal) as avg_sal
        from
          emp
      )
  ) a
  left join emp as b on a.mgr = b.empno
select 
deptno,
avg(sal) as avg_sal 
from emp 
group by 
deptno; 

select 
ename,
sal,
deptno,
avg_sal
from 
(
	select 
	ename,
	sal,
	deptno
	from emp 
) as a left join 
(
	 select
          deptno,
          avg(sal) as avg_sal
        from
          emp
        group by
          deptno
) as b 
on a.deptno = b.deptno

 

 

 

 

 

 

 

 

 

 

举报

相关推荐

Day3

Python Day3

QT DAY3

Qt(day3)

Leetcode Day3

PAT DAY3

JS DAY3

【C语言】day3

0 条评论