希望高手能解释的详细一点,谢谢!
...emp where deptno in (select deptno from dept group by deptno...
1.首先sql语句从右往左执行。所以先执行select deptno from emp。查询雇员表所有部门编号。2.in代表在...的范围。3.再执行select deptno from dept。查询部门表中所有部门编号。4.总括:查询部门表中的部门编号且在雇员表中的部门编号的范围内。
查询拥有最多的员工的部门的基本信息,如果有多个部
select from dept where deptno in (select deptno from emp group by deptno having count(*) >= ALL (select count(*)from emp group by deptno))
select deptno from emp 问题
select deptno from emp group by deptno having count(*)>1group by 是分组的意思, select deptno from emp group by deptno 这句话的意思是,根据deptuno也就是部门编号分组查询 查询出来所有的部门编号 having count(*)>1 是一个限制条件,在查询出来的结果集的基础之上,找到部门编号数量大于1...
表dept的字段:DEPTNO(部门编号),EMPNO(员工编号),DEPTNO有重复的记录...
select count(*),DEPTNO from dept group by DEPTNO having count(*)>1 delete from dept where EMPNO not in(select max(EMPNO ) from dept where DEPTNO in(select DEPTNO from (select count(*),DEPTNO from dept group by DEPTNO having count(*)>1)))...
SQL中IN和EXISTS用法的区别
(低效)SELECT * FROM EMP (基础表) WHERE EMPNO > 0 AND DEPTNO IN(SELECT DEPTNO FROM DEPT WHERE LOC = ‘MELB’)12.用EXISTS替换DISTINCT 当提交一个包含一对多表信息(比如部门表和雇员表)的查询时,避免在SELECT子句中使用DISTINCT. 一般可以考虑用EXIST替换, EXISTS 使查询更为迅速,因为RDBMS...
in 和 exists在查询效率的问题
WHERE EMPNO > 0 AND DEPTNO IN (SELECT DEPTNO FROM DEPT WHERE LOC = ‘MELB’)高效:SELECT * FROM EMP (基础表)WHERE EMPNO > 0 AND EXISTS (SELECT ‘X’FROM DEPT WHERE DEPT.DEPTNO = EMP.DEPTNO AND LOC = ‘MELB’)在子查询中,NOT IN子句将执行一个内部的排序和合并,对子...
SQL中SELECT中的FROM子句可否带另外一个SELECT
select ename,deptno,sal from emp where deptno=(select deptno from dept where loc='NEW YORK');例2:select子查询出现在from子句中 SELECT ename,job,sal,rownum FROM (SELECT ename,job,sal FROM EMP ORDER BY sal);例3:select子查询出现在select list中,作为一个字段值来返回 SELECT ...
mysql复杂查询--多表查询
select * from emp where deptno=(select deptno from emp where ename='smith');多行子查询指返回多行数据的子查询 使用关键字 in 如果我们的一个子查询,返回的 结果是多列,就叫做列子查询 合并多个select语句的结果,可以使用集合操作符 union,union all ...
普通sql嵌套SELECT语句
SELECT ename,deptno,sal FROM emp WHERE deptno=(SELECT deptno FROM dept WHERE loc='NEW YORK');多行子查询返回多行数据,使用IN、ALL、ANY比较。例:查询选修Rona老师任意课程的学生名;查询所有部门编号为A的资料;查询成绩高于Kaka所有成绩的学生名。多列子查询同样采用IN、ALL、ANY比较,返回多...
SQL中EXISTS怎么用
是判断是否存在,和in类似,但效率要比in高 SELECT FROM EMP (基础表)WHERE EMPNO > 0 AND EXISTS (SELECT ‘X'FROM DEPT WHERE DEPT.DEPTNO = EMP.DEPTNO AND LOC = ‘MELB')SELECT FROM EMP (基础表)WHERE EMPNO > 0 AND DEPTNO IN(SELECT DEPTNO FROM DEPT WHERE LOC = ‘MELB')这两句...