子语句查询列名
例:
--按部门统计各部门员工人数,要求显示部门号,部门名,人数,并按照降序排列
select
d.deptno,count(e.empno),d.dname
from emp e,dept d
where e.deptno=d.deptno
group by d.deptno,d.dname
order by count(e.empno) desc
;
运行显示:不是GROUP BY表达式
原因:
select子语句后面出现的列名要么出现在聚合函数中,要么出现在group by后
正确方式:将d.dname放到group by后
--按部门统计各部门员工人数,要求显示部门号,部门名,人数,并按照降序排列
select
d.deptno,count(e.empno)
from emp e,dept d
where e.deptno=d.deptno
group by d.deptno,d.dname
order by count(e.empno) desc
;
ANY和ALL
例:
--查询工资比20号部门【任意any】一个员工工资【低<】的员工信息(多行子查询,使用any关键字)
select sal from emp where deptno=20;
select *
from emp
where
sal < any
(
select sal from emp where deptno=20
);
ANY:在oracle中代表小于最大的
ALL:在oracle中代表小于最小的
查询工资比20号部门【所有all】员工【低<】的员工信息(多行子查询,使用all关键字)
select *
from emp
where
sal < all
(
select sal from emp where deptno=20
);
AND和“ ,”
例:
--查询每个员工编号,姓名,部门名,工资等级
select
e.empno,e.ename,d.dname,s.grade
from emp e,salgrade s,dept d
where e.sal between s.losal and s.hisal,e.deptno=d.deptno
;
运行显示:不是GROUP BY表达式
原因:where语句中连接条件不能使用“,”,应使用and
--查询每个员工编号,姓名,部门名,工资等级
select
e.empno,e.ename,d.dname,s.grade
from emp e,salgrade s,dept d
where e.sal between s.losal and s.hisal and e.deptno=d.deptno
;