数据库作业-sql语句查询

Posted 快乐的xiao何

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库作业-sql语句查询相关的知识,希望对你有一定的参考价值。

建表的插入数据的表数据库作业-sql建表和插入数据_快乐的xiao何的博客-CSDN博客create table supplier( supplierno char(6) primary key, suppliername nvarchar(10), address nvarchar(20), number char(11) )create table category( categoryno char(5) primary key, categoryname varchar(20), descriptions text...https://blog.csdn.net/m0_53967061/article/details/124065839

-- 1. 查询全部商品信息。
SELECT * from goods

-- 2.查询类别为“cn002”的所有商品信息。
SELECT * from goods JOIN category on goods.categoryno = category.categoryno where goods.categoryno ="cn002"
-- 3. 查询类别为“cn001”和“cn002”对应的所有商品信息。
select * from category join goods on  category.categoryno=goods.categoryno where category.categoryno like 'cn00[1-2]';
-- 4.查询类别为“cn005”且进价大于 20 的商品信息。
SELECT * from goods WHERE goods.categoryno="cn005" and goods.inprice >20
-- 5.查询库存数量小于 10 的商品号,供应商号,商品名和数量。
SELECT g.goodsno,g.goodsname,g.supplierno,g.number from goods as g where g.number <10
-- 6.查询有购买记录学生的学号。
SELECT DISTINCT s.sno  from salebill as s

-- 7.查询计算机专业“IT”学生的学号,姓名和年龄
SELECT s.sno,s.sname,2022-s.birthyear as age from student as s WHERE s.major ='IT'
-- 8.查询年龄在 22 到 24 岁学生的学号,姓名,学院,专业和年龄
SELECT s.sno,s.sname,s.college,s.major,2022-s.birthyear as age from student 
as s where 2022-s.birthyear BETWEEN 22 and 24

-- 9. 查询姓张的同学的学号,姓名,学院。
SELECT s.sno,s.sname,s.college from student as s where s.sname like "张%"
-- 10.查询库存数量在 20 以上的商品信息,并按数量的降序排列。
SELECT * from goods as g where g.number >20 order by g.number desc
-- 11.查询库存数量在 10 以内的商品编号、商品名称、类别号、数量,按类别升序库存数量降序排列结果。
SELECT g.goodsno,g.goodsname,g.categoryno,g.number from goods as g where g.number BETWEEN 6 and 13 ORDER BY g.categoryno ,g.number desc

-- 12.查询库存数量排名前三的商品信息。
SELECT  * from goods as g order by g.number DESC limit 3
-- 13. 查询类别为“cn001”库存数量最大的商品,显示商品号,商品名和数量
SELECT  * from goods as g order by g.number DESC limit 1
-- 14.查询管理信息系统专业“MIS”的学生人数。
SELECT count(*) from student as s where s.major ="MIS"
-- 15.按学院统计各个学院,年龄在 20 岁以上的学生人数。
SELECT s.college,count(*) from student as s where 2022-s.birthyear >20 GROUP BY s.college
-- 16.统计 2018 年各商品的销售总量。
SELECT g.goodsname,sum(s.number) as 销售总量 from   goods  as g join   salebill as s on 
g.goodsno = s.goodsno 
where s.happentime between '2018/01/01' and '2018/12/31' 
GROUP BY s.goodsno
-- 17.查询学生人数在 3 人以上的学院及学生人数。

SELECT s.college,count(*) from student  as s GROUP BY s.college HAVING count(s.college) >3
-- 18.查询 2019 年商品购买总数在 5 以上的学生的学号和购买总数。
SELECT student.sno,count(*) as 购买总数 from student  join salebill on student.sno=salebill.sno
where salebill.happentime between '2019/01/01' and '2019/12/31'
group by student.sno
HAVING 购买总数>5
-- 19.查询咖啡类商品的平均售价。
SELECT goods.goodsname,avg(goods.saleprice)as 平均售价 from goods join category on goods.categoryno=category.categoryno  where category.categoryname ="咖啡"
-- 20.查询与张小红同学院的学生的学号、姓名、学院。
SELECT s.sno,s.sname,s.college from student  as s  
where  s.college in  (
SELECT student.college from student where student.sname = "张小红") and s.sname!="张小红"
-- 21.查询所有商品的销售情况,包括商品号,商品名,学生姓名,销售时间,销售数量,按销售日期降序排列。

select goods.goodsno,goodsname,student.sname,happentime,salebill.number from goods 
join salebill on goods.goodsno=salebill.goodsno 
join student on salebill.sno=student.sno
order by happentime desc

-- 22.查询购买了商品的学生学号、姓名、学院。
SELECT DISTINCT s.sno,s.sname,s.college from student as s join salebill  as b on s.sno= b.sno 

--23 查询购买了咖啡类商品的学生的学号、姓名、学院
select SNO,SName,College from Student where SNO in
(select SNO from SaleBill where GoodsNO in 
(select GoodsNO from Goods where GoodsName like '%咖啡%'))

--24查询没有出售过的商品号,商品名,类别名、售价和数量。(连接查询和子查询)
select Goods.GoodsNO,GoodsName,Categoryname,Saleprice,number 
from Goods join Category on Goods.CategoryNO=Category.CategoryNO where Goods.GoodsNO not in(
select Goods.GoodsNO from Goods join SaleBill on Goods.GoodsNO=SaleBill.GoodsNO
)
--25 查询购买了商品号为“gn001”或“gn002”商品的学号,姓名和学院。
select Student.SNO,SName,college from Goods join SaleBill on Goods.GoodsNO=SaleBill.GoodsNO
join Student on Student.SNO=SaleBill.SNO
where Goods.GoodsNO like 'gn00[1-2]';
--26. 查询购买了商品号为“gn001”和“gn002”商品的学号,姓名和学院。
select SNO,SName,College from Student where SNO in
(select SNO from SaleBill where GoodsNO='GN001')
and Student.SNO in(select SNO from SaleBill where GoodsNO='GN002')
--27. 查询各学院年龄最大的学生,显示学号,姓名和学院。(相关子查询和派生表)
select SNO,SName,College from Student s 
where (YEAR(getdate())-YEAR(birthyear))=(select MAX(YEAR(getdate())-YEAR(birthyear)) 
from student where s.college = student.college)

--28. 统计每个商品的销售总数,要求显示商品号,商品名和销售总数。

select Goods.GoodsNO,GoodsName,sum(SaleBill.Number) as 销售总数 from Goods,SaleBill 
where Goods.GoodsNO = SaleBill.GoodsNO group by Goods.GoodsNO,GoodsName

--29.查询 2019 年商品购买总数在 5 以上的学生的姓名、学院和购买总数。
select SName,College,SaleBill.number
from Goods join SaleBill on Goods.GoodsNO=SaleBill.GoodsNO
join Student on SaleBill.SNO=Student.SNO
where (Happentime between '2019/01/01' and '2022/12/31') and SaleBill.number>5
group by SName,College,SaleBill.number;
--30. 找出供应商品总数在 50 以上的供应商号,供应商名,供应商地址,供应总数,
--结果按地址分组,并按供应总数的降序排列
select supplier.suppliername,Address,sum(goods.number) from
goods join supplier on goods.supplierno=supplier.supplierno
group by Address,suppliername
having sum(goods.Number)>50
order by sum(goods.number) desc;

SQL SERVER代理作业

(1)MS SQL代理中的作业是保存在MSDB数据库中的,可以通过如下SQL语句查询作业列表
select * from msdb.dbo.sysjobs
(2)MS SQL代理作业历史记录也是保存再MSDB数据库中的,可以通过如下SQL语句查询作业历史记录列表
select * from msdb.dbo.sysjobshistory
(3)作业时刻表可以通过如下SQL语句查询
select * from msdb.dbo.sysjobschedules
(4)通过如下SQL语句可以查询作业最后一次执行情况
select * from msdb.dbo.sysjobservers
(5)通过如下SQL语句可以查询作业执行步骤、执行命令等列表信息
select * from msdb.dbo.sysjobsteps

(6)以上查询还可以通过组合条件得出更为直观的结果,由于本人刚刚学习和了解SQL,具体语句还有待研究和总结;

(7)默认情况下,MS SQL代理左右作业历史记录为1000行,每个作业最多100行,
因此,如果作业比较多,会导致您看到的每个作业的历史记录会不一样多,记录开始的日期也不一样,有的作业甚至是空记录;为了改善以上问题,通过调整作业历史记录大小来改善,调整方法如下:
在“SQL SERVER代理”上点击右键,单击“属性”,然后选择“历史记录”,在右侧窗体内容中修改作业历史记录日志的大小。
修改完成后确定,即可生效。

以上是关于数据库作业-sql语句查询的主要内容,如果未能解决你的问题,请参考以下文章

Database | 分页SQL语句

sql的语句提示有语法错误,求大神 create database if not exists `fwzl`;

oracle 用sql语句查询 已打补丁列表。

C# Access 模糊查询SQL语句

SQL语句基本语法

Database | DQL&DML