表结构
--部门表
create table dept
(
deptno int(3) primary key,
dname varchar(14),
loc varchar(13)
);
--雇员表
create table emp
(
empno int(4) not null primary key,
ename varchar(10),
job varchar(10),
mgr int(4),
hiredate datetime,
sal double,
comm double,
deptno int(3),
foreign key(deptno) references dept(deptno)
);
--工资级别表
create table salgrade
(
grade int(3) primary key,
losal int(3),
hisal int(3)
);
2.插入数据(进行初始化)
use company;
--往部门表中查数据
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');
insert into dept values(50,'Admin','Washing');
--往雇员表中插数据
insert into emp values(7369,'Smith','Clerk',7902,'1980-12-17',800,0,20);
insert into emp values(7499,'Allen','Salesman',7698,'1981-2-20',1600,300,30);
insert into emp values(7844,'Turner','Salesman',7499,'1981-9-8',1500,0,30);
insert into emp values(7698,'Tom','Manager',0,'1981-9-8',6100,600,40);
insert into emp values(7876,'Adams','Clerk',7900,'1987-5-23',1100,0,20);
insert into emp values(7900,'James','Clerk',7698,'1981-12-3',2400,0,30);
insert into emp values(7902,'Ford','Analyst',7698,'1981-12-3',3000,null,20);
insert into emp values(7901,'Kik','Clerk',7900,'1981-12-3',1900,0,30);
--往工资级别表中插数据
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,5000);
insert into salgrade values(6,5001,10000);
1.查询雇员表中工资最高的雇员的员工号、员工姓名、工资和部门号。
SELECT
empno,
ename,
sal,
deptno
FROM emp
WHERE sal = (SELECT max(sal) FROM emp);
2.薪水大于1200的雇员,按照部门编号进行分组,分组后的平均薪水必须大于1500,查询各分组的平均工资,按照工资的倒序进行排列。
SELECT
avg(sal) avg_sal,
deptno
FROM
emp
WHERE
sal > 1200
GROUP BY
deptno
HAVING
avg_sal > 1500
ORDER BY
avg_sal DESC;
3.查询每个雇员和其所在的部门名
SELECT
ename,dname
FROM
emp,dept
WHERE(emp.deptno = dept.deptno);
-- 或者(推荐)(on中就写连接条件,where中就写过滤条件,各司其职)
SELECT
ename,
dname
FROM
emp
JOIN dept ON (emp.deptno = dept.deptno);
4.查询每个雇员姓名及其工资所在的等级
SELECT
ename,
grade
FROM
emp e
JOIN salgrade s ON (
e.sal BETWEEN s.losal
AND s.hisal
);
5.查询雇员名第2个字母不是a的雇员的姓名、所在的组名、工资所在的等级。
SELECT
ename,
dname,
grade
FROM
emp e
JOIN dept d ON (e.deptno = d.deptno)
JOIN salgrade s ON (
e.sal BETWEEN s.losal
AND s.hisal
)
WHERE
ename NOT LIKE '_a%';
6.查询每个雇员和其经理的姓名
SELECT
e1.ename,
e2.ename
FROM
emp e1,
emp e2
WHERE
(e1.mgr = e2.empno);
SELECT
e1.ename,
e2.ename
FROM
emp e1
JOIN emp e2 ON (e1.mgr = e2.empno);
7.查询每个雇员和其经理的姓名(包括公司老板本身(他上面没有经理))
SELECT
e1.ename,
e2.ename
FROM
emp e1
LEFT JOIN emp e2 ON (e1.mgr = e2.empno);
8.查询每个雇员的姓名及其所在部门的部门名(包括没有雇员的部门)
SELECT
ename,
dname
FROM
emp e
RIGHT JOIN dept d ON (e.deptno = d.deptno);
9.查询每个部门中工资最高的人的姓名、薪水和部门编号
-- 先求出每个部门中的最高工资:
select max(sal) max_sal,deptno from emp group by deptno
-- 在使用连接查询:
select ename,sal,e.deptno
from emp e join
(select max(sal) max_sal,deptno from emp group by deptno) t
on(e.sal = t.max_sal and e.deptno = t.deptno);
10.查询每个部门平均工资所在的等级
select deptno,avg_sal,grade from salgrade
join(select deptno,avg(sal) avg_sal from emp group by deptno) t
on(t.avg_sal between salgrade.losal and salgrade.hisal);
或者:
select deptno,avg_sal,grade from
(select deptno,avg(sal) avg_sal from emp group by deptno) t
join salgrade s on(t.avg_sal between s.losal and s.hisal);
11.查询每个部门内平均的薪水等级
先求每个人的薪水等级
select ename,deptno,grade from emp join salgrade s
on(emp.sal between s.losal and s.hisal);
再按组进行分组求平均
select deptno,avg(grade) from
(select ename,deptno,grade from emp join salgrade s
on(emp.sal between s.losal and s.hisal)) t
group by deptno;
12.查询雇员中有哪些人是经理人:
select ename from emp where empno in(select distinct mgr from emp);
或者:
select ename from emp join
(select distinct mgr from emp) t
on(emp.empno=t.mgr);
13.平均薪水最高的部门的部门编号
1:先求出每个部门的平均薪水和部门号(把这个看成一张表)
select avg(sal) avg_sal,deptno from emp group by deptno;
2:再求出平均薪水最高值(把这个看成一个值)
select max(avg_sal) from (select avg(sal) avg_sal,deptno from emp group by deptno) t;
3:对1表使用2条件进行查询即可
select avg_sal,deptno from
(select avg(sal) avg_sal,deptno from emp group by deptno) t1
where avg_sal=
(select max(avg_sal) from (select avg(sal) avg_sal,deptno from emp group by deptno) t2);
14.求平均薪水最高的部门的部门名称
select dname from dept where deptno =
( select deptno from
(select avg(sal) avg_sal,deptno from emp group by deptno) t1
where avg_sal=
(select max(avg_sal) from (select avg(sal) avg_sal,deptno from emp group by deptno) t2)
);
15.查询平均薪水的等级最低的部门名称
1.求平均薪水
select avg(sal) avg_sal,deptno from emp group by deptno;
2.求平均薪水的等级
select avg_sal,deptno,grade from
(select avg(sal) avg_sal,deptno from emp group by deptno) t
join salgrade s on(t.avg_sal between s.losal and hisal);
3.求平均薪水的等级最低的那个值
select min(grade) from
( select avg_sal,deptno,grade from
(select avg(sal) avg_sal,deptno from emp group by deptno) t
join salgrade s on(t.avg_sal between s.losal and hisal)
) t3;
4.平均薪水的等级最低的部门的部门编号(将2和3组合起来,将2看成要查询的表,将3看成查询条件)
select deptno from
( select avg_sal,deptno,grade from
(select avg(sal) avg_sal,deptno from emp group by deptno) t
join salgrade s on(t.avg_sal between s.losal and hisal)
) t2
where grade=
( select min(grade) from
( select avg_sal,deptno,grade from
(select avg(sal) avg_sal,deptno from emp group by deptno) t
join salgrade s on(t.avg_sal between s.losal and hisal)
) t3
);
5.平均薪水的等级最低的部门名称
select dname,deptno from dept where deptno in
( select deptno from
( select avg_sal,deptno,grade from
(select avg(sal) avg_sal,deptno from emp group by deptno) t
join salgrade s on(t.avg_sal between s.losal and hisal)
) t2
where grade=
( select min(grade) from
( select avg_sal,deptno,grade from
(select avg(sal) avg_sal,deptno from emp group by deptno) t
join salgrade s on(t.avg_sal between s.losal and hisal)
) t3
)
);