问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
你好,欢迎来到懂视!登录注册
当前位置: 首页 - 正文

SQL数据库的例题请教一下大家

发布网友 发布时间:2022-04-08 22:50

我来回答

2个回答

懂视网 时间:2022-04-09 03:11

" and class = 95031

7、 以Class降序查询Student表的所有记录。

Select * from student order by class desc

8、 以Cno升序、Degree降序查询Score表的所有记录。

Select * from score order by cno asc,degree desc

 

9、 查询“95031”班的学生人数。

Select count(*) from student where class=’95031’

10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)

Select sno,cno from score order by degree desc limit 0,1

Select  sno,cno from score where degree=(select max(degree) from score)

查询语句查询出一个或者一列结果,可以作为其他查询语句的参数来使用,这就是子查询,也就是查询的嵌套

11、查询每门课的平均成绩(要按照课程分组 group by,然后求每门课平均分)。//avg

Select avg(degree) ,cno from score group by cno

12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

select avg(degree) from score  where cno like "3%" group by cno having count(*)>=5

--查询以3开头的课程信息--模糊查询

Select * from score where cno like “3%”

--查询选修课程人数大于等于5的课程编号

--select cno from score   group by cno having count(*)>=5

13、查询分数大于70,小于90的Sno列。

Select sno from score where degree>70 and degree<90

14、查询所有学生的Sname、Cno和Degree列。

Select ( select sname from student where student.sno=score.sno),cno,degree from score

//select *from a,b 笛卡尔积

Select sname,cno,degree from score join student on score.sno=student.sno

15、查询所有学生的Sno、Cname和Degree列。

Select sno,degree,(select cname from course where course.cno=score.cno) from score

 

16、查询所有学生的Sname、Cname和Degree列。

select  (Select sname from student where student.sno=score.sno),(select cname from course where course.cno=score.cno),degree from score

或者

Select sname,cname,degree from student,course,score where student.sno =score.sno and  course.cno = score.cno

17、 查询“95033”班学生的平均分。

Select avg(degree),cno from score join student on student.sno= score.sno 

where cno in (select cno from score  where class=95033)  group by cno

18、 假设使用如下命令建立了一个grade表:

create table grade(low  int(3),upp  int(3),rank  char(1))

insert into grade values(90,100,’A’)

insert into grade values(80,89,’B’)

insert into grade values(70,79,’C’)

insert into grade values(60,69,’D’)

insert into grade values(0,59,’E’)

现查询所有同学的Sno、Cno和rank列。

Select  sno,cno,rank from  score join grade on degree between low and   upp

19、  查询选修“3-105”课程的且成绩高于“109”号同学成绩的所有同学的记录。

Select * from score  where cno = "3-105"  and degree > (Select degree from score where sno = 109 and cno = "3-105")

20、查询score中选学多门课程的同学中分数为非                                                                                                                                                                                                                                                                                                                                                                   最高分成绩的记录。

 Select * from score  a where degree < (select max(degree) from score b where a.cno =b.cno ) group by sno having count(*) > 1  

21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。

select * from score where degree > (select degree from score where sno=109 and cno="3-105")

22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。

Select * from student where year(sbirthday) = (select year(sbirthday) from student where sno = 108)

23、查询“张旭“教师任课的学生成绩。

select * from score where cno in (select cno from course where tno in  (Select tno from teacher where tname = "张旭"))

24、查询选修某课程的同学人数多于5人的教师姓名。

select tname from teacher where tno in (select tno from course where cno in (Select cno from score  group by cno having count(*)>5))

25、查询95033班和95031班全体学生的记录。

Select * from student  where class = 95033 or class = 95031

26、  查询存在有85分以上成绩的课程Cno.

Select cno from score where degree > 85

27、查询出“计算机系”教师所教课程的成绩表。

select degree from score where cno in (select cno from course join teacher on course.tno = teacher.tno where depart = "计算机系")

或者

select degree from score where cno in (select cno from course where tno in (Select tno from teacher where depart = "计算机系"))

28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。

Select * from teacher where prof not in (select prof from teacher where prof in (select prof from teacher where depart = "计算机系") and depart = "电子工程系" )

Union

Select * from teacher where prof not in (select prof from teacher where prof in (select prof from teacher where depart = "电子工程系") and depart = "计算机系" )

29、查询选修编号为“3-105”课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。

Select * from score where cno = "3-105" and  degree > any( select degree from score where cno = "3-245")

30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.

Select * from score where cno = "3-105" and  degree > all( select degree from score where cno = "3-245")

31、 查询所有教师和同学的name、sex和birthday.

Select sname,ssex,sbirthday from student union select tname,tsex,tbirthday from teacher

32、查询所有“女”教师和“女”同学的name、sex和birthday.

Select sname,ssex,sbirthday from student where ssex = "女" union select tname,tsex,tbirthday from teacher where tsex = "女"

33、 查询成绩比该课程平均成绩低的同学的成绩表。

select sno,cno,degree from score a  where  degree < (Select avg(degree) from score b  where a.cno = b.cno) 

34、 查询所有任课教师的Tname和Depart.

Select tname,depart from teacher where tno in (Select tno from course where Cno in(select distinct Cno from Score))

35 、 查询所有未讲课的教师的Tname和Depart. 

Select tname,depart from teacher where tno not in (Select tno from course where Cno in(select distinct Cno from Score))

36、查询至少有2名男生的班号。

Select class from student group by class having count(ssex = "男") > 2 

37、查询Student表中不姓“王”的同学记录。

Select * from student where sname  not like "王%"

38、查询Student表中每个学生的姓名和年龄。

Select sname,year(now())-year(sbirthday) from student 

39、查询Student表中最大和最小的Sbirthday日期值。

Select max(sbirthday),min(sbirthday) from student 

40、以班号和年龄从大到小的顺序查询Student表中的全部记录。

Select * from student order by class desc,year(now())-year(sbirthday) desc

或者

select * from Student order by class desc,Sbirthday

41、查询“男”教师及其所上的课程。

select cname from course join teacher on teacher.tno = course.tno where tsex = "男"

或者

Select cname from course where tno in (Select tno from teacher where tsex = "男")

42、查询最高分同学的Sno、Cno和Degree列。

Select sno,cno,degree from score where degree = (select max(degree) from score)

或者

Select * from score order by degree desc limit 0,1

43、查询和“李军”同性别的所有同学的Sname.

Select sname from student where ssex = (select ssex from student where sname = "李军")

44、查询和“李军”同性别并同班的同学Sname.

Select sname from student where ssex = (select ssex from student where sname = "李") 

&& class = (select class from student where sname = "李军")

45、查询所有选修“计算机导论”课程的“男”同学的成绩表。

Select student.sno,sname,cname,degree from score,student,course where score.sno = student.sno and course.cno = score.cno and ssex = "男" and cname= "计算机导论"

或者

select * from Score where Sno in (select Sno from Student where Ssex = ‘男‘) and Cno in(select Cno from Course where Cname =‘计算机导论‘)

SQL例题合集

标签:

热心网友 时间:2022-04-09 00:19

看了你的题,首先几个表需要说明一下Out_Goods和In_Goods在定义表时少了Sh_address列,列的属性参考Provider表中的Address

问题解答:
问题1、自定义一个函数,计算供货商编号为PD001所提供产品的平均价格,如果平均价格〉70,则输出“价格适中”;如果平均价格<=70,则输出价格较低。
CREATE FUNCTION [dbo].[fn_calc_avg_price]
(
@PID char(10) = 'PD001'-----------货商编号,此处默认值为PD001
)

RETURNS varchar(50) AS

BEGIN
DECLARE @vchPriceDesc varchar(50)
DECLARE @mnyPriceAvg money

SELECT @mnyPriceAvg = AVG(PPrice)
FROM Proct WHERE PID = @PID

IF (@mnyPriceAvg > 70) SET @vchPriceDesc = '价格适中'
IF (@mnyPriceAvg <= 70) SET @vchPriceDesc = '价格较低'

RETURN @vchPriceDesc
END

问题2、创建一个内联表值函数,返回一个时间段内的出货信息,要求包括出货商品名称、供货商名称、出货价格、出货数量、出货日期。
CREATE FUNCTION [dbo].[fn_get_out_goods_info]
(
@dtBeginTime datetime, --------------------------时间段的起始时间
@dtEndTime datetime --------------------------时间段的终止时间
)

RETURNS @TempTable table
(
out_proct_name varchar(20),
out_provider_name varchar(20),
out_proct_price money,
out_proct_num int,
out_date datetime
)
AS

BEGIN
INSERT INTO @TempTable
SELECT
Proct.PName,
Provider.ProviderName,
Out_Goods.OutPrice,
Out_Goods.OutNum,
Out_Goods.OutDate
FROM
Proct, Provider, Out_Goods
WHERE
Provider.ProviderId = Out_Goods.ProviderId
AND Proct.PId = Out_Goods.PId
AND Out_Goods.OutDate <= @dtEndTime
AND Out_Goods.OutDate >= @dtBeginTime
RETURN
END

问题3、创建一个after触发器,当Provider 供货商信息表中的ProviderId发生更改时,同时更改In_Goods进货信息表和Out_Goods出货信息表ProviderId字段的值。
CREATE TRIGGER [trig_update_proID]
ON [dbo].[Provider]
AFTER UPDATE

AS

IF UPDATE(ProviderID)
BEGIN
UPDATE In_Goods SET ProviderID = (SELECT ProviderID FROM INSERTED) WHERE ProviderID = (SELECT ProviderID FROM DELETED)
UPDATE Out_Goods SET ProviderID = (SELECT ProviderID FROM INSERTED) WHERE ProviderID = (SELECT ProviderID FROM DELETED)
END

问题4、定义一个存储过程,要求使用游标,计算某段时间内售出产品的平均价格。

坦白的说,其实没必要使用游标,既然用了,那就顺便用下WHILE循环好了
CREATE PROCEDURE [dbo].[sp_calc_part_sales_price]
(
@dtBeginTime datetime,
@dtEndTime datetime
)
AS

DECLARE @mnyTotalPrice money
DECLARE @mnyCurPrice money
DECLARE @nCount int SELECT @nCount = 0

DECLARE cursPrice CURSOR LOCAL
FOR SELECT OutPrice FROM Out_Goods WHERE Out_Goods.OutDate <= @dtEndTime AND Out_Goods.OutDate >= @dtBeginTime

OPEN cursPrice
FETCH NEXT FROM cursPrice INTO @mnyCurPrice

WHILE @@FETCH_STATUS=0
BEGIN
SET @nCount = @nCount + 1
SET @mnyTotalPrice = @mnyTotalPrice + @mnyCurPrice
FETCH NEXT FROM cursPrice INTO @mnyCurPrice
END

IF @nCount = 0 SELECT 0 AS '平均价格'
IF @nCount > 0 SELECT @mnyTotalPrice/@nCount AS '平均价格'

OK,就这样吧,如果有不明白或者我写错的地方,再联系,Good Luck!
SQL数据库的例题请教一下大家

问题1、自定义一个函数,计算供货商编号为PD001所提供产品的平均价格,如果平均价格〉70,则输出“价格适中”;如果平均价格&lt;=70,则输出价格较低。CREATE FUNCTION [dbo].[fn_calc_avg_price](PID char(10) = 'PD001'---货商编号,此处默认值为PD001 )RETURNS varchar(50) AS BEGIN DECLARE @...

数据库的题目,请教高手

1.关系代数:πP#,PN,PR(σPS='希望')SQL:SELECT P#,PN,PR FROM P WHERE PS='希望';2.SELECT RD,R.C#,CN,PN,QTY FROM C,P,R WHERE C.C#=R.C#;第二题我也不是很清楚

SQL请教大家一个日期的加减法

DateAdd(m, -1, A)这里,m 表示月,-1 表示减1个月,而A 是你需要操作的日期字段。例如,如果你想获取某个日期的上个月,只需在查询中使用这个函数。对于日期减1,函数的写法同样简单,使用d 代表天数,-1 代表减1天:DateAdd(d, -1, A)同样地,将这个函数应用到你的数据表的某个日期字段...

请教关于SQL数据库的一道题目

首先假设有这么一个表,有idnum 银行帐号,mima银行密码,qukuan 取款金额 cunru 存款 create proc yh_zz idnum char(10),@mima char(10),qukuan float,@cunru float as update yinhang set qukuan=@qukuan where idnum=@idnum and mima=@mima and isnull(@cunru,0)=0 and qukuan is no...

师兄,我想向您请教个问题,我的sQL数据库,之前用了几个触发器来修改_百...

Update 库存表 Set 数量=数量-Inserted.数量 Where 库存表.物料编号=Inserted.编号单触这个触发器是没有问题的.如果再添加库存表的更新触发器:Insert Into 库存预警表(...) Select ... From Inserted Inner Join 物料表 On Inserted.物料编号=物料表.编号 Where 物料表.预警存量&lt;...

关系数据库SQL的断言表达问题,求达人指点。

至于你提到的约束,我认为只要同一时间,同一部门,同一课程的课程数小于等于该部门教师的数目就可以。疑问你的前四项并不能唯一确定一名老师,所以误认为实现的办法是弄一个insert的触发器,根据前四项count一下,只要大于老师的数目就不再插入,可能能解决你的问题。

请教一条非常难的sql查询,试试你的数据库编写水平

还有,你要统计的酒店价格、图片等信息应该使用非空约束。否则统计数据就会出错。最后。没有必要把SQL写成一句,多句的话可写的简单得多,还可以利用视图、临时表等手段来实现。多句SQL,可由存储过程来调用。而且效率要比单句直接调用要高。(非多次查询或非建立临时表的情况下)...

SQL语句问题,我不大会,麻烦请教一下,

(1)写出创建如上所示表STUINFO的SQL语句 create table stuInfo ( 学号varchar(6) primary key, 姓名varchar(8), 籍贯varchar(4), 数学tinyint ) (2)插入值为(200605,王五,北京,89)的一行数据,写出SQL语句 insert into stuInfo values ('200605','王五','北京',89) (3)删除姓名中包含...

请教sql高手一个or的问题

or是模糊查询的时候才用的,你的这是完全知道条件的查询~在显示结果的时候系统就不知道应该显示的课号等于1还是2的了。我不知道你是不是要想两个都查,如果是,就应该是这么写 select from curse where course1='vb'and course2='vb'要不然你就应该分别查~...

请教sql数据库高手跨表计算

建议建一个人员基本信息表,存每个人的基本工资,再建个出勤表,存加班等信息,这样的话 select (a.基本工资+b.加班小时*每小时加班费+b.出差天数*费用-扣费) 实发工资 from 人员信息表 a,出勤表b where a.人员=b.人员 and b.月份=''如果想要工资表的话就写存储过或直接用语句插入到工资...

声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。
E-MAIL:11247931@qq.com
用脑过度用脑过度的危害 吃味精人会变笨吗?拜托了各位 谢谢 聪明低调沉默孤傲的男上司如果对已婚女下属有意思他会表现岀来吗还是... ...就出汗,运动后大汗淋漓,常年这样,怎么回事,是身体有病吗? iPod2代,键盘密码丢失怎样重新刷机 爱出汗咋回事呢 考张家港常青藤实验中学 需要面试吗? 10年张家港市常青藤高中录取问题 符合右外踝骨骨折X线 右外踝骨折严重吗 形容窘迫一副狼狈相换成成语是什么 顺义区后沙峪古城村拆迁 掌上火车票好用么?抢票快么? 表达很糟糕的成语 顺义后沙峪规划图 表示很糟糕的成语有哪些? 非常窘迫,一副狼狈相是什么成语? 至强e5v2是什么意思? 描述“缺少钱,境况窘迫”的四字词语是什么? 境况窘迫的成语 表达生活窘迫的词语有哪些? 形容十分困苦或窘迫的四字成语 特斯拉和蔚来车主的共识,与Model3和ES6的抉择 IntelXeon E5-2650 v2和IntelXeon E5-2640 v3哪个好 形容心急如焚,紧张窘迫的成语,谢谢你们啦 形容窘迫的词语 非常窘迫,一副狼狈相。用什么成语表示。 至强E5 1680V3和至强E5 2680V2哪个性能好。 形容困苦或受窘的样子的成语 困顿、窘迫得不能忍受 是哪个成语 北京顺义后沙峪古城近几年有没有拆迁规划 掌上火车票抢票方便么? 形容窘迫的笑容的成语 2021年北京顺义后沙峪古城村会拆吗? 顺义区还需要几个村庄拆迁? 浅谈基层国土所如何加强效能建设 顺义区拆迁规划 基层国土所如何守住“前沿阵地” 乡(镇)国土资源管理所的职能 顺义区还需要几个村庄拆迁。 基层国土所如何创新管理模式 后沙峪会发展成望京吗 孟州市国土资源局 驾驶员有必要买意外险吗 顺义今年拆迁那些村 后沙峪和顺义城区哪边发展有前景? 夏普ux79cn打印机怎么装纸? 600字左右自我鉴定哪里有 四季分别开花的盆栽花有哪些 黄琳的学术专著
  • 焦点

最新推荐

猜你喜欢

热门推荐