## 例题练习

Create Table student(
Sno char(12) Primary Key,
Sname char(12),
Ssex char(2),
Sdept char(40),
Sage SmallInt
);
Insert Into Student Values ('20191102062','刘哲轩','男','Computer Science',19)
Insert Into Student Values ('20191102063','叶晓晓','男','information security',20)
Insert Into Student Values ('20191102064','刘小轩','女','Computer Science',21)
Insert Into Student Values ('20191102065','叶大大','男','information security',20)
Insert Into Student Values ('20191102066','刘大轩','男','Computer Science',22)
Insert Into Student Values ('20191102067','刘轩','女','information security',20)
Insert Into Student Values ('20191102068','刘车干','男','Computer Science',19)
Insert Into Student Values ('20191102069','刘刘','女','information security',20)
Insert Into Student Values ('20191102070','刘刘流','男','Computer Science',22)
Insert Into Student Values ('20191102071','叶小刘','女','information security',20)
Create Table Course(
Cno Char(12) Primary Key,--课序号
Cname Char(12),
Cpno Char(12),--先行课的课序号
Ccredit SmallInt,
Foreign Key(Cpno) References Course(Cno)
);
Insert Into Course Values('1','数据库',null,4)
Insert Into Course Values('2','数学',null,2)
Insert Into Course Values('3','信息系统',null,4)
Insert Into Course Values('4','操作系统',null,3)
Insert Into Course Values('5','数据结构',null,4)
Insert Into Course Values('6','数据处理',null,2)
Insert Into Course Values('7','PASCAL语言',null,4)
update course set cpno='5' where cno='1'
update course set cpno='1' where cno='3'
update course set cpno='6' where cno='4'
update course set cpno='7' where cno='5'
update course set cpno='6'where cno='7'
Create Table SC(--学生选课表
Sno Char(12),
Cno Char(12),
Primary Key(Sno, Cno),
Foreign Key(Sno) References Student(Sno),
Foreign Key(Cno) References Course(Cno)
);
Insert Into SC Values('20191102062', '1', 100)
Insert Into SC Values('20191102062', '2', 51)
Insert Into SC Values('20191102063', '1', 65)
Insert Into SC Values('20191102064', '2', 46)
Insert Into SC Values('20191102065', '3', 85)
Insert Into SC Values('20191102066', '3', 95)
Insert Into SC Values('20191102067', '7', 99)
Insert Into SC Values('20191102068', '6', 93)



### 例3.19 查询全体学生的姓名及其出生年份

SELECT子句的<目标列表达式>不仅可以是表中的属性列，也可以是表达式。

Select Sname, 2020 - Sage from student


Select Sname, 2020 - Sage as '出生日期' from student


### 例3.20 查询全体学生姓名、出生日期和所在院系，要求使用小写字母表示系名

Select Sname, 'Year of Birth： ' as 'Year of Birth', 2020 - Sage, Lower(Sdept) From Student;


Select Sname, 'Year of Birth： '  Birth, 2020 - Sage Birthday, Lower(Sdept) From Student Department;


### 例3.21 查询选修了课程的学生学号

Select Sno   From SC;
--等价于：
Select All  Sno  From SC;


Select Distinct  Sno  From SC;


### 例3.22 查询计算机科学系全体学生的名单

Select Sname From  Student
Where  Sdept='Computer Science';


### 例3.23 查询所有年龄在20岁一下的学生姓名及年龄

Select Sname, Sage From Student Where Sage < 20


### 例3.24 查询考试不及格的学生的学号

Select Distinct Sno From SC Where Grade < 60


distinct可以去重, 为了体现这一点, 我们在SC表中加入一行

Insert Into SC Values('20191102062', '3', 51)


Select Distinct Sno, Cno From SC Where Grade < 60


### 例3.25 查询年龄在20~23岁之间的学生姓名、系别和年龄

Between…And语句可以找出特定范围内的元组，between后的是下界，and后的是上界

Select Sname, Sdept, Sage From Student Where Sage Between 20 And 23


Between ... And语句是两端的闭区间, 当要选择开区间时，可以根据容斥原理， 配合Not选出所需的开区间.

### 例3.26 查询年龄不在20~23岁之间的学生姓名、系别和年龄

Select Sname, Sdept, Sage From Student Where Sage Not Between 20 And 23


### 例3.27 查询计算机科学系(Computer Science)和信息系(Information Science)学生的姓名和性别.

Select Sname, Ssex From Student Where Sdept In ('Computer Science', 'Information Security');


### 例3.28 查询既不是计算机科学系(Computer Science)也不是信息系(Information Science)学生的姓名和性别.

Select Sname, Ssex From Student Where Sdept Not In ('Computer Science');


### 例3.29 查询学号为20191102062的学生的详细情况

[NOT] LIKE'<匹配串>' [ESCAPE '<换码字符>']


·_（下横线)代表任意单个字符。

Select *  From  Student Where  Sno Like '20191102062';


### 例3.30 查询所有姓刘的学生的姓名、学号和性别

Select Sname, Sno, Ssex  From Student Where  Sname Like '刘%';


### 例3.31 查询所有姓刘且全名不超过三个汉字的学生的姓名

Selcet Sname, Sno, Ssex From Student Where Sname Like '刘__'


### 注意:数据库字符集为ASCII时一个汉字需要两个;当字符集为GBK时只需要一个

Insert Into Student Values ('20191102072','叶晓大','男','information security',20)
Select Sname, Sno From Student Where Sname Like '_晓%'


### 例3.33 查询所有不姓刘的学生的姓名、学号和性别

Select Sname, Sno, Ssex  From Student Where  Sname Not Like '刘%';


### 例3.34 查询数据_库课程的课程号和学分

Insert Into Course Values('8','数据_库',null,4)
Select Cno,Ccredit From Course Where  Cname Like '数据\_库' Escape '\' ;


### 例3.35 查询以’数据_'开头,且倒数第三个字符是1的课程的详细情况

Insert Into Course Values('9','数据_库1111',null,4)
Select Cno,Ccredit From Course Where  Cname Like '数据\_%1__' Escape '\' ;


### 例3.36 某些学生选修课程后没有参加考试，所以有选课记录，但没有考试成绩，查询缺少成绩的学生的学号和相应的课程号

Insert Into SC Values('20191102071', '7', Null)
Insert Into SC Values('20191102069', '7', Null)
Select Sno,Cno From SC WHERE  Grade Is Null


Is不能改为=， 改成等于后, 查询为空.

### 例3.37 查所有有成绩的学生学号和课程号

Select Sno,Cno From SC WHERE  Grade Is Not Null


### 例3.38 查询计算机科学系年龄在20岁一下的学生姓名

Select Sname From Student Where Sdept = 'Computer Science' And Sage < 20


In谓词实际上是多个Or运算符的缩写

---Select Sname, Ssex From Student Where Sdept In ('Computer Science', 'Information Security');
Select Sname, Ssex From Student Where Sdept = 'Computer Science' Or 'Information Security'


### 例3.39 查询选修了3号课程的学生的学号及其成绩， 查询结果按降序排列

Select Sno, Grade From Sc Where Cno = '3' Order By Grade Desc;
Select Sno, Grade From Sc Where Cno = '3' Order By Grade Asc;
Select Sno, Grade From Sc Where Cno = '3' Order By Grade;


Order可以对表进行排序, Desc是降序排序, 以Grade为关键字, Asc是升序,下面分别是降序排序, 升序排序和默认排序的结果， 可以看出， 默认排序是升序排序.

### 例3.40 查询全体学生情况， 查询结果按所在系的系号升序排列，同一系中的学生按年龄降序排列

Select * From Student Order By Sdept, Sage Desc;


### 例3.41查询学生总人数

Count是一个聚集函数，可以计算出表的元组个数或者列的个数

Select Count(*) as '学生数' From Student


### 例3.42查询了选修课程的学生总人数

Select Count(Distinct Sno) From Sc;


### 例3.43计算选修了1号课程学生的平均成绩

Avg函数的作用是求出某一列的平均值，该列必须是数值型

Select Avg(Grade) From SC Where Cno = '1'


### 例3.44查询选修1号课程的学生最高分数

Max函数的作用是求出某一列的平均值，该列必须是数值型

Select Max(Grade) From SC Where Cno = '1'


### 例3.45查询学生20191102062选修课程的总学分数

Sum函数的作用是求出某一列的和，该列必须是数值型

Select Sum(Ccredit) From SC, Course Where Sno = '20191102062' And Sc.Cno = Course.Cno;


### 例3.46求各个课程号及相应的选课人数

GROUP BY子句将查询结果按某一列或多列的值分组，值相等的为一组。

Select Cno, Count(Sno) From SC Group By Cno;


### 例3.47查询选修了三门以上课程的学生学号

Select Sno From SC Group By Sno Having Count(*) > 3


### 例3.48查询平均成绩不低于90分的学生学号和平均成绩

Select Sno, Avg(Grade) From SC Where Avg(Grade) >= 90 Group By Sno;


Select Sno, Avg(Grade) From SC Group By Sno Having Avg(Grade) >= 90;