3.4.1 单表查询
一.选择表中 若干列
1 查询 全体学生的 学号 和 姓名
select sno, sname
from student
3 查询全体学生的 详细记录
select sno, sname, ssex, sage, sdept
from student
select *
from student
2 查询 全体学生的 学号 姓名 所在系
select sno,sname,sdept
from student
4 查 全体学生的 姓名 出生年份
select sname, 2004-sage
from student
select sname, 2004-sage as Sbirthyear
from student
5 查询 全体学生的姓名 出生年份 所在系,,,,,,(用小写字母 表示 所有的 系名
select sname, 'Year of Birth:' as Sbirthyear , 2016-sage , lower(sdept)
from student
5 取别名
select sname NAME, 'Year of Birth:' BIRTH , 2016-sage BIRTHDAY , lower(sdept) DEPARTMENT
from student
二.选择表中 若干元组行 P93
6 查询 选修了 课程的 学生学号 distinct.note
select sno
from sc
等价
select all sno
from sc
select distinct sno
from sc
7 查询 计算机系 全体学生的 名单.note
select sname
from student
where sdept='cs'
8 查询 所有年龄 在 20岁 以下的 学生姓名 及其年龄.note
select sname, sage
from student
where sage<20
9 查询 考试成绩 有 不及格 学生的 学号.note
select distinct sno
from sc
where grade < 60
10 查询 年龄在 20-23岁(包括 20 23) 之间 的 学生的 姓名 系别 年龄.note
select sname, sdept, sage
from student
where sage between 20 and 23
11 查询 年龄 不在在 20-23岁(包括 20 23) 之间 的 学生的 姓名 系别 年龄.note
12 查询 计算机系(CS) 数学系(MA) 信息系(IS)学生 的 姓名 和 性别.note
select sname, ssex
from student
where sdept in ('cs' , 'ma', 'is')
select sname, ssex
from student
where sdept='cs' or sdept='ma' or sdept='is'
13 查询 不是 计算机系(CS) 数学系(MA) 信息系(IS)学生 的 姓名 和 性别.note
select sname, ssex
from student
where sdept not in ('cs' , 'ma', 'is')
like 14 查询 学号为 200215121的 学生的 详细情况.note
select *
from student
where sno like '200215121'
not like
15 查询 所有 刘 姓 的 学生的 姓名, 学号, 性别.note
select *
from student
where sname like '刘%'
16 查询 姓 刘 且 全名 为2个汉字 的 学生 的 姓名.note
查询 姓 "刘" 且 全名 为2个汉字 的 学生 的 姓名
select *
from student
where sname like '刘_'
17 查询 名字中的 第2个字 为 晨 字的 学生的 姓名 和 学号.note
查询 名字中的 第2个字 为 晨
select *
from student
where sname like '_晨%'
18--15.note
查询 所有 不是 刘 姓 的 学生的 姓名, 学号, 性别
19 .note
21 有关空值的 查询.note
某些同学 选修课程后没有参加 考试
有选课记录,,但是 没有 考试成绩
查询 缺少 成绩的学生 的 学号 和 相应的 课程号
select sno, cno
from sc
where grade is null
22.note
select sno, cno
from sc
where grade is not null
23 查询 计算机系 年龄在 20岁 以下 的 学生 的 姓名.note
select sname
from student
where sdept='cs' and sage<20
三.Order by子句
24 查询 选修了 3号课程的学生 的 学号 和 成绩,,(成绩 降序.note
select sno, grade
from sc
where cno='3'
order by grade desc
25 查询全体学生情况 系号升序asc 年纪 降序 desc.note
四.聚集函数 P98
26 查询学生 总人数.note
select count(*) from student
27 查询 选修了 课程的 学生 人数.note
28 计算1 号 课程 学生的 平均成绩.note
29 查询 选修了1号 课程 的 学生 最高分数.note
30 查询 学生200215121 选修课程的 总的 学分数.note
五.Group by子句
32 查询 选修 3门 以及 以上 课程 的 学生 学号.note
select sno from sc
group by sno
having count(*)>=3
31 求各个 课程号 及其 相应的 选课人数.note