第1关:卖过“海信”但没有卖过“海尔”空调的员工
-- 写出能实现以下查询的SQL语句: -- 查询销售记录中,卖过“海信”但没有卖过“海尔”空调的员工编号,姓名,性别和手机号,结果以员工编号排序.。 -- 请在以下空白处填写语句: select s.sid, s.sname, s.gender, s.mobile from staff s where exists( select 1 from sales_record sr join products p on sr.pid = p.pid where sr.sid = s.sid and p.manufacturer = '海信' )and not exists( select 1 from sales_record sr join products p on sr.pid =p.pid where sr.sid = s.sid and p.manufacturer = '海尔' ) order by s.sid; -- EOF
第2关:卖过1号员工所卖过的全部产品的员工
-- 写出能实现以下查询的SQL语句: -- 查询销售记录中,卖过1号员工所卖过的全部产品的员工编号和姓名,结果以员工编号排序。 -- 请在以下空白处填写语句: select s.sid, s.sname from staff s where s.sid !=1 and not exists( select p.pid from sales_record sr1 join products p on sr1.pid = p.pid where sr1.sid=1 and not exists( select 1 from sales_record sr2 where sr2.sid = s.sid and sr2.pid = sr1.pid ) ) order by s.sid; -- EOF
第3关:只卖过“海尔”空调的员工
-- 写出能实现以下查询的SQL语句: -- 查询销售记录中,只卖过“海尔”空调(没卖过其他品牌空调)的员工编号和姓名,结果以员工编号排序。 -- 请在以下空白处填写语句: select s.sid, s.sname from staff s join sales_record sr on s.sid=sr.sid join products p on p.pid = sr.pid group by s.sid,s.sname having count(distinct case when p.manufacturer!='海尔' then p.manufacturer end)=0 and count(distinct case when p.manufacturer = '海尔' then p.manufacturer end)>0 order by s.sid; -- EOF
第4关:2023年月报表
-- 写出能实现以下查询的SQL语句: -- 统计2023年每月销售总额.列出月份(列名:月),销售额(列名:销售额),结果依月份排序。 -- 请在以下空白处填写语句: select month(sr.sdate) as '月', sum(sr.quantity * p.price * coalesce(sr.discount,1)) as '销售额' from sales_record sr join products p on sr.pid = p.pid where year(sr.sdate)='2023' group by month(sr.sdate) order by month(sr.sdate); -- EOF
第5关:2023年10月员工销售报表
-- 写出能实现以下查询的SQL语句: -- 查询2023年10月,每位员工的销售额.列出:员工号(sid),姓名(sname),销售额(sales_amount),单笔金额超过10000元的单数(即销售记录数,命名large_rec_nums),单笔金额超过10000元的单数不超过5笔的不列出,结果按销售额从高到低排序。 -- 请在以下空白处填写语句: select s.sid, s.sname, sum(sr.quantity * p.price * coalesce(sr.discount,1)) as 'sales_amount', count(case when sr.quantity * p.price * coalesce(sr.discount,1)>10000 then 1 end) as 'large_rec_nums' from staff s join sales_record sr on sr.sid = s.sid join products p on sr.pid = p.pid where year(sr.sdate)='2023' and month(sr.sdate)='10' group by s.sid,s.sname having count(case when sr.quantity * price * coalesce(sr.discount,1)>10000 then 1 end)>5 order by sales_amount desc; -- EOF
第6关:年度销售冠军
-- 写出能实现以下查询的SQL语句: -- 查询2023年销售额最高(第1名)的员工号(sid),姓名(sname)和销售金额(sales_amount),所给数据集没有并列第1名 -- 请在以下空白处填写语句: select s.sid, s.sname, sum(sr.quantity * p.price * coalesce(sr.discount,1))as 'sales_amount' from sales_record sr join products p on sr.pid = p.pid join staff s on sr.sid = s.sid where year(sr.sdate)=2023 group by s.sid, s.sname order by sales_amount desc limit 1; -- EOF