赞
踩
前面实验2已经加过,不再执行
select SName,SNO,Major from Student
select * from Student;
select SNO,SName,YEAR(GETDATE())-BirthYear Age from Student
select distinct SNO from SaleBill
select * from Student where Major = 'MIS'
select * from Student where YEAR(GETDATE())-BirthYear !> 20;
select * from Goods where Number between 3 and 10
select * from Goods where ProductTime between '2017-1-1' and '2017-12-31'
select * from Student where SName between '李明' and '闵红'
select * from SaleBill where GoodsNO in ('GN0001','GN0002')
select * from Student where Major != 'MIS'
select * from Goods where GoodsName like '%咖啡%'
select * from Student where SName like '_民%'
select * from Goods where GoodsNO not like '%[147]'
select * from Goods where GoodsNO like '%[^147]'
select * from Student where Major = 'AC' or Major = 'MIS' and Ssex = '男'
select * from Student order by BirthYear
select GoodsNO,GoodsName,Number,ProductTime from Goods
where GoodsName like '%咖啡%' order by Number,ProductTime desc
select GoodsNO,GoodsName,Number,ProductTime,
QGPeriod * 30 - DATEDIFF(DAY,ProductTime,GETDATE()) days_remaining
from Goods
order by days_remaining
select COUNT(*) 商品个数 from Goods
select COUNT(distinct GoodsNO) 商品种类 from SaleBill
select MAX(Number) 最大销售量,MIN(Number) 最小销售量,AVG(Number) 平均销售量 from SaleBill
select SNO,COUNT(*) 商品种类 from SaleBill group by SNO
select SNO,COUNT(*) 商品种类 from SaleBill group by SNO having COUNT(*)>=3
select Student.BirthYear,Student.Ssex,COUNT(*) from Student
group by BirthYear,Ssex
order by BirthYear desc,count(*)
select * from Student S join SaleBill SA on S.SNO = SA.SNO
select * from Student S
join SaleBill SA on S.SNO = SA.SNO
where Major = 'MIS'
select college,SName,sum(SA.Number * SalePrice) 消费金额
from Student S join SaleBill SA on S.SNO = SA.SNO join Goods G on SA.GoodsNO = G.GoodsNO
where college = 'CS'
group by college,SName
select G2.GoodsNO,G2.GoodsName
from Goods G join Goods G2 on G.CategoryNO = G2.CategoryNO
where G.GoodsName = '麦氏威尔冰咖啡'
and G2.GoodsName != '麦氏威尔冰咖啡'
select GoodsName,G.Number 现货存量
from Goods G left join SaleBill GA on GA.GoodsNO = G.GoodsNO
where GA.SNO is null
select GoodsName from Goods
where CategoryNO = (select CategoryNO from Goods
where GoodsName = '麦氏威尔冰咖啡') and GoodsName != '麦氏威尔冰咖啡';
select GoodsName,InPrice from Goods
where InPrice > (select avg(InPrice) from Goods)
select SNO,SName from Student where SNO
in(select distinct SNO from SaleBill where GoodsNO
in(select GoodsNO from Goods where SupplierNO
= (select SupplierNO from Supplier
where SupplierName = '东菀市南城久润食品贸易部')))
select * from Goods
where InPrice > (select avg(InPrice) from Goods G where CategoryNO = Goods.CategoryNO)
select * from Student where exists
(select * from SaleBill where SNO = Student.SNO)
select distinct SNO from SaleBill S1 where
S1.SNO != 'S02' and not exists
(select * from SaleBill S2 where S2.SNO = 'S02' and not exists
(select * from SaleBill S3 where S3.SNO = S1.SNO and S3.GoodsNO = S2.GoodsNO))
select * from Student where Major = 'MIS'
union
select * from Student where BirthYear > 1999
select * from Student where Major = 'MIS'
intersect
select * from Student where BirthYear > 1991
select distinct SNO from SaleBill
where SNO!='S02' and not exists
(select GoodsNO from SaleBill where SNO = 'S02'
except
select GoodsNO from SaleBill S where S.SNO = SaleBill.SNO)
select C.CategoryName,AVG_CA.AVGSALEPRICE
from Category C join
(select CategoryNO,avg(SalePrice) from Goods group by CategoryNO)
as AVG_CA(CategoryNO,AVGSALEPRICE) on C.CategoryNO = AVG_CA.CategoryNO
select * from Student S join
(select SNO,GoodsNO from SaleBill where GoodsNO = 'GN0002') SA_SNO on S.SNO = SA_SNO.SNO
select top 3 G.GoodsNO,sum(SA.Number * G.SalePrice) GOODSUM
from Goods G join SaleBill SA
on SA.GoodsNO = SA.GoodsNO
group by G.GoodsNO
order by GOODSUM desc
select top 3 with ties * from Student
order by BirthYear
(1)查询商品种类信息。
select CategoryNO,CategoryName,Description from Category
(2)查询IT专业所有学生信息。
select * from Student where Major = 'IT'
(3)查询MIS专业年龄小于20岁的学生信息。并为MIS列取别名为“信息管理系统”。
select SNO,SName,BirthYear,Ssex,College,Major 信息管理系统,WeiXin
from Student where YEAR(GETDATE()) - BirthYear <22 and Major = 'MIS'
(4)查询利润率大于30%的商品编号与商品名。
select GoodsNO,GoodsName,ROUND((SalePrice-InPrice)/InPrice,2) 利润率
from Goods where (SalePrice-InPrice)/InPrice > 0.3
(5)查询广州佛山供应的商品信息。
select * from Goods G join Supplier S on G.SupplierNO = S.SupplierNO
where S.Address = '广州佛山'
(6)查询购买了商品种类为咖啡的MIS专业的学生信息。
select * from Student where SNO in(
select SNO from SaleBill where GoodsNO in(
select GoodsNO
from Goods G join Category C
on G.CategoryNO = C.CategoryNO
and CategoryName = '咖啡'
)
) and Major = 'MIS'
(7)查询购买了商品种类为咖啡的各专业的学生人数。
select Major,count(Major) 人数 from Student where SNO in( select SNO from SaleBill where GoodsNO in( select GoodsNO from Goods G join Category C on G.CategoryNO = C.CategoryNO and CategoryName = '咖啡' ) ) group by Major go --方法二 select Major, count(*) 人数 from ( select distinct Student.* from Salebill,Student,Goods,Category where SaleBill.SNO=Student.SNO and SaleBill.GoodsNO=Goods.GoodsNO and Goods.CategoryNO=Category.CategoryNO and CategoryName='咖啡' ) S group by Major go --方法三 select Major,count(distinct(Student.SNO)) 人数 from Salebill,Student,Goods,Category where SaleBill.SNO=Student.SNO and SaleBill.GoodsNO=Goods.GoodsNO and Goods.CategoryNO=Category.CategoryNO and CategoryName='咖啡' group by Major go
(8)查询购买各商品种类的各专业的学生人数。
select CategoryName,Major, count(*) 人数
from (
select distinct Student.*,CategoryName
from Salebill,Student,Goods,Category
where SaleBill.SNO=Student.SNO
and SaleBill.GoodsNO=Goods.GoodsNO
and Goods.CategoryNO=Category.CategoryNO
) S
group by CategoryName,Major
order by CategoryName
go
(9)查询从未购买过商品的学生信息。
select * from Student where SNO not in( select distinct SNO from SaleBill where GoodsNO in( select GoodsNO from Goods G join Category C on G.CategoryNO = C.CategoryNO ) ) go select * from student except select distinct student.* from SaleBill,Student where SaleBill.SNO=Student.SNO go
(10)查询与商品编号GN0005相同产地的商品编号、商品名。
select GoodsNO,GoodsName from Goods where SupplierNO in ( select SupplierNO from Supplier where Address =( select Address from Supplier S join Goods G on S.SupplierNO = G.SupplierNO where GoodsNO = 'GN0005' ) ) go select GoodsNo, GoodsName from Goods,Supplier where Goods.SupplierNO=Supplier.SupplierNO and Goods.GoodsNO!='GN0005' and address=( select address from goods,supplier where goods.GoodsNO='GN0005' and Goods.SupplierNO=Supplier.SupplierNO ) go
(11) 使用派生表查询各供应商的存货量。
select SupplierName 供应商名称,S2.SUM_Number 存货量 from Supplier S join ( select SupplierNO,SUM(Number) SUM_Number from Goods group by SupplierNO) S2 on S.SupplierNO = S2.SupplierNO go select supplierName 供应商,存货量 from supplier, ( select Supplier.supplierNO,sum(goods.number) 存货量 from goods,supplier where goods.SupplierNO=supplier.SupplierNO group by supplier.SupplierNO) N where supplier.SupplierNO=N.SupplierNO go
(12) 查询售价大于该种类商品售价均值的商品号、商品名。
select GoodsNO,GoodsName
from Goods G join (
select CategoryNO,ROUND(avg(SalePrice),2) avg_salePrice
from Goods
group by CategoryNO) G2
on G.CategoryNO = G2.CategoryNO
and SalePrice > avg_salePrice
go
select GoodsNO, GoodsName
from Goods G
where SalePrice>(select avg(SalePrice) from goods where CategoryNO=G.CategoryNO)
go
(13)分别用子查询与连接查询查询购买了商品编号为“GN0003”和“GN0007”的学生学号与姓名。
select SNO,SName from Student where SNO in ( select SNO from SaleBill where GoodsNO in('GN0003','GN0007') group by SNO having count(GoodsNO) = 2) go select S.SNO,SName from Student S right join ( select SNO from SaleBill where GoodsNO in('GN0003','GN0007') group by SNO having count(GoodsNO) = 2 ) g on S.SNO = g.SNO order by S.SNO select S.SNO,S.SName from SaleBill SB, Student S where SB.GoodsNO='GN0003' and Exists (select * from SaleBill where SaleBill.GoodsNO='GN0007' and SB.SNO=SaleBill.SNO) and SB.SNO=S.SNO go
(14)查询各校销售额。
select College,sum(sum_Number_salePrice) 销售额 from Student S join( select SA.SNO,sum(SA.Number * G.SalePrice) sum_Number_salePrice from SaleBill SA join Goods G on SA.GoodsNO = G.GoodsNO group by SNO) g on S.SNO = g.SNO group by College go select College, Sum(SaleBill.Number*Goods.SalePrice) from SaleBill,Goods,Student where SaleBill.GoodsNO=Goods.GoodsNO and SaleBill.SNO=Student.SNO group by College go
(15)查询购买额前三的校名、专业名。
select top 3 S.College,S.Major,sum(sum_Number_salePrice) amount from Student S join ( select SA.SNO,sum(SA.Number * G.SalePrice) sum_Number_salePrice from SaleBill SA join Goods G on SA.GoodsNO = G.GoodsNO group by SNO) g on S.SNO = g.SNO group by College,Major order by amount desc go select top 3 college,major,sum(saleprice*salebill.number) amount from salebill,student,goods where salebill.sno=student.sno and salebill.goodsno=goods.goodsno group by college,major order by amount desc go
(16)使用集合查询方式查询生产日期早于2018-1-1 或库存量小于30的商品信息。
select * from Goods where ProductTime <'2018-1-1'
union
select * from Goods where Number <30
go
select * from goods where datediff(day,'2018-1-1',producttime)<0
union
select * from goods where number<30
go
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。