表一:student_info
学号 姓名 性别 出生年月 家庭住址 备注
0001 张三 男 1981-8-9 北京 NULL
表二:curriculum
课程编号 课程名称 学分
0001 计算机基础 2
0002 C语言 2
表三:grade
学号 课程编号 分数
0001 0001 80
0001 0002 90
a. 条件查询
- 在GRADE表中查找80-90份的学生学号和分数
select 学号,分数 from grade where 分数 between 80 and 90 - 在GRADE 表中查找课程编号为003学生的平均分
select avg(分数) from grade where 课程编号=‘003’ - 在GRADE 表中查询学习各门课程的人数
Select课程编号,count(学号) as 人数from grade group by 课程编号 - 查询所有姓张的学生的学号和姓名
select 姓名,学号 from student_info where 姓名 like '张%’
b. 嵌套查询 - 查询和学号’0001’的这位同学性别相同的所有同学的姓名和出生年月
select 姓名,出生年月 from student_info where 性别 in(select 性别 from student_info where sno=‘0001’) - 查询所有选修课程编号为0002 和0003的学生的学号、姓名和性别
select 学号,姓名,性别 from student_info where 学号 in(select 学号 from grade where 课程编号=‘0002’ and 学号 in(select 学号 from grade where 课程编号=‘0001’)) - 查询出学号为0001的学生的分数比0002号学生最低分高的课程编号的课程编号和分数
select 课程编号, 分数 from grade where 学号=‘0001’ and 分数>(select min(分数) from grade where 学号=‘0002’)
c. 多表查询
- 查询分数在80-90分的学生的学号、姓名、分数
select student_info.学号,student_info.姓名,grade.分数 from student_info,grade where grade.分数 between 80 and 90 - 查询学习了’C语言’课程的学生学号、姓名和分数
select student_info.学号,student_info.姓名,grade.成绩from student_info,grade,curriculum where student_info.学号=grade.学号and grade.课程号=curriculum.课程号and curriculum.课程名='C语言’ - 查询所有学生的总成绩,要求列出学号、姓名、总成绩,没有选课的学生总成绩为空
select grade.学号,student_info.姓名,sum(grade.成绩) as 总成绩from student_info,grade where grade.学号=student_info.学号group by grade.学号,student_info.姓名