数据库--学习笔记2

来源:互联网 发布:淘宝号星级怎么算的 编辑:程序博客网 时间:2024/06/11 17:42

数据查询  :

  一、目的与要求

1.掌握SELECT语句的基本语法。

2.掌握子查询的表示。

3.掌握连接查询的表示。

4.掌握SELECT语句的统计函数的作用和使用方法。

5.掌握SELECT语句的GROUPBY和ORDERBY子句的作用和使用方法。


二、实验准备

1.了解SELECT语句的基本语法格式。

2.了解SELECT语句的执行方法。

3.了解子查询的表示方法。

4.了解SELECT语句的统计函数的作用。

5.了解SELECT语句的GROUP BY和ORDER BY子句的作用。


三、实验步骤

1.在SQL查询分析器中执行,生成STUDENT、 COURSE、 SC 三张表。将所有运行通过的SQL语句保存在文档中。

请认真观察每张表的表名、属性及表之间的联系

2.SELECT语句的基本使用

(1)分别查询STUDENT、 COURSE、 SC的基本信息.

今天学习了数据库的多表查询:

select student.*,sc.*  from student ,sc where student.sno=sc.sno; -- 1 查询每个学生及其选课情况select student.sno,sname,ssex,sage,sdept,cno,grade from student ,sc where student.sno=sc.sno;--2查询每个学生及选修课的课程情况,去掉重复性select first.Cname,second.Cpno from Course first,Course second where first.Cpno=second.Cno--3查询每门课的间接先修课select *from student right join sc on student.sno=sc.sno--4将STUDENT,SC进行右连接<pre name="code" class="sql">//5查询既选修了2号课程又选修了3号课程的学生姓名、学号;
--6查询和刘晨同一年龄的学生select sc.sno,student.sname from sc,student where cno='3' intersect select sc.sno,student.sname from sc,student where cno='2';select * from student where sage=(select sage from student where sname='刘晨' );<pre name="code" class="sql">--7选修了课程名为“数据库”的学生姓名和年龄

select student.sno,sname from student,sc,course where student.sno=sc.snoand course.cname='数据结构';


//8查询其他系比IS系任一学生年龄小的学生名单
select sname,sage from student where sdept<>'IS'and sage<any (select sage from student where sdept='IS') order by sage desc; 


//9查询其他系中比IS系所有学生年龄都小的学生名单
select sname,sage from student where sdept<>'IS'and sage<all (select sage from student where sdept='IS') order by sage desc; 


//10查询选修了全部课程的学生姓名查询选修课程1的学生集合和选修2号课程学生集合的差集

-11查询计算机系学生及其性别是男的学生

select sname from student where sno in (select Sno from SC group by Sno having count(*) = (select count(*) from course )) select * from student where sdept='cs' intersect select * from student where ssex='男';-


-12查询选修课程1的学生集合和选修2号课程学生集合的差集

select sno from sc where cno='1'except select sno from sc where cno='2'-


//13查询李丽同学不学的课程的课程号

select cno from course where cno not in (select cno from sc where sno in(select sno from student where sname='李丽')); select AVG(sage) from student ,sc where sc.cno='3' and student .sno=sc.sno 


//14查询选修了3号课程的学生平均年龄 select sno,AVG(grade),MAX(grade),MIN(grade) from sc group by sno;

//15求每门课程学生的平均成绩,(最大值,最小值) //16 统计每门课程的学生选修人数(超过3人的才统计)。要求输出课程号和选修人数,结果按人数降序排列,若人数相同,按课程号升序排列select cno,COUNT(sno) as num from sc where cno in(select cno from course) group by cno having COUNT (sno)>3 order by COUNT (sno) desc,cno asc

//17查询学号比刘晨大,而年龄比他小的学生姓名
select sname from student where sno>(select sno from student where sname='刘晨') and sage<(select sage from student where sname='刘晨'); 
//18求年龄大于所有女同学年龄的男同学姓名和年龄select sname,sage from student where sage>(select AVG(sage) as av_age from student where ssex ='女') and ssex='男'; 










0 0
原创粉丝点击