select d.dname,d.deptno from dept d left join (select e.deptno,count() from emp e group by e.deptno having count() >= 1) new on d.deptno in (new.deptno);
select * from emp e where e.sal > (select e1.sal from emp e1 where e1.ename = ‘smith’);
select e.ename 员工名,m.ename 上级名 from emp e left join emp m on e.mgr = m.empno;
select e.empno 编号,e.ename 姓名,d.dname 部门名,e.hiredate,m.ename,m.hiredate from emp e left join dept d on e.deptno = d.deptno left join emp m on e.mgr = m.empno where e.hiredate < m.hiredate;
select * from dept d left join emp e on d.deptno = e.deptno;
select jobandsal.,jobandnum.numofjob from ( select min(e.sal) minsal,e.job from emp e group by e.job having minsal > 1500) jobandsal left join ( select count() numofjob,e.job from emp e group by e.job) jobandnum on jobandsal.job = jobandnum.job;
select e.ename,e.deptno,newd.dname from emp e left join (select d.deptno,d.dname from dept d where d.dname = ‘sales’) newd on e.deptno = newd.deptno where e.deptno = newd.deptno;
select e2.ename,e2.empno,e2.mgr,d.DNAME,e3.ename,e2.sal,ssal.GRADE from emp e2 left join dept d on e2.deptno = d.deptno left join emp e3 on e2.mgr = e3.empno left join salgrade ssal on e2.sal between ssal.LOSAL and ssal.HISAL where e2.sal > (select avg(e1.sal) avgsal from emp e1);
select e2.*,d.dname from emp e2 left join dept d on e2.deptno = d.deptno where e2.job = (select e1.job from emp e1 where e1.ename = ‘scott’);
select e2.ename,e2.sal from emp e2 where e2.sal in (select e1.sal from emp e1 where e1.deptno = 30);
select newe.ename,newe.sal,d.DNAME from (select * from emp e2 where e2.deptno != 30) newe left join dept d on newe.deptno = d.deptno where newe.sal > (select max(e1.sal) from emp e1 where e1.deptno = 30);
select new1.,new.service_year from (select e2.DEPTNO,avg(e2.SAL), count()from emp e2 GROUP BY e2.DEPTNO) new1 left join (select e.DEPTNO,avg(TIMESTAMPdiff(year,e.HIREDATE,CURRENT_DATE)) service_year from emp e GROUP BY e.DEPTNO) new on new1.DEPTNO = new.DEPTNO ;
select e1.ename,e1.deptno,d.dname,avgsal.avsal from emp e1 left join dept d on e1.deptno = d.deptno left join (select e2.deptno,avg(sal) avsal from emp e2 group by e2.deptno) avgsal on e1.deptno = avgsal.deptno;
select d.* ,new.num from dept d left join (select e.deptno,count(*) num from emp e group by e.deptno) new on d.deptno = new.deptno;
select e2.ename,e2.job,new.最低工资 from emp e2 join (select min(e1.sal) 最低工资,e1.job from emp e1 group by e1.job) new on e2.job = new.job and e2.sal = new.最低工资;
select min(e.sal) from emp e where e.job = ‘manager’;
select e.*,e.sal *12 as 年总收入 from emp e order by 年总收入;
select e2.* from emp e2 where e2.empno in (select distinct(e1.mgr) from emp e1) and e2.sal > 3000;
select d.dname,d.deptno,sum(e.sal) 月薪合计,count(*) 部门人数
from emp e
right join dept d
on e.deptno = d.deptno
where d.dname like ‘%s%’
group by d.dname,d.deptno;
select avg(e.sal) 部门平均月薪,d.dname from emp e inner join dept d on e.deptno = d.deptno group by e.deptno having 部门平均月薪 = (select max(部门平均月薪a ) 部门最大平均月薪 from (select avg(e.sal) 部门平均月薪a ,d.dname from emp e inner join dept d on e.deptno = d.deptno group by e.deptno) new);
简便写法:不严格
select max(temp.avgsal),d.dname
from (select avg(sal) avgsal,deptno from emp group by deptno) temp
join dept d on temp.deptno = d.deptno;
写法2,使用分页查询获得
select temp.avgsal,d.dname
from (select avg(sal) avgsal,deptno from emp group by deptno order by avgsal desc limit 1) temp
join dept d on temp.deptno = d.deptno;