having 和where的区别

xiaoxiao2021-02-28  60

在查询过程中聚合语句(sum,min,max,avg,count)要比having子句优先执行.而where子句在查询过程中执行优先级别优先于聚合语句(sum,min,max,avg,count)。

--having子句,对分组查询的结果进行过滤   --查询部门号大于10的不同部门的不同工作岗位的人数   select deptno,job, count(*) from emp where deptno > 10 group by deptno,job;--from -where-group by   select deptno,job, count(*) from emp group by deptno,job having deptno >10;--from -group by-having   --当where和having都可以使用的时候,这时优先使用where,where执行效率高     --查询不同部门的不同工作岗位的人数并且人数大于1的信息     --多行函数必须先分组再过滤    select deptno,job, count(*) from emp group by deptno,job having  count(*) >1;      --统计人数小于4的部门的平均工资。    select avg(sal), count(*)      from emp     group by deptno         --分组    having count(*) < 6      --过滤     order by avg(sal) desc; --排序   --统计各部门的最高工资,排除最高工资小于3000的部门。    select max(sal)      from emp     group by deptno    having max(sal) >= 3000     order by max(sal) desc;

--查询条件where子句 使用算术表达式 = ,< , > ,>= ,<= ,<> --查看工资等于1250的员工信息 select * from emp where sal = 1250; --查看工作是CLERK的员工信息,注意大小写 select * from emp where job = 'clerk';--查询的值是区分大小写     SELECT * from emp where job = 'CLERK';--关键字不区分大小写 --查看工资大于1250的员工姓名和工作 select ename,job,sal from emp  where sal>1250 order by sal ;--order by关键字放到where后面 --查看工资大于等于2000的员工信息 select * from emp  where sal>=1250; --查看工资小于等于2000的员工信息; select * from emp  where sal<=1250; --查看工资不等于1500的员工信息 select * from emp where sal <> 1250; --查看入职日期在1981年后的员工信息     select * from emp where hiredate > '31-12月-1981';-- 日期默认的格式 日月年  --where子句使用关键字  and,between, or, in, like ,is null ,is not null --查询工资在2000-3000(包括2000和3000)之间的员工信息     select * from emp where sal>=2000 and sal<=3000; --and     select * from emp where sal between 2000 and 3000;--between and   --查询工作为SALESMAN,ANALYST,MANAGER的员工信息     select * from emp where job='SALESMAN' or job='ANALYST' or  job='MANAGER'; --or     select * from emp where job in('SALESMAN','ANALYST','MANAGER'); --in   --查询姓名中包含s的,以s开头的,以s结尾的,第二个字符为A的,包含下划线的名字。     select * from emp where ename like '%S%';--%代表一到多个任意的字符     select * from emp where ename like 'S%'; --S开头     select * from emp where ename like '%S';--S结尾     select * from emp where ename like '_A%';--下划线代表任意一个字符            --查询名字中包含下划线的用户信息      select * from emp where ename like '%A_%' escape 'A'; --escape '\'转义字符   --查询有津贴奖金的员工信息      select * from emp where comm >0;      select * from emp where comm is not null; --包含奖金为0的员工信息       select * from emp where comm is null;     --修改表的数据     select * from emp for update;

转载请注明原文地址: https://www.6miu.com/read-21816.html

最新回复(0)