SQL相关面试题 联系客服

发布时间 : 星期二 文章SQL相关面试题更新完毕开始阅读1007e4dcb04e852458fb770bf78a6529647d358f

表一:student_info 学号 0001

表二:curriculum 课程编号 0001 0002 表三:grade 学号 0001 0001 题目: 条件查询:

1. 在GRADE表中查找80-90份的学生学号和分数

select 学号,分数 from grade where 分数 between 80 and 90 2. 在GRADE 表中查找课程编号为003学生的平均分

select avg(分数) from grade where 课程编号='003' 3. 在GRADE 表中查询学习各门课程的人数

Select课程编号,count(学号) as 人数from grade group by 课程编号 4. 查询所有姓张的学生的学号和姓名

select 姓名,学号 from student_info where 姓名 like '张%'

嵌套查询:

1、 查询和学号’0001’的这位同学性别相同的所有同学的姓名和出生年月

select 姓名,出生年月 from student_info where 性别 in(select 性别 from student_info where sno='0001')

2、 查询所有选修课程编号为0002 和0003的学生的学号、姓名和性别

select 学号,姓名,性别 from student_info where 学号 in(select 学号 from grade where 课程编号='0002' and 学号 in(select 学号 from grade where 课程编号='0001'))

3、 查询出学号为0001的学生的分数比0002号学生最低分高的课程编号的课程编号和分数

select 课程编号, 分数 from grade where 学号='0001' and 分数>(select min(分数) from grade where 学号='0002') 多表查询:

1、 查询分数在80-90分的学生的学号、姓名、分数

课程编号 0001 0002 分数 80 90 课程名称 计算机基础 C语言 学分 2 2 姓名 张三 性别 男 出生年月 1981-8-9 家庭住址 北京 备注 NULL select student_info.学号,student_info.姓名,grade.分数 from student_info,grade where grade.分数 between 80 and 90 2、 查询学习了’C语言’课程的学生学号、姓名和分数

select student_info.学号,student_info.姓名,grade.成绩from

student_info,grade,curriculum where student_info.学号=grade.学号and grade.课程号=curriculum.课程号and curriculum.课程名='C语言'

3、 查询所有学生的总成绩,要求列出学号、姓名、总成绩,没有选课的学生总成绩为空。

select grade.学号,student_info.姓名,sum(grade.成绩) as 总成绩from student_info,grade where grade.学号=student_info.学号group by grade.学号,student_info.姓名

题目、活期存款中,“储户”通过“存取款单”和“储蓄所”发生联系。假定储户包括:账号,姓名,电话,地址,存款额;“储蓄所”包括:储蓄所编号,名称,电话,地址(假定一个储户可以在不同得储蓄所存取款) 1、写出设计以上表格的语句(4分) 2、创建一个触发器TR1完成下面内容:

当向“存取款单”表中插入数据时,如果存取标志=1则应该更改储户表让存款额加上存取金额,如果存取标志=0则应该更改储户表让存款额减去存取金额,如果余额不足显示余额不足错误。

CREATE TABLE CREATE TRIGGER tr1 on qukuan after insert AS BEGIN

declare @sid nvarchar(50) declare @type int declare @qian int declare @yuer int

select @sid=sid,@type=[type],@m=m from inserted select @yuer=yuer from cunkuan if(@type=1) begin

update cunkuan set yuer=yuer+@qian

end else begin

if(@yuer<@qian) begin

print '余额不足' end else begin

update cunkuan set yuer=yuer-@qian end end END GO

本题用到下面三个关系表:

CARD 借书卡: (CNO 卡号,NAME 姓名,CLASS 班级)

BOOKS 图书: (BNO 书号,BNAME 书名,AUTHOR 作者,PRICE 单价,QUANTITY 库存册数 )

BORROW 借书记录: (CNO 借书卡号,BNO 书号,RDATE 还书日期 备注:限定每人每种书只能借一本;库存册数随借书、还书而改变。 要求实现如下处理:

1. 写出自定义函数,要求输入借书卡号能得到该卡号所借书金额的总和

CREATE FUNCTION getSUM (

@CNO int )

RETURNS int AS BEGIN

declare @sum int

select @sum=sum(price) from BOOKS where bno in (select bno from BORROW where cno=@CNO)

return @sum END GO

2. 找出借书超过5本的读者,输出借书卡号及所借图书册数。

select CNO,count(BNO) as 借书数量from BORROW group by CNO having count(BNO)>3

3. 查询借阅了\水浒\一书的读者,输出姓名及班级。

select name,class from card where cno in( select cno from borrow where bno in(

select bno from BOOKS where bname='水浒'))

4. 查询过期未还图书,输出借阅者(卡号)、书号及还书日期。

select CNO,BNO,RDATE from borrow where getdate()>RDATE

5. 查询书名包括\网络\关键词的图书,输出书号、书名、作者。

select bno,bname,author from books where bname like '网络%'

6. 查询现有图书中价格最高的图书,输出书名及作者。

select bname,author from books where price in(select max(price) from books )

7. 查询当前借了\计算方法\但没有借\计算方法习题集\的读者,输出其借书卡号,并按卡号降序排序输出。

select cno from borrow where bno in (select bno from books where bname='计算方法') and cno not in ( select cno from borrow where bno in(select bno from books where bname='计算方法习题集')) order by cno desc 或

SELECT a.CNO

FROM BORROW a,BOOKS b

WHERE a.BNO=b.BNO AND b.BNAME=N'计算方法' AND NOT EXISTS(

SELECT * FROM BORROW aa,BOOKS bb WHERE aa.BNO=bb.BNO

AND bb.BNAME=N'计算方法习题集' AND aa.CNO=a.CNO) ORDER BY a.CNO DESC

8. 将\班同学所借图书的还期都延长一周。

update borrow set rdate=dateadd(day,7,rdate) from BORROW where cno in(select cno from card where class='一班')