案例:查询工作岗位是MANAGER和SALESMAN的员工?
select ename,job from emp where job = ‘MANAGER’ or job = ‘SALESMAN’;
select ename,job from emp where job in(‘MANAGER’,‘SALESMAN’);
1 2 3 4
select ename,job from emp where job = 'MANAGER' union select ename,job from emp where job = 'SALESMAN';
select e.deptno, avg(e.sal) as ag from emp e join dept t on e.deptno = t.deptno groupby deptno orderby ag desc limit 1;
select d.dname, s.grade, t.* from (select e.deptno as dd, avg(e.sal) as ag from emp e groupby dd) t join dept d on t.dd = d.deptno join salgrade s on t.ag between s.losal and s.hisal;
9. select ename ,sal from emp orderby sal desc limit 5,5; 11. select ename, hiredate from emp orderby hiredate desc limit 5;
12. select e.ename, e.sal, s.grade from emp e join salgrade s on e.sal between s.losal and s.hisal;
select s.grade ,count(*) from emp e join salgrade s on e.sal between s.losal and s.hisal groupby s.grade;
13、面试题: 有 3 个表 S(学生表),C(课程表),SC(学生选课表) S(SNO,SNAME)代表(学号,姓名) C(CNO,CNAME,CTEACHER)代表(课号,课名,教师) SC(SNO,CNO,SCGRADE)代表(学号,课号,成绩) 问题: 1,找出没选过“黎明”老师的所有学生姓名。 select sname from s where sno notin ( select sno from sc where cno in( select cno from c where cteacher ='黎明' ) ); 2,列出 2 门以上(含2 门)不及格学生姓名及平均成绩。 select sname, avg(scgrade) from s join sc on s.sno = sc.sno where scgrade <60 ; groupby s.sno havingcount(*) >=2; 3,即学过 1 号课程又学过 2 号课所有学生的姓名。
select sname from s join sc s1 on s.sno = s1.sno join sc s2 on s.son = s2.sno where s1.cno ='1'and s2.cno ='2';
14. select a.ename , b.ename from emp a leftjoin emp b on a.mgr = b. empno; 15. select e.empno, e.ename '员工名字', e.hiredate, b.ename '领导名字', b.hiredate, d.dname from emp e join emp b on e.mgr = b.empno and e.hiredate < b.hiredate join dept d on d.deptno = e.deptno;