【Sql Server】实验报告三:SQL的数据查询

一、实验目的

1.掌握单表查询。

2.掌握多表连接查询。

3.掌握子查询、集合查询。

4. 掌握派生表查询

5. 掌握聚合函数使用方法

二、实验内容

操作系统:Windows 10

数据库管理系统:SQL Server 2017

SQL Server数据库创建

参考的是教材P84页的“实验3-2数据查询”的内容

  • 实验过程

1.查询商品种类信息

select * from Category

2.查询IT专业所有学生信息

select * from Student where Major='IT'

3.查询MIS专业年龄大于20岁的学生信息。并为MIS列取别名为”信息管理系统“

select SNO,SName,BirthYear,Ssex,college,Major as "信息管理系统",WeiXin from Student 
where Major='MIS'and YEAR(GETDATE())-BirthYear>20

4.查询利润率大于30%的商品编号与商品名

select GoodsNO,GoodsName from Goods 
where ((SalePrice-InPrice)/InPrice)>0.3

5.查询广州佛山供应的商品信息

select * from Goods 
where SupplierNO=(select SupplierNo from Supplier where Address='广州佛山')

6.查询购买了商品种类为咖啡的MIS专业的学生信息

--先筛选出咖啡的商品编号

select GoodsNO from Goods 
join Category on Goods.CategoryNO=Category.CategoryNO 
where CategoryName='咖啡'

--查找MIS学生的消费记录

select * from Student join SaleBill on Student.SNO=SaleBill.SNO

where Major='MIS'

--消费记录中选择咖啡类商品编号

select DISTINCT Student.SNO,SName,BirthYear,Ssex,college,Major,WeiXin from Student

join SaleBill on Student.SNO=SaleBill.SNO

where Major='MIS'

and GoodsNO in

(select GoodsNO from Goods join Category on Goods.CategoryNO=Category.CategoryNO 
where CategoryName='咖啡')

7.查询购买了商品种类为咖啡的各专业的学生人数

select Major,count(*) as 人数 from Student
join SaleBill on Student.SNO=SaleBill.SNO
where GoodsNO in
(select GoodsNO from Goods join Category on Goods.CategoryNO=Category.CategoryNO where CategoryName='咖啡')
group by Major

8.查询购买各商品种类的各专业的学生人数

select Major,count(*) as 人数 from Student 
join SaleBill on Student.SNO=SaleBill.SNO
where GoodsNO in
(select GoodsNO from Goods join Category on Goods.CategoryNO=Category.CategoryNO)
group by Major

9.查询从未购买过商品的学生信息

select * from Student where SNO not in(select SNO from SaleBill)

10.查询与商品编号GN0005相同产地的商品编号、商品名

select GoodsNO, GoodsName from Goods

where SupplierNO in (select SupplierNO from Goods where GoodsNO='GN0005')

11.使用派生表查询各供应商的存货量

select Supplier.SupplierNO,SupplierName,kc.Number
from Supplier join
(select SupplierNO,sum(Number) from Goods group by SupplierNO) 
as kc(SupplierNO,Number)
on Supplier.SupplierNO=kc.SupplierNO

12.查询售价大于该种类商品售价均值的商品号、商品名

--得到商品种类及平均售价

select CategoryNO,avg(SalePrice) as AvgPrice from Goods group by CategoryNO

--完整代码
select GoodsNO,GoodsName from Goods

join (select CategoryNO,avg(SalePrice) as AvgPrice from Goods group by CategoryNO) 

as ag

on Goods.CategoryNO=ag.CategoryNO

where Goods.SalePrice>ag.AvgPrice

13.分别用子查询与连接查询查询购买了商品编号为”GN0003“和”GN0007“的学生学号与姓名

--子查询

select SNO,SName from Student where SNO in

(select SNO from SaleBill where GoodsNO='GN0003'or GoodsNO='GN0007')

--连接查询

select Student.SNO,Student.SName from Student

join(select DISTINCT SNO from SaleBill where GoodsNO='GN0003'or GoodsNO='GN0007') g

on Student.SNO=g.SNO

order by Student.SNO

14.查询各校销售额

--每个学校的学生

select SNO,college from Student

--每个学生的销售额

select SNO,sum(s.Number*g.SalePrice) sumprice from SaleBill s 
join (select GoodsNO,SalePrice from Goods) g on s.GoodsNO=g.GoodsNO 
group by SNO

--各校的销售额

select college,sum(sumprice) 销售额 from Student st
join(select SNO,sum(s.Number*g.SalePrice) sumprice from SaleBill s 
join (select GoodsNO,SalePrice from Goods) g on s.GoodsNO=g.GoodsNO group by SNO) p
on st.SNO=p.SNO
group by college

15.查询购买额前三的校名、专业名

select college,Major, sum(sumprice) 销售额 from Student st
join(select SNO,sum(s.Number*g.SalePrice) sumprice from SaleBill s 
join (select GoodsNO,SalePrice from Goods) g on s.GoodsNO=g.GoodsNO group by SNO) p
on st.SNO=p.SNO
group by college,Major
order by Major desc

16.使用集合查询方式查询生产日期早于2018-1-1或库存量小于30的商品信息

select* from Goods where ProductTime < '2018-1-1'

union

select * from Goods where Number<30

  • 实验结果

1:6203da961c9943619d1baab7a363b8ae.png

2:2db4c57bfbea4b3ea5d67e64a8089cfb.png

3:7a717fb276c74dad91ec35dae61f2ba4.png

4:6f02f0bfc04241e0b21a0f95dec8f896.png

5:041dc1cd09b845ae83d15e0b05347c4f.png

6:56e6b7fddf3145d6a47839853ddb9f1b.png

7:2824146e40494fd580496b90d774d15e.png

8:191585c330fe4be28a1d8d5195b60549.png

9:759d88e6d8ba48ddb293823892d58156.png

10:08d534b7ce19483a85216cf0ceb33c34.png

11:ff76d59c18274545952407059b6ecdf6.png

12:2f4e62fc1bf7422f904e985baf2b5955.png

13:2c02c50696eb491a8960c49d58278bf4.png

14:e7e715ae090a4983b7cd6c228a0ad141.png

15:41fc409c7d644b5b8dbefd273e075a7d.png

16:5513a4568fca4279b4d3cb47f353ddc7.png

  • 实验出错问题和解决
  • 实验心得

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱因斯坦乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值