0831练习作业(待纠正)

– 1.查询管理部目前薪资大于5ooo的员工的详细情况(员工)

select * from 员工 where 部门='管理部' and 目前薪资>5000

在这里插入图片描述

– 2.统计各部门男员工,女员工的最高薪资〈员工)

select 姓名,
case 性别
when -1 then '女'
when 0 then '男'
end as 性别,部门,
max(目前薪资) as 最高薪资 
from 员工 group by 性别,部门

在这里插入图片描述

– 3、统计各部门平均年龄(员工)

select 部门,avg(year(now())-year(出生日期)) as 平均年龄 from 员工 GROUP BY 部门

在这里插入图片描述

– 4、统计各部门的人数(员工)

select 部门,count(部门) as 人数 from 员工 GROUP BY 部门

在这里插入图片描述

– 6.查询员工表中年龄最大的员工信息

select 姓名,max(year(now())-year(出生日期)) as 年龄 from 员工 
select 姓名,year(now())-year(出生日期) as 年龄 from 员工 order by 年龄 desc LIMIT 1

在这里插入图片描述

– 7.查询员工表中各部门年龄最小的员工信息

select 姓名,min(year(now())-year(出生日期)) as 最小年龄,部门 from 员工 group by 部门

select 姓名,部门,year(now())-year(出生日期) as 最小年龄 from 员工 GROUP BY 部门  having min(year(now())-year(出生日期))

在这里插入图片描述

– 10.查询有哪些员工姓名是重复的并显示重复姓名的人数

select 姓名 , count(姓名)as 重复姓名人数
from 员工
group by 姓名
having count(姓名)>1
order by 重复姓名人数 desc

在这里插入图片描述

– 11.查询比平均单价高的产品的名称和单价(产品资料)

select 产品,单价 from 产品资料 where 单价>(select avg(单价)as 平均单价 from 产品资料) 

在这里插入图片描述

– 12、查询哪些公司没有采期(客户,订货主档)

select a.公司名称,a.客户编号,b.订单号码 from 客户 a
left join 订货主档 b on a.客户编号=b.客户编号
where 订单号码 is null

– l3、查询销售数量最多的产品信息(产品资料,订货明细〉

select a.产品编号,b.产品,b.已订购量 from 订货明细 a inner join 产品资料 b on a.产品编号=b.产品编号 oRDER BY 已订购量 desc limit 1

在这里插入图片描述

14.查询销售金额最多的产品信息产品资料,订货明细〉

SELECT  a.产品,round(b.数量*b.单价*(1-折扣),1) AS 销售金额 FROM  `产品资料` a,`订货明细` b WHERE a.`产品编号`=b.`产品编号`  ORDER BY  销售金额 DESC

在这里插入图片描述

– 15、查询出本月销售业绩最好的前三名员工,并将其工资增加10%(订货主档,员工)

select b.`员工编号`,round(sum(a.`运费`),1)*1.1 as 工资 from `订货主档` as a,`员工` as b where b.`员工编号`=a.`员工编号` group by `员工编号` order by 工资 desc limit 3

在这里插入图片描述

16、查询1997年7月份客户采购的详细信息(客户,订货主档,订货明细)

select * from 订货主档 a
inner join 客户 b on a.客户编号=b.客户编号
inner join 订货明细 c on a.订单号码=c.订单号码
where 订单日期>(1997-07-01)

select * from  订货主档  a, 客户 b,订货明细 c where a.客户编号=b.客户编号 and a.订单号码=c.订单号码 and 订单日期>(1997-07-01)

在这里插入图片描述

– 17、查询一个订单中采购金额最多的前三位公司详细信息(客户,订货主档,订货明细)

select a.公司名称,round(c.数量*c.单价*(1-c.折扣),1) as 总金额 from 客户 a,订货主档 b,订货明细 c where a.客户编号=b.客户编号 and b.订单号码=c.订单号码 order by 总金额 desc limit 3

在这里插入图片描述

18.根据公司采购数量多少从高到低排列(订货主档,订货明细)

select count(b.订单号码) as 总数量 from 订货主档 a,订货明细 b where a.订单号码=b.订单号码 group by a.收货人 order by 总数量 desc

在这里插入图片描述

19.统计各种产品类别的产品数量,按照产品类别数量从低到高显示(产品类别,产品资料)

select *,(select count(类别编号) from 产品资料 where 产品类别.类别编号=产品资料.类别编号 group by 类别编号) 
产品数量 from 产品类别 order by 产品数量

在这里插入图片描述

– 20.统计各个城市中客户的人数(客户)

select 城市,count(客户编号)as 客户人数 from 客户 group by 城市

在这里插入图片描述

原文地址:https://www.cnblogs.com/James-221/p/13647449.html