数据库的多表连接查询 emp表,dept表,salgrade表

Posted 三里清风33

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库的多表连接查询 emp表,dept表,salgrade表相关的知识,希望对你有一定的参考价值。

数据库的多表连接查询 emp表,dept表,salgrade表

emp表,dept表,salgrade表 相关表

#创建dept表
CREATE table dept(
DEPTON int(2) not NULL,
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');
#创建emp表
CREATE TABLE emp(
empno int(4) not null,
ename varchar(10),
job varchar(9),
mgr int(4),
hiredate date,
sal DECIMAL(7,2),
comm DECIMAL(7,2),
deptno int(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','MANNAGER',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','MANNAGER',7839,'1981-05-01',2850,NULL,30);
INSERT into emp values(7782,'CLARK','MANNAGER',7839,'1981-06-09',2450,NULL,10);
INSERT into emp values(7788,'SCOTT','ANALYST',7566,'1987-07-13',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,'1987-07-13',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-13',1300,NULL,10);
#创建salgrade表
CREATE TABLE salgrade(
GRADE int(11) not NULL,
LOSAL VARCHAR(14),
HISAL VARCHAR(13)
)
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表

dept表

salgrade表

#1.列出至少有三个员工的所有部门和部门信息。
//第一步先查出至少有三个员工的部门,然后通过查出来的结果当成一个新表与dept表建立关系,查出结果
SELECT deptno,dname,loc  from  dept d ,(SELECT deptno,count(*) from emp GROUP BY deptno HAVING  count(*)>=3) e WHERE d.depton=e.deptno  
#2.列出所有员工的姓名及其上级的姓名。
//通过隐式内连接查找
 SELECT e1.ename,e2.ename  from emp e1,emp e2 WHERE e1.mgr=e2.empno;
#3.列出受雇日期早于直接上级的所有员工的编号,姓名,部门名称
SELECT  e3.e4,e3.e5,e3.e7,dname FROM dept d,
(SELECT e1.empno e4,e1.ename e5,e2.ename e6,e1.deptno e7  from emp e1,emp e2 
WHERE e1.mgr=e2.empno and e1.hiredate<e2.hiredate) e3
WHERE d.depton=e3.e7;
#4.列出部门名称和这些部门员工的信息,同时列出那些没有员工的部门。
SELECT * from emp e RIGHT JOIN dept d on e.deptno=d.DEPTON 
#5.列出“CLERK”的姓名和部门名称,部门人数:
SELECT ename,deptno,dname,d2.sum from  dept d1,(SELECT e.ename,e.deptno,d.sum FROM 
(SELECT ename,deptno from emp WHERE job='clerk') e,
(SELECT deptno,COUNT(*) sum from emp GROUP BY deptno) d WHERE e.deptno=d.deptno) d2
WHERE  d1.depton=d2.deptno
#6.列出最低薪金大于1500的各种工作以及从事此工作的全部雇员人数
SELECT job,count(*) from emp GROUP BY job HAVING min(sal)>1500
#7.列出在部门销售部工作的员工姓名,假设不知道销售部的部门编号
SELECT ename from emp WHERE deptno=(SELECT depton FROM dept WHERE dname='sales')
#8.列出薪金高于工资平均薪金的所有员工,所在部门,上级领导,公司的工资等级。
SELECT e6.e3,e6.e4,e6.e5,e6.e7,s.grade  FROM 
(SELECT e1.ename e3,e1.deptno e4,e2.ename e5,e1.sal e7 FROM emp e1,emp e2 
WHERE e1.sal>(SELECT avg(sal) FROM emp )
AND e1.mgr=e2.empno) e6,salgrade s
where e6.e7 BETWEEN s.losal and hisal
#9.列出和“SCOTT”从事相同工作的所有员工及部门名称:
SELECT e.ename,e.deptno,d.dname   from  (SELECT ename,deptno FROM emp WHERE job=(SELECT job from emp where ename='scott')) e,
dept d WHERE e.deptno=d.depton
#10.列出薪金等于部门30中员工薪金的所有员工的姓名和薪金
SELECT e.ename,e.sal from emp e WHERE sal in(SELECT sal from emp WHERE deptno=30)
#11.列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金、部门名称
SELECT ename,sal,deptno from emp WHERE sal>(SELECT MAX(sal) from emp WHERE deptno=30 )
#12.列出所有部门的详细信息和部门人数
SELECT e.deptno,e.sum,d.dname FROM (SELECT deptno,COUNT(*) sum from emp GROUP BY deptno) e, dept d WHERE e.deptno=d.depton
#13、列出各种工作的最低工资以及从事此工作的雇员姓名:
SELECT ename,sal FROM emp WHERE sal in(SELECT MIN(sal) from emp GROUP BY job)
#14、列出各个部门的MANAGER 的最低薪金:
SELECT deptno,min(sal) FROM emp WHERE job='manager' GROUP BY deptno 
#15、列出所有员工的年工资,按照年薪从低到高排序:
SELECT ename,sal*12+IFNULL(comm,0)*12 salall from emp ORDER BY salall
#16、查询出某个员工的上级主管,并要求这些主管中的薪水超过3000
SELECT e1.ename,e2.ename from emp e1,emp e2 WHERE e1.mgr=e2.empno  and e2.sal>3000
#17、求出部门名称中带有’S‘字符的部门员工的工资合计,部门人数
select dname,ifnull(sum_sal,0) '工资合计',ifnull(人数,0) '人数' from dept LEFT JOIN 
(select deptno, sum(sal) 'sum_sal',count(*) '人数' 
from emp GROUP BY deptno) a on a.DEPTNO = dept.DEPTON where dname like '%s%'
#18、给任职日期超过10年的人加薪10%;
select ename,if(year(now())-year(hiredate)>39,sal*1.1,sal) from emp





以上是关于数据库的多表连接查询 emp表,dept表,salgrade表的主要内容,如果未能解决你的问题,请参考以下文章

补12.关于mysql的多表查询

多表查询(重点)

oracle的多表查询与表的连接

Oracle系列:(13)多表查询

oracle多表查询

Oracle --- 多表查询和子查询