## 单表查询

### 1.选择表中若干列

#### 【例3.19】查询全体学生姓名和出生年份

``````select Sname,2021-Sage --查询出生年份需要利用算术表达式（今年年份-学生年龄）
from Student
``````

#### 【例3.20】查询全体学生姓名、出生年份、院系（用小写）

``````select Sname,'Year of Birth:',2021-Sage,LOWER(Sdept)
from Student;
``````

LOWER( ) 返回小写的字符串，UPPER( ) 返回大写的字符串

*方法一：

``````select Sname NAME,'Year of Birth:' BIRTH ,2021-Sage BIRTHDAY,LOWER(Sdept) DEPARTMENT
from Student;
``````

*方法二

``````select Sname[Name],'Year of Birth:'[Year of Birth:],2021-Sage[2021-Sage],LOWER(Sdept)[LOWER(Sdept)]
from Student;
``````

### 2.选择表中的若干元组

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

``````select Sno
from SC
``````

``````select DISTINCT Sno
from SC
``````

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

``````select Sname
from Student
where Sdept='CS';
``````

#### 【例3.23】查询所有年龄在20岁以下的学生姓名和年龄

``````select Sname,Sage
from Student
where Sage<20;
``````

#### 【例3.24】查询考试成绩不及格的学生的学号

``````select DISTINCT Sno
from SC
``````

#### 【例3.25】查询年龄在20~23岁（包括20和23）之间的学生姓名、系别、年龄

``````select Sname,Sdept,Sage
from Student
where Sage BETWEEN 20 AND 23;
``````

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

``````select Sname,Sdept,Sage
from Student
where Sage NOT BETWEEN 20 AND 23;
``````

#### 【例3.27】查询CS、MA的学生姓名和性别

``````select Sname,Ssex
from Student
where Sdept IN('CS','MA');
``````

#### 【例3.28】查询不是CS也不是MA的学生姓名和性别

``````select Sname,Ssex
from Student
where Sdept NOT IN('CS','MA');
``````

#### 【例3.29】查询学号为201911020的学生的详细情况

``````select *
from Student
where Sno LIKE '201911020'; --等价于 Sno='201911020'
``````

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

``````INSERT  INTO  Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215121','李勇','男','CS',20);
INSERT  INTO  Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215122','刘晨','女','CS',19);
INSERT  INTO  Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215123','王敏','女','MA',18);
INSERT  INTO  Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215125','张立','男','IS',19);
INSERT  INTO  Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215128','陈冬','男','IS',20);
INSERT  INTO  Student (Sno,Sname,Ssex,Sdept,Sage) VALUES ('201215129','欧阳询','男','IS',20);
``````
``````select Sname,Sno,Ssex
from Student
where Sname LIKE '刘%';
``````

#### 【例3.31】查询姓"欧阳"且全名为三个汉字的学生的姓名。

``````select Sname
from Student
where Sname LIKE '欧阳_';
``````

#### 【例3.32】查询名字中第2个字为"阳"字的学生的姓名和学号。

``````select Sname,Sno
from Student
where Sname LIKE '_阳%';
``````

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

``````select Sname,Sno,Ssex
from Student
where Sname NOT　LIKE '_阳%';
``````

#### 【例3.34】查询DB_Design课程的课程号和学分。

``````select Cno,Ccredit
from Course
where Cname LIKE 'DB\_Design' ESCAPE'\'; --ESCAPE是对‘\’进行转义
``````

#### 【例3.35】查询以"DB_"开头，且倒数第3个字符为 i的课程的详细情况。

``````select *
from Course
where Cname LIKE 'DB\_%i__' ESCAPE'\' --是% 不是& c语言写多了...
``````

#### 【例3.36】某些学生选修课程后没有参加考试。查询缺少成绩的学生的学号和相应的课程号。

``````select Sno,Cno
from SC
``````

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

``````select Sno,Cno
from SC
``````

#### 【例3.38】查询计算机系年龄在20岁以下的学生姓名。

``````select Sname
from Student
where Sdept= 'CS' AND Sage<20;
``````

### 3.ORDER BY 子句

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

``````select Sno,Grade
from SC
where Cno='3'
``````

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

``````select *
from Student
ORDER BY Sdept,Sage DESC;
``````

### 4.聚集函数

#### 【例3.41】查询学生总人数。

``````select COUNT(*)
from Student;
``````

#### 【例3.42】查询选修了课程的学生人数。

``````select COUNT(DISTINCT Sno)
from SC;
``````

#### 【例3.43】计算1号课程的学生平均成绩。

``````select AVG(Grade)
from SC
where Cno='1';
``````

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

``````select MAX(Grade)
from SC
where Cno='1';
``````

#### 【例3.45】查询学生201215121选修课程的总学分数。

``````select SUM(Ccredit)
from Course,SC
where Sno='201215121' AND Course.Cno=SC.Cno --这句要好好理解一下
``````

### 5.GROUP BY 子句

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

``````select Cno,COUNT(Sno)
from SC
GROUP BY Cno;
``````

#### 【例3.47】查询选修了2门以上课程的学生学号。

``````select Sno
from SC
GROUP BY Sno
HAVING COUNT(*)>2;
``````