# (1)工作岗位为“CLERK”的信息 select d.deptno,d.dname,e.ename from emp e join dept d on e.deptno=d.deptno where e.job='CLERK'; +--------+------------+--------+ | deptno | dname | ename | +--------+------------+--------+ |20| RESEARCH | SMITH | |20| RESEARCH | ADAMS | |30| SALES | JAMES | |10| ACCOUNTING | MILLER | +--------+------------+--------+
# (2)每个部门的员工数量 select e.deptno,count(e.ename) as totalEmp from emp e groupby e.deptno; +--------+----------+ | deptno | totalEmp | +--------+----------+ |20|5| |30|6| |10|3| +--------+----------+
# (3)两张表连接 select t.deptno,t.dname,t.ename,f.totalEmp from (select d.deptno,d.dname,e.ename from emp e join dept d on e.deptno=d.deptno where e.job='CLERK') t join (select e.deptno,count(e.ename) as totalEmp from emp e groupby e.deptno) f on t.deptno=f.deptno; +--------+------------+--------+----------+ | deptno | dname | ename | totalEmp | +--------+------------+--------+----------+ |20| RESEARCH | SMITH |5| |20| RESEARCH | ADAMS |5| |30| SALES | JAMES |6| |10| ACCOUNTING | MILLER |3| +--------+------------+--------+----------+
# (1)部门名为"SALES"的部门编号 select deptno from dept where dname="SALES"; +--------+ | deptno | +--------+ |30| +--------+
# (2)查询员工姓名 select ename from emp e where deptno=(select deptno from dept where dname="SALES"); +--------+ | ename | +--------+ | ALLEN | | WARD | | MARTIN | | BLAKE | | TURNER | | JAMES | +--------+
# (2)第一个连接:部门名字 # 第二个左连接:emp表看成两张表,列出员工对应的上级领导 # 第三个连接:工资等级 select d.dname,e.ename,b.ename as leadername,grade from emp e join dept d on e.deptno=d.deptno leftjoin emp b on e.mgr=b.empno join salgrade s on e.sal between s.losal and s.hisal where e.sal>(selectavg(sal) avgsal from emp); +------------+-------+------------+-------+ | dname | ename | leadername | grade | +------------+-------+------------+-------+ | RESEARCH | FORD | JONES |4| | RESEARCH | SCOTT | JONES |4| | ACCOUNTING | CLARK | KING |4| | SALES | BLAKE | KING |4| | RESEARCH | JONES | KING |4| | ACCOUNTING | KING |NULL|5| +------------+-------+------------+-------+
# (1)查询出“SCOTT”的工作岗位 select job from emp where ename="SCOTT"; +---------+ | job | +---------+ | ANALYST | +---------+
# (2) select d.dname,e.* from emp e join dept d on e.deptno=d.deptno where e.job=(select job from emp where ename="SCOTT"); +----------+-------+-------+---------+------+------------+---------+------+--------+ | dname | EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO | +----------+-------+-------+---------+------+------------+---------+------+--------+ | RESEARCH |7788| SCOTT | ANALYST |7566|1987-04-19|3000.00|NULL|20| | RESEARCH |7902| FORD | ANALYST |7566|1981-12-03|3000.00|NULL|20| +----------+-------+-------+---------+------+------------+---------+------+--------+
列出薪资等于部门30中工作的员工的薪资的其他员工的姓名和薪资
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# (1)部门30中工作的员工的薪资 selectdistinct sal from emp where deptno=30; +---------+ | sal | +---------+ |1600.00| |1250.00| |2850.00| |1500.00| |950.00| +---------+
# (2)薪资与以上的相同,排除部门30 select ename,sal from emp where sal in (selectdistinct sal from emp where deptno=30) and deptno !=30; Emptyset (0.00 sec)
# (1)部门30中的员工最高工资 selectmax(sal) from emp where deptno=30; +----------+ |max(sal) | +----------+ |2850.00| +----------+
# (2)高于最高工资的其他员工 select deptno,ename,sal from emp where sal>(selectmax(sal) from emp where deptno=30) and deptno!=30; +--------+-------+---------+ | deptno | ename | sal | +--------+-------+---------+ |20| JONES |2975.00| |20| SCOTT |3000.00| |10| KING |5000.00| |20| FORD |3000.00| +--------+-------+---------+
# (3)连接dept表 select d.dname,t.ename,t.sal from (select deptno,ename,sal from emp where sal>(selectmax(sal) from emp where deptno=30) and deptno!=30) t join dept d on t.deptno=d.deptno; +------------+-------+---------+ | dname | ename | sal | +------------+-------+---------+ | RESEARCH | JONES |2975.00| | RESEARCH | SCOTT |3000.00| | ACCOUNTING | KING |5000.00| | RESEARCH | FORD |3000.00| +------------+-------+---------+
# 右连接:员工的姓名、部门名称和工资 select d.dname,e.ename,e.sal from emp e rightjoin dept d on e.deptno=d.deptno; +------------+--------+---------+ | dname | ename | sal | +------------+--------+---------+ | ACCOUNTING | MILLER |1300.00| | ACCOUNTING | KING |5000.00| | ACCOUNTING | CLARK |2450.00| | RESEARCH | FORD |3000.00| | RESEARCH | ADAMS |1100.00| | RESEARCH | SCOTT |3000.00| | RESEARCH | JONES |2975.00| | RESEARCH | SMITH |800.00| | SALES | JAMES |950.00| | SALES | TURNER |1500.00| | SALES | BLAKE |2850.00| | SALES | MARTIN |1250.00| | SALES | WARD |1250.00| | SALES | ALLEN |1600.00| | OPERATIONS |NULL|NULL| +------------+--------+---------+
列出所有部门的详细信息和人数
1 2 3 4 5 6 7 8 9 10 11 12 13
select d.deptno,d.dname,d.loc,count(ename) totalename from emp e rightjoin dept d on d.deptno=e.deptno groupby deptno; +--------+------------+----------+------------+ | deptno | dname | loc | totalename | +--------+------------+----------+------------+ |10| ACCOUNTING |NEW YORK |3| |20| RESEARCH | DALLAS |5| |30| SALES | CHICAGO |6| |40| OPERATIONS | BOSTON |0| +--------+------------+----------+------------+
# (2)匹配每个员工姓名 select e.ename,e.job,e.sal from emp e join (select e.job,min(e.sal) minsal from emp e groupby e.job) t on e.job=t.job where e.sal=t.minsal; +--------+-----------+---------+ | ename | job | sal | +--------+-----------+---------+ | SMITH | CLERK |800.00| | WARD | SALESMAN |1250.00| | MARTIN | SALESMAN |1250.00| | CLARK | MANAGER |2450.00| | SCOTT | ANALYST |3000.00| | KING | PRESIDENT |5000.00| | FORD | ANALYST |3000.00| +--------+-----------+---------+
列出各个部门MANAGER的最低薪资
1 2 3 4 5 6 7 8 9 10 11 12
# (1) 匹配工作岗位中的MANAGER select e.deptno,min(sal) minsal from emp e where e.job="MANAGER" groupby e.deptno; +--------+---------+ | deptno | minsal | +--------+---------+ |20|2975.00| |30|2850.00| |10|2450.00| +--------+---------+
select ename,(sal+ifnull(comm,0))*12as yearsal from emp orderby yearsal asc; +--------+----------+ | ename | yearsal | +--------+----------+ | SMITH |9600.00| | JAMES |11400.00| | ADAMS |13200.00| | MILLER |15600.00| | TURNER |18000.00| | WARD |21000.00| | ALLEN |22800.00| | CLARK |29400.00| | MARTIN |31800.00| | BLAKE |34200.00| | JONES |35700.00| | SCOTT |36000.00| | FORD |36000.00| | KING |60000.00| +--------+----------+
列出员工领导的薪资超过3000的员工名称和领导名称
1 2 3 4 5 6 7 8 9 10 11 12
select e.ename,b.ename as leadername from emp e join emp b on e.mgr=b.empno where b.sal>3000; +-------+------------+ | ename | leadername | +-------+------------+ | JONES | KING | | BLAKE | KING | | CLARK | KING | +-------+------------+
列出部门名称中带“S”字符的部门员工的工资合计、部门人数
1 2 3 4 5 6 7 8 9 10 11 12
select d.dname,sum(e.sal) sumavg,count(e.ename) totalemp from emp e join dept d on e.deptno=d.deptno where d.dname like'%s%' groupby d.dname; +----------+----------+----------+ | dname | sumavg | totalemp | +----------+----------+----------+ | RESEARCH |10875.00|5| | SALES |9400.00|6| +----------+----------+----------+