实验二 SQL语言的基本操作 下载本文

内容发布更新时间 : 2024/5/2 11:14:34星期一 下面是文章的全部内容请认真阅读。

实验二 SQL语言的基本操作

实验目的和要求:

掌握利用SQL语句完成各种查询操作的能力。重点掌握用SELECT语句进行各种查询; 掌握INSERT语句的用法。 实验内容:

用SQL语句完成一下的要求: 1.查询信息系(IS)的所有学生信息

select * from student where sdept=’is’

2.查询选修了“数学”课的所有学生名单 Select s.sno,sname From student s,course c,sc

Where s.sno=sc.sno and sc.cno=c.cno and cname=’数学’

3.查询至少选修了一门其直接先行课为5号课程的学生的姓名。 Select sname

From student s, sc, course c

Where s.sno=sc.sno and sc.cno=c.cno and pcno=’5’ 4.查询全体学生的姓名和出生年份。

select sname,year(now())-sage as '出生年份' from student

5.查询所有姓王的学生。

select * from student where sname like '王%'

6.查询选修了3号课程的学生姓名及成绩,并按成绩降序排序。 Select sname,grade From student s, sc

Where s.sno=sc.sno and sc.cno=’3’ Order by grade desc

7.查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列。

Select * From student

Order by sdept asc,age desc 8.计算2号课程的平均成绩。 Select avg(grade) From sc Where cno=’2’

9.查询选修了2号课程的学生的最高成绩。

select max(grade) from sc where cno='2'

10.求各个课程号及相应的选课人数。 Select cno as 课程号,count(sno) as 人数 From sc Group by cno

11.查询至少选修了3门课程以上的学生学号。

select sno

from sc group by sno having count(*)>2

12.查询“数据库”的间接先行课。 Select c3.cname

From course c1,course c2,course c3

Where c1.cpno=c2.cno and c1.cname=’数据库’ and c2.cpno=c3.cno 13.查询平均成绩最高的学生的学号和姓名。 select top 1 sno,avg(grade) from sc group by sno

order by avg(grade) desc

14.查询数学成绩最高的学生的学号和姓名。 select top 1 s.sno,sname,grade from student s,course c, sc

where s.sno=sc.sno and c.cno=sc.cno and cname='数学' order by grade desc

15.查询出成绩最低学号最大的学生学号。 select top 1 sc.sno,grade from sc

order by grade asc,sno desc

16.查询成绩高于学生平均成绩的记录。 Select * From sc

Where grade>(select avg(grade)

From sc )

17.查询至少选修了1号课程和3号课程的学生学号。 Select sc1.sno From sc sc1,sc sc2

Where sc1.sno=sc2.sno and sc1.cno=’1’ and sc2.cno=’3’ 18.查询只选修了1号课程和3号课程的学生学号。 select sno from sc

where cno=’1’ and sno in( select sno from sc where cno=’3’)

and sno in(select sno from sc group by sno having count(cno)=2) 19.查询没有选修1号课程的学生姓名。 Select distinct s.sname From student s, sc

Where s.sno=sc.sno and sc.cno!='1' 20.查询选修了全部课程的学生姓名。 Select sname From student s

Where not exist (select * From course c

Where not exist (select *