MySQL
一、聚合函数的使用
COUNT()函数
SUM()函数
AVG()函数
MAX()函数
MIN()函数
1、我想知道我们公司有多少员工
select count(*) as 员工人数 from 员工
select concat('共有员工:',count(*),'人') as 员工人数 from 员工
第二种在MySQL中会出现乱码。
2,我想知道我们公司每个月员工的工资开销是多少?
select sum(目前薪资) as 工资总额 from 员工
3,我想知道我们公司员工的平均薪水如何?
select avg(目前薪资) as 平均薪水 from 员工
4,我想知道我们公司员工的最高工资是多少?
select max(目前薪资) as 最高工资 from 员工
5,我想知道我们公司员工的最低工资是多少?
select min(目前薪资) as 最低工资 from 员工
6,我想知道公司最富有的员工是谁?
select * from 员工 where 目前薪资=(select max(目前薪资) from 员工)
7,我想知道公司最穷的员工是谁?
select * from 员工 where 目前薪资=(select min(目前薪资) from 员工)
8,我想知道公司哪些员工的工资低于公司工资平均水平?
select 姓名,目前薪资 from 员工 where 目前薪资<(select avg(目前薪资) from 员工)
9,查询出比生产制造部最高工资还要高的其他部门员工信息。
select 姓名,目前薪资,部门 from 员工 where 目前薪资>=(select max(目前薪资) as 生产制造部的最高工资 from 员工 where 部门='生产制造部') and 部门!='生产制造部'
第二种方法
select 姓名,目前薪资,部门 from 员工 where 目前薪资>=(
select cast(max(目前薪资) as UNSIGNED) from 员工 where 部门='生产制造部') and 部门!='生产制造部&