SQL查询语句复习题


 

新建学生-课程数据库的三个表:

学生表:Student(Sno,Sname,Ssex,Sage,Sdept) Sno主键;

课程表:Course(Cno,Cname,Cpno,Credeit) Cno主键;

学生选修表:SC(Sno,Cno,Grade) Sno,Cno,主键;

Student

学号

Sno姓名

Sname性别

Ssex年龄

Sage所在系

Sdept

95001 李勇 20 CS

95002 刘晨 19 IS

95003 王敏 18 MA

95004 张立 19 IS

 

Course

课程号Sno

课程名Cname

先行课Cpno

学分   Credit 

1 数据库 5 4

2 数学 2

3 信息系统 1 4

4 操作系统 6 3

5 数据结构 7 4

6 数据处理 2

7 Pascal语言 6 4

 

SC

学号 Sno

课程号 Cno

成绩 Grade

95001 1 92

95001 2 85

95001 3 88

95002 2 90

95002 3 80

:查询表中的列和行

1:查询全体学生的学与姓名

sele sno,sname from student

2:查询全体学生的姓名、学号、所在系。

sele sno,sname,sdept from student

3:查询全体学生的详细记录

sele from student

4:查询全体学生的姓名及出生年份

sele sno,sage from student

5:查询全体学生的姓名,出生年份及所在系,要用小写字母表示系名

6:查询选修了课程的学生学号

sele sno,cno from sc

7:查询选修了课程的学生姓名

sele distinct sname from student,sc where student.sno=sc.sno

:条件查询:

常用的查询条件

查询条件谓词

比较=,<,>,>=,<=,!=,<>,!>,!<;

not+上述比较运算符

确定范围Between and,Not between And,

确定集合IN,not IN

字符匹配Like,Not Like

空值IsNull,ISNOTNULL

多重条件AND,OR

1:查询计算机系全体学生的姓名

sele sname from student where sdept=”CS”

2:查询所有年龄在20岁以下的学生姓名及其年龄

sele sname,sage from student where sage<20

3:查询考试成绩有不及格的学生的学号

sele student.sno from student,sc where student.sno=sc.sno and grade<60

4:查询年龄在2023间的学生的姓名,系别及年龄

sele sname,sdept,sage from student where sage between 20 and 23

5: 查询年龄不在2023间的学生的姓名,系别及年龄

sele sname,sdept,sage from student where sage not between 20 and 23

6:查询信息系(IS),数学系(MA)和计算机系(CS)学生的姓名和性别

sele sname,ssex from student where sdept in("IS","MA","CS")

7:查询不是信息系(IS),数学系(MA)和计算机系(CS)学生的姓名和性别

sele sname,ssex from student where sdept not in("IS","MA","CS")

8:查询学号为”95001”的学生详细情况

sele from student where sno=95001

9:查询所有姓刘的学生的姓名,学号和性别(where name like %’)

sele sname,sno,ssex from student where sname like '%'

10:查询姓欧阳且命名为三个汉字的学生的姓名

sele sname from student where sname like '欧阳_'

11:查询名字中第2个字为字的学生姓名和学号(where sname like ‘_ _%’)

sele sname,sno from student where sname like '_ _%'

12:查询所有不姓刘的学生姓名

sele sname from student where sname not like '%'

13:查询DB_Design课程的课程号和学分(where cname like ‘Db_Design’Escape’’)

sele cno,gredit from course where cname like ‘Db_Design’Escape’’

14:查询以”DB_”开头,且倒数第3个字符为i的课程的详细情况(where cname like ‘DB_%i__’escape’’)

‘DB_%i__’escape’’) sele cno,gredit from course where cname like ‘Db_%i__’escape’’

15:查询缺少成绩的学生的学号和相应的课程号

sele student.sno,cno from student,sc where grade is null

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值