赞
踩
select * from EMP where (sal>500 or job='MANAGER') and ename like 'J%';
select * from EMP order by deptno, sal desc;
select ename, sal*12+ifnull(comm,0) as '年薪' from EMP order by 年薪 desc;
select ename, job from EMP where sal = (select max(sal) from EMP);
select ename, sal from EMP where sal>(select avg(sal) from EMP);
select deptno, format(avg(sal), 2) , max(sal) from EMP group by deptno;
select deptno, avg(sal) as avg_sal from EMP group by deptno having avg_sal<2000;
select job,count(*), format(avg(sal),2) from EMP group by job;
select EMP.ename, EMP.sal, DEPT.dname from EMP, DEPT where EMP.deptno = DEPT.deptno;
select ename, sal,dname from EMP, DEPT where EMP.deptno=DEPT.deptno and DEPT.deptno = 10;
select ename, sal, grade from EMP, SALGRADE where EMP.sal between losal and hisal;
select empno,ename from emp where emp.empno=(select mgr from emp where ename='FORD');
# from emp leader, emp worker,给自己的表起别名,因为要先做笛卡尔积,所以别名可以先识别
select leader.empno,leader.ename from emp leader, emp worker
where leader.empno = worker.mgr and worker.ename='FORD';
select * from EMP WHERE deptno = (select deptno from EMP where ename='smith');
select ename,job,sal,deptno from emp where job in (select distinct job from emp where deptno=10) and deptno<>10;
select * from emp where sal > (select max(sal) from emp where deptno=30);
select ename, sal, deptno from EMP where sal > all(select sal from EMP where deptno=30);
select ename, sal, deptno from EMP where sal > any(select sal from EMP where deptno=30);
# 以下两个写法结果相同,感受in的作用
select ename from EMP where (deptno, job)=(select deptno, job from EMP
where ename='SMITH') and ename <> 'SMITH';
select ename from EMP where (deptno, job) in (select deptno, job from EMP
where ename='SMITH') and ename <> 'SMITH';
# 获取各个部门的平均工资,将其看作临时表,再进行笛卡尔积筛选
select ename, deptno, sal, format(asal,2) from EMP, (select avg(sal) asal, deptno from EMP group by deptno) tmp
where EMP.sal > tmp.asal and EMP.deptno=tmp.deptno;
select EMP.ename, EMP.sal, EMP.deptno, ms from EMP, (select max(sal) ms, deptno from EMP group by deptno) tmp
where EMP.deptno=tmp.deptno and EMP.sal=tmp.ms;
select DEPT.dname, DEPT.deptno, DEPT.loc, count(*) '部门人数' from EMP, DEPT
where EMP.deptno=DEPT.deptno group by DEPT.deptno, DEPT.dname, DEPT.loc;
# 1. 对EMP表进行人员统计
select count(*), deptno from EMP group by deptno;
# 2. 将上面的表看作临时表
select DEPT.deptno, dname, mycnt, loc from DEPT, (select count(*) mycnt, deptno from EMP group by deptno) tmp where DEPT.deptno=tmp.deptno;
mysql> select ename, sal, job from EMP where sal>2500 union
-> select ename, sal, job from EMP where job='MANAGER';
+-------+---------+-----------+
| ename | sal | job |
+-------+---------+-----------+
| JONES | 2975.00 | MANAGER |
| BLAKE | 2850.00 | MANAGER |
| SCOTT | 3000.00 | ANALYST |
| KING | 5000.00 | PRESIDENT |
| FORD | 3000.00 | ANALYST |
| CLARK | 2450.00 | MANAGER |
+-------+---------+-----------+
mysql> select ename, sal, job from EMP where sal>2500 union all
-> select ename, sal, job from EMP where job='MANAGER';
+-------+---------+-----------+
| ename | sal | job |
+-------+---------+-----------+
| JONES | 2975.00 | MANAGER |
| BLAKE | 2850.00 | MANAGER |
| SCOTT | 3000.00 | ANALYST |
| KING | 5000.00 | PRESIDENT |
| FORD | 3000.00 | ANALYST |
| JONES | 2975.00 | MANAGER |
| BLAKE | 2850.00 | MANAGER |
| CLARK | 2450.00 | MANAGER |
+-------+---------+-----------+
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。