数据库总结(一):基本SQL
数据库总结(二):基本查询
数据库总结(三):分组,联结
数据库总结(四):表设计之关联关系
数据库总结(五):视图,约束,索引
数学相关的函数
- 向下取整 floor(num)
select floor(3.14); - 四舍五入 round(num)
select round(23.8);
round(num,m)
select round(23.879,2); - 非四舍五入 truncate(num,m)
select truncate(23.879,2); - 随机数 rand() 0-1
select rand();
-获取0-5的整数随机数
select floor(rand()*6);
-3-5的随机数
分组查询 group by
-
查询每个部门的平均工资
select deptno,avg(sal) from emp group by deptno; -
查询每个部门的工资总和
select deptno,sum(sal) from emp group by deptno; -
查询每种职业的最高工资
select job,max(sal) from emp group by job; -
查询每个领导下的人数
select mgr,count(*) from emp
where mgr is not null group by mgr; -
查询每个部门工资大于1000的员工数量
select deptno,count(*) from emp
where sal>1000 group by deptno;
多字段分组查询 只需要在group by后面写多个字段名通过逗号分隔 -
每个部门每个主管的手下人数
select deptno,mgr,count(*) from emp
where mgr is not null
group by deptno,mgr;53.案例:查询emp表中每个部门的编号,人数,工资总和,最后根据人数进行升序排列,如果人数一致,根据工资总和降序排列。
select deptno,count(),sum(sal) from emp
group by deptno
order by count(),sum(sal) desc;
-别名写法
select deptno,count(*) c,sum(sal) s from emp
group by deptno
order by c,s desc;54.案例:查询工资在1000~3000之间的员工信息,每个部门的编号,平均工资,最低工资,最高工资,根据平均工资进行升序排列。
select deptno,avg(sal) a,min(sal),max(sal) from emp
where sal between 1000 and 3000
group by deptno
order by a;
55.案例:查询含有上级领导的员工,每个职业的人数,工资的总和,平均工资,最低工资,最后根据人数进行降序排列,如果人数一致,根据平均工资进行升序排列
select job,c