9道SQL练习题

--部门表
dept部门表(deptno部门编号/dname部门名称/loc地点)

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');

--工资等级表
salgrade工资等级表(grade 等级/losal此等级的最低/hisal此等级的最高)

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);


--员工表
emp员工表(empno员工号/ename员工姓名/job工作/mgr上级编号/hiredate受雇日期/sal薪金/comm佣金/deptno部门编号)
工资 = 薪金 + 佣金

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 dept;


 

select * from salgrade;

select * from emp;

 

 

 

#1.查询出部门编号为30的所有员工的编号和姓名

select empno,ename,deptno from emp where deptno = 30;

 

#2.找出部门编号为10中所有经理,和部门编号为20中所有销售员的详细资料

//  经理:MANAGER ,销售员:SALESMAN

select * from emp where deptno =10 and job = 'MANAGER'
union
select * from emp where deptno= 20 and job = 'SALESMAN';

 

select * from emp where (deptno =10 and job = 'MANAGER') or (deptno= 20 and job = 'SALESMAN');

 

#3.查询所有员工详细信息,用工资降序排序,如果工资相同使用入职日期升序排序 //工资 = 薪金 + 佣金

select * from emp order by (
sal + ifnull(comm,0)
)
desc ,hiredate asc ;


##易错点:null字段计算 null+任意字段=null

 

#4.列出薪金大于1500的各种工作及从事此工作的员工人数。

select c.job,count(c.job) from (  
     select * from emp where sal > 1500
)as c
group by c.job;

 

select job,count(job) from emp where sal > 1500 group by job; 


 #为了避坑原题目是:列出最低薪金大于1500的各种工作及从事此工作的员工人数。

select job,count(job) from emp group by job having min(sal)>1500;

 

 

#5.列出在销售部工作的员工的姓名,假定不知道销售部的部门编号。
 

select  ename from emp where deptno=(
select deptno from dept where dname = 'SALES');


#6.查询姓名以S开头的\以S结尾\包含S字符\第二个字母为L  __
 

select ename from emp where ename like 's%';


 

select ename from emp where ename like '%s';


 

select ename from emp where ename like '%s%';

select ename from emp where ename like '_L%';

 

#7.查询每种工作的最高工资、最低工资、人数
select job,max(sal+ifnull(comm,0)) from emp  group by job; 
select job,min(sal+ifnull(comm,0)) from emp  group by job;
select job,count(job) from emp group by job;

select job ,
max(sal+ifnull(comm,0)) as maxpay,
min(sal+ifnull(comm,0)) as minpay,
count(job) as cnum
from emp group by job;



###注意添加别名,标准

 

#8.列出薪金高于公司平均薪金的所有员工号,员工姓名,所在部门名称,上级领导,工资,工资等级
 

select  e.empno, 
e.ename,
e.deptno,d.dname,
e.mgr,m.ename,
e.sal+ifnull(e.comm,0) as pay,s.grade
from emp as e
left join dept as d on e.deptno=d.deptno
left join emp as m on e.mgr=m.empno
left join salgrade as s on (e.sal+ifnull(e.comm,0)) between s.losal and s.hisal
where 
(e.sal > (select avg(sal) from emp));


#9.列出薪金  高于  在部门30工作的  所有/任何一个员工的薪金的员工姓名和薪金、部门名称。

select e.ename,
e.sal,
d.dname
from emp as e
left join dept as d on e.deptno=d.deptno
where
e.sal>(select max(sal) from emp where deptno = '30');

 

select e.ename,
e.sal,
e.deptno 
from emp as e,dept as d
where e.deptno =d.deptno
and (e.sal > all(select sal from emp where deptno = '30'));

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值