sql 语言查询.doc
文本预览下载声明
一、实验目的
掌握利用SQL语句完成各种查询操作的能力。
二、实验内容
给定表结构如下:
学生表:student(主键Sno)
学号
Sno 姓名
Sname 性别
Ssex 年龄
Sage 所在系
Sdept 95001 李勇 男 20 CS 95002 刘晨 女 21 IS 95003 王敏 女 18 MA 95004 张力 男 19 IS 课程表:Course(主键Cno)
课程号
Cno 课程名
Cname 先行课
Cpno 学分
Ccredit 1 数据库 5 4 2 数学 2 3 信息系统 1 4 4 操作系统 6 3 5 数据结构 7 4 6 数据处理 2 7 PASCAL语言 6 4 选课表:SC(主键Sno,Cno,外部键Sno,Cno)
学号
Sno 课程表
Cno 成绩
Grade 95001 1 92 95001 2 85 95001 3 88 95002 2 90 95002 3 85 95003 3 59 用SQL语句完成以下的要求
四、试验结果
1.查询信息系(IS)的所有学生信息
select *
from student
where sdept IS;
2.查询选修了“数学”课的所有学生名单
select sname
from student,sc,course
where sc.cno course.cno and sc.sno student.sno and course.cname 数学
select sname
from student,sc,course
where sc.cno course.cno and sc.sno student.sno and course.cpno 5
4.查询全体学生的姓名和出生年份。
select sname,Year of Birth BIRTH,2008-sage BIRTHDAY
from student;
5.查询所有姓王的学生。
select *
from student
where sname like 王%;
select sname,grade
from student,sc
where sc.sno student.sno and sc.cno 3
order by grade desc; //可有可无,缺省为升序
select *
from student
order by sdept,sage desc;
8.计算2号课程的平均成绩。
select avg grade AVG
from sc
where cno 2;
9.查询选修了2号课程的学生的最高成绩。
select max grade MAX
from sc
where cno 2;
10.求各个课程号及相应的选课人数。
select course.cno,count sno
from course left join sc //外连接
on course.cno sc.cno
group by course.cno;
select sno
from sc
group by sno
having count * 3;
12.查询“数据库”的间接先行课。
select b.cname
from course a,course b
where a.cpno b.cno and a.cname 数据库;
select sno,sname
from student
where sno in
select sno
from sc
group by sno
having avg grade all
select avg grade from sc
group by sno 14.查询数学成绩最高的学生的学号和姓名。
select student.sname,student.sno
from student,sc
where sc.sno student.sno and grade in
select max grade
from sc,course
where course.cno sc.cno and course.cname 数学 select sno
from sc
where grade in
select min grade
from sc
and sno in
select max sno
from sc 16.查询成绩高于学生平均成绩的记录。
select *
from sc x
where grade select avg grade
from sc y
where y.sn
显示全部