SQL精选习题<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
1、          创建数据库 MYDB
解答:create database MYDB
2、          创建学生表student (sno,sname,ssex,sage,sclass)
解答:create table student
(sno int primary key ,
sname varchar(8),
ssex varchar(3),
sage  int ,
sclass varchar (6))
3、          创建课程表 course(cno,cname,ccredit)
解答:create table course
(cno int primary key,
cname varchar(20),
ccredit int )
4、          创建选课表SC(sno,cno,grade)
解答:create table sc
(sno int foreign key references student(sno),
cno int foreign key references course(cno),
grade int)
5、          添加数据(student)
1
李勇
20
Y01
2
刘晨
21
Y02
3
王敏
19
Y02
4
张力
25
Y05

 

 

 

 

 

解答:insert into student alues (1,'李勇','男',20,'y01')
insert into student values (2,'刘晨','男',21,'y02')
insert into student values (3,'王敏','女',19,'y02')
insert into student values (4,'张力','男',20,'y05')
6、          添加数据(course
1
C语言
5
2
数据库
5
3
开发模式_VB
5

 

 

 

 

答:insert into course values (1,'数据库',5)
insert into student values (2,'C语言',5')
insert into course values (3,'开发模式-VB',5)
7、          添加数据(SC)
1
1
90
1
2
95
2
1
55
4
3
null

 

 

 

 

解答:insert into SC values (1,1,5)
insert into SC values (1,2,5)
insert into SC values (1,1,5)
insert into SC values (1,3,5)
8、          查询全体同学的学号,姓名
解答:select sno,sname from student
9、          查询全体同学的姓名学号班级(按顺序输出)
解答:select sname,sno,sclass from student
10、     查询全体同学的记录(显示所有行)
解答:select *from student
11、     查询全体同学的姓名及出生年份
解答:select sname,2006-sage 出生年份 from student
12、     查询全体同学姓名出生年份班级(班级要用小写字母LOWER函数)
解答:select sname,2006-sage 出生年份 ,lower(sclass) from student
13、     查询全体同学的姓名/出生年份/所在班级列为YearOfBirth
解答:select sname,2006-sage YearOfBirth ,sclass from student
14、     查询选课中学员的学号并且去掉重复行用distinct
解答:select distinct sno from sc
15、     查Y02班全体同学名单
解答:select sno from student where sclass='y02'
16、     查所有年龄在20岁以下的同学姓名及年龄
解答:select sname,sage from student where sage<20
17、     查考试不合格的同学学号
解答:select sno from sc where grade<60
18、     查年龄在19-20岁(包括19-20)之间的同学姓名班级年龄
解答:select sname,sclass,sage from student where sage>=19 and sage<=20
19、     查年龄不在19-20岁之间的同学的姓名,班级,年龄
解答:select sname,sclass,sage from student
where sage not between 19 and 20
20、     查Y02班和Y05班的同学姓名,性别
解答:select sname ,ssex from student
where sclass='y02' or sclass='y05'
21、     查不是Y02班和Y05班的同学的姓名,性别
解答:select sname,ssex from student where
 not sclass='y02' and not sclass='y05'
22、     查所有姓刘的同学的姓名,学号,性别
解答:select sname,sno,ssex from student where sname like '刘%'
23、     查所有姓张且全名为2个汉字的学生姓名
解答:select sname,sno,ssex from student where sname like '张_'
24、     某些学生未考试查缺少成绩的同学的学号和课程号
解答:select sno,cno from sc where grade is null
25、     查所有成绩的同学的学号,课程号,和成绩
解答:select sno,cno from sc where grade is not null
26、     查Y02班年龄在20岁以下的姓名和年龄
解答:select sname,sage from student where sclass='y02' and sage<20

 

27、     查选修1号课程的同学的学号和成绩,按降序排列
解答:select sno,grade from sc where cno=1 order by grade desc
28、     查全体同学信息查询结果按所在班级的班级名称按降序排列,同班同学按年龄升序排列
解答:select *from student order by  sclass desc , sage asc
29、     查学员总人数
解答:select count(*) from student
30、     查选修课程学员人数
解答:select count(*) from sc
31、     统计1号课的学员平均成绩
解答:select avg(grade) from SC where cno=1
32、     查选修1号课和同学最高成绩
解答:select max(grade) from SC where cno=1
33、     求各个课程号及相应选课人数
解答:select count(*) from SC group by cno
34、     查选取1门以上课程的同学学号
解答:select sno from SC group by sno having count(cno)>1
35、     查每个学员及其选修,课程情况
解答:select sno,cno from SC
36、     查每个学员及其选修课程情况对没有选课的也要输出
其姓名,学号,性别,班级
解答:select a.sno,a.sname,a.sage,a.ssex,a.sclass,sc.cno,sc.grade     
from student a left outer join sc on a.sno=sc.sno order by a.sname
37、     查选取2号课程且成绩在90分以上同学
解答:select *from SC where cno=2 and grade>9
38、     查询每个同学学号姓名,选课程名称及成绩
解答:select a.sno,a.sname,sc.cno,sc.grade from
student a left outer join SC on a.sno=SC.sno order by SC.sno
39、     查与刘晨在一个班的同学
解答:select sname from student where sclass in
(select sclass from student where sname='刘晨')
40、     选取C语言的同学学号和姓名
解答:select sno,sname from student where sno in (select sno from SC
 where cno in (select cno from course where cname='C语言'))
41、     查其他班中比Y02班某一同学大的同学姓名和年龄
解答:select sname,sage from student where sage>any
 (select sage from student where sclass='Y02') and sclass<>'Y02'
42、     查其他班中比Y02班同学全部都大的同学姓名和年龄
解答:select sname,sage from student where sage>all
(select sage from student where sclass='Y02') and sclass<>'Y02'
43、     查选取1号课程的学员的姓名
解答:select sname from student where sno in
 (select sno from SC where cno=1)
44、     查没有选取1号课程的学员的姓名
解答:select sname from student where sno not in
(select sno from SC where cno =1)
45、     查Y02班同学及年龄不大于19岁的学员(union)
解答:select *from student where sclass ='y02' union
select *from student where sage<=19
46、     查询选取1号课程或者2号课程的同学学号
解答:select distinct sno from SC where cno=1 or cno=2
47、     将4号学员的年龄改为23岁
解答:update student set sage=23 where sno=4
48、     将所有同学的年龄增加1岁
解答:update student set sage=sage+1
49、     Y02班的同学的成绩改为100分
解答:update sc set grade=100 where sno in
 (select sno from student where sclass='y02')
50、     删除学号为1的同学记录
解答:delete from student where sno=1
 
1、          创建数据库 MYDB
解答:create database MYDB
2、          创建学生表student (sno,sname,ssex,sage,sclass)
解答:create table student
(sno int primary key ,
sname varchar(8),
ssex varchar(3),
sage  int ,
sclass varchar (6))
3、          创建课程表 course(cno,cname,ccredit)
解答:create table course
(cno int primary key,
cname varchar(20),
ccredit int )
4、          创建选课表SC(sno,cno,grade)
解答:create table sc
(sno int foreign key references student(sno),
cno int foreign key references course(cno),
grade int)
5、          添加数据(student)
1
李勇
20
Y01
2
刘晨
21
Y02
3
王敏
19
Y02
4
张力
25
Y05

 

 

 

 

 

解答:insert into student alues (1,'李勇','',20,'y01')
insert into student values (2,'刘晨','男',21,'y02')
insert into student values (3,'王敏','女',19,'y02')
insert into student values (4,'张力','男',20,'y05')
6、          添加数据(course
1
C语言
5
2
数据库
5
3
开发模式_VB
5

 

 

 

 

答:insert into course values (1,'数据库',5)
insert into student values (2,'C语言',5')
insert into course values (3,'开发模式-VB',5)
7、          添加数据(SC)
1
1
90
1
2
95
2
1
55
4
3
null

 

 

 

 

解答:insert into SC values (1,1,5)
insert into SC values (1,2,5)
insert into SC values (1,1,5)
insert into SC values (1,3,5)
8、          查询全体同学的学号,姓名
解答:select sno,sname from student
9、          查询全体同学的姓名学号班级(按顺序输出)
解答:select sname,sno,sclass from student
10、     查询全体同学的记录(显示所有行)
解答:select *from student
11、     查询全体同学的姓名及出生年份
解答:select sname,2006-sage 出生年份 from student
12、     查询全体同学姓名出生年份班级(班级要用小写字母LOWER函数)
解答:select sname,2006-sage 出生年份 ,lower(sclass) from student
13、     查询全体同学的姓名/出生年份/所在班级列为YearOfBirth
解答:select sname,2006-sage YearOfBirth ,sclass from student
14、     查询选课中学员的学号并且去掉重复行用distinct
解答:select distinct sno from sc
15、     查Y02班全体同学名单
解答:select sno from student where sclass='y02'
16、     查所有年龄在20岁以下的同学姓名及年龄
解答:select sname,sage from student where sage<20
17、     查考试不合格的同学学号
解答:select sno from sc where grade<60
18、     查年龄在19-20岁(包括19-20)之间的同学姓名班级年龄
解答:select sname,sclass,sage from student where sage>=19 and sage<=20
19、     查年龄不在19-20岁之间的同学的姓名,班级,年龄
解答:select sname,sclass,sage from student
where sage not between 19 and 20
20、     查Y02班和Y05班的同学姓名,性别
解答:select sname ,ssex from student
where sclass='y02' or sclass='y05'
21、     查不是Y02班和Y05班的同学的姓名,性别
解答:select sname,ssex from student where
 not sclass='y02' and not sclass='y05'
22、     查所有姓刘的同学的姓名,学号,性别
解答:select sname,sno,ssex from student where sname like '%'
23、     查所有姓张且全名为2个汉字的学生姓名
解答:select sname,sno,ssex from student where sname like '_'
24、     某些学生未考试查缺少成绩的同学的学号和课程号
解答:select sno,cno from sc where grade is null
25、     查所有成绩的同学的学号,课程号,和成绩
解答:select sno,cno from sc where grade is not null
26、     查Y02班年龄在20岁以下的姓名和年龄
解答:select sname,sage from student where sclass='y02' and sage<20

 

27、     查选修1号课程的同学的学号和成绩,按降序排列
解答:select sno,grade from sc where cno=1 order by grade desc
28、     查全体同学信息查询结果按所在班级的班级名称按降序排列,同班同学按年龄升序排列
解答:select *from student order by  sclass desc , sage asc
29、     查学员总人数
解答:select count(*) from student
30、     查选修课程学员人数
解答:select count(*) from sc
31、     统计1号课的学员平均成绩
解答:select avg(grade) from SC where cno=1
32、     查选修1号课和同学最高成绩
解答:select max(grade) from SC where cno=1
33、     求各个课程号及相应选课人数
解答:select count(*) from SC group by cno
34、     查选取1门以上课程的同学学号
解答:select sno from SC group by sno having count(cno)>1
35、     查每个学员及其选修,课程情况
解答:select sno,cno from SC
36、     查每个学员及其选修课程情况对没有选课的也要输出
其姓名,学号,性别,班级
解答:select a.sno,a.sname,a.sage,a.ssex,a.sclass,sc.cno,sc.grade     
from student a left outer join sc on a.sno=sc.sno order by a.sname
37、     查选取2号课程且成绩在90分以上同学
解答:select *from SC where cno=2 and grade>9
38、     查询每个同学学号姓名,选课程名称及成绩
解答:select a.sno,a.sname,sc.cno,sc.grade from
student a left outer join SC on a.sno=SC.sno order by SC.sno
39、     查与刘晨在一个班的同学
解答:select sname from student where sclass in
(select sclass from student where sname='刘晨')
40、     选取C语言的同学学号和姓名
解答:select sno,sname from student where sno in (select sno from SC
 where cno in (select cno from course where cname='C语言'))
41、     查其他班中比Y02班某一同学大的同学姓名和年龄
解答:select sname,sage from student where sage>any
 (select sage from student where sclass='Y02') and sclass<>'Y02'
42、     查其他班中比Y02班同学全部都大的同学姓名和年龄
解答:select sname,sage from student where sage>all
(select sage from student where sclass='Y02') and sclass<>'Y02'
43、     查选取1号课程的学员的姓名
解答:select sname from student where sno in
 (select sno from SC where cno=1)
44、     查没有选取1号课程的学员的姓名
解答:select sname from student where sno not in
(select sno from SC where cno =1)
45、     查Y02班同学及年龄不大于19岁的学员(union)
解答:select *from student where sclass ='y02' union
select *from student where sage<=19
46、     查询选取1号课程或者2号课程的同学学号
解答:select distinct sno from SC where cno=1 or cno=2
47、     将4号学员的年龄改为23岁
解答:update student set sage=23 where sno=4
48、     将所有同学的年龄增加1岁
解答:update student set sage=sage+1
49、     Y02班的同学的成绩改为100分
解答:update sc set grade=100 where sno in
 (select sno from student where sclass='y02')
50、     删除学号为1的同学记录
解答:delete from student where sno=1

转载于:https://blog.51cto.com/youxue/51425

SQL精选习题及解答相关推荐

  1. 计算机网络在结构上可分为什么不同,计算机等级考试三级网络精选习题及详细解答(一)...

    1.下列说法中,哪一个是正确的? A.网络中的计算机资源主要指服务器;路由器;通信线路与用户计算机 B.网络中的计算机资源主要指计算机操作系统;数据库与应用软件 C.网络中的计算机资源主要指计算机硬件 ...

  2. 微型计算机原理及应用李干林,微机原理及接口技术李干林习题参考解答-20210415154329.docx-原创力文档...

    Document serial number[NL89WT-NY98YT-NC8CB-NNUUT-NUT108] Document serial number[NL89WT-NY98YT-NC8CB- ...

  3. vbnullchar相当于 java_VB第2版习题与解答的.doc

    VB第2版习题与解答的 VB习题与解答 第1章 了解Visual Basic 1. Visual Basic 6.0有哪些主要特点? 解答: Visual Basic 6.0主要有如下的特点\B .取 ...

  4. 21天学通python第4章课后题答案_人工智能教程习题及答案第4章习题参考解答

    94 第四章 不确定性推理 习题参考解答 4.1 练习题 4.1 什么是不确定性推理?有哪几类不确定性推理方法?不确定性推理中需要解决的 基本问题有哪些? 4.2 什么是可信度?由可信度因子 CF(H ...

  5. 通常所说的pc机是指微型计算机,2017年自考计算机应用基础精选习题及答案(1)

    1.如果删除文档中一部分选定的文字的格式设置,可按组合键_____A__. (A). [Ctrl]+[Shift]+[Z] (B). [Ctrl]+[Alt]+[Del] (C). [Ctrl]+[S ...

  6. 什么软件能解答c语言的题,C语言课本习题及解答.pdf

    第一篇 习题及解答 本篇按教材上的章目,先对各章所附的习题进行解答,给出参考答案:然后按各章内容 给出各类课外习题,并对习题进行了解答,给出参考答案:接着编写了假期作业并进行了解 答,供大家参考选用: ...

  7. 玩转 Numpy 的精选习题

    玩转 Numpy 的精选习题 (一) 1,打印 numpy 版本 import numpy as np print(np.__version__)>1.16.5 2.创建 10 个元素空向量 Z ...

  8. 浙大翁恺pat练习题_中国大学MOOC-翁恺-C语言-PAT习题及解答-第二周

    中国大学MOOC-翁恺-C语言-PAT习题及解答-第二周 02-0. 整数四则运算 输入格式:输入在一行中给出2个正整数A和B. 输出格式:在4行中按照格式"A 运算符 B = 结果&quo ...

  9. c语言1GB转成B,2018职称计算机考试WPS_Office精选习题9

    2018职称计算机考试WPS_Office精选习题9 1).下列说法中,正确的是a A)同一个汉字的输入码的长度随输入方法不同而不同 B)一个汉字的机内码与它的国标码是相同的,且均为2字节 C)不同汉 ...

最新文章

  1. 【spring框架】spring整合hibernate初步
  2. 牛顿迭代法求解平方根
  3. java的CountDownLatch使用
  4. SOL注入——HTTP头部注入(六)
  5. 网页版消消乐快速实现,无代码吗iVX 真那么简单?
  6. 使用Spring MVC开发Restful Web服务
  7. 搜索引擎的那些事(web遍历)
  8. cygwin 'unable to remap' issue
  9. 工控自动化 c语言编程,三菱PLC编程软件FXGP-WIN-C编程介绍
  10. 易语言调用大漠把血蓝内力体力转化为进度条
  11. linux 网络设置 DHCP
  12. PPT制作设计感需从哪些角度提升呢?
  13. [Aria2][Linux]宝塔面板Aria2安装和配置教程
  14. 《多多自走棋》、《全民超神》均停服:盘点那些凉得最快的游戏
  15. Visual reasoning
  16. python设置excel边框_python win32com excel边框格式
  17. 【数据库】GaussDB
  18. 小程序引用公共js,不看可惜了!!
  19. 二、八、十、十六进制之间的转换
  20. linux 命令硬盘指示灯闪烁,macOS下移动硬盘无法挂载且硬盘灯一直闪烁的解决方法...

热门文章

  1. java 分布式事务处理
  2. 时间管理类APP的Demo版本
  3. 设置log缓存_全局变量、事件绑定、缓存爆炸?Node.js内存泄漏问题分析
  4. 华为鸿蒙手机开机动画,华为“鸿蒙”来了!开机动画美炸了,用PPT一分钟就搞定...
  5. it项目管理案例_盈通顾问项目管理精英训练营(第一期)
  6. Linux的触屏手势软件安装,如何添加Mac的多点触控手势到Ubuntu | MOS86
  7. oracle的tns错误,Oracle TNS-12514错误的解决步骤
  8. serv u服务器显示图片,图解经典FTP服务器工具 SERV-U最安全的设置【防止被入侵】...
  9. oracle ocr掉盘,恢复OCR磁盘组一则
  10. Python基础(10) Python创建list