/********************************************
例1查询每个学生及其选修课程的情况
*********************************************/
代码如下 复制代码
select student.*,sc.* from sc,student where sc.sno = student.sno;
/********************************************
对例1用自然连接完成
*********************************************/
代码如下 复制代码
select student.sname,student.ssex,student.sage,student.sdept,sc.*
from sc,student
where sc.sno = student.sno;
/*****************************************
查询每一门课程的间接先修课
*****************************************/
代码如下 复制代码
select sy.cno,sx.cpno from course sx,course sy where sx.cno = sy.cpno
/********************************************
对例1用左外连接连接完成
*********************************************/
代码如下 复制代码
select student.sname,student.ssex,student.sage,student.sdept,sc.*
from student left join sc on(sc.sno = student.sno);
/********************************************
查询每个学生的学号、姓名、选修的课程名及成绩 *********************************************/
代码如下 复制代码
select student.sno,student.sname,course.cname,course.ccredit
from sc,student,course
where sc.cno =course.cno and sc.sno = student.sno;
/********************************************
查询与刘晨在同一个系学习的学生
*********************************************/
代码如下 复制代码
select sx.*
from student sx
where sx.sdept in (
select sy.sdept from student sy where sy.sname='刘晨'
);
/************
或者如下:
代码如下 复制代码
select sx.*
from student sx, student sy
where sx.sdept = sy.sdept and sy.sname = '刘晨'; 或者:
select *
from student sx
where exists (
select * from student sy where sy.sdept = sx.sdept and sy.sname='刘晨'
); **************/
/********************************************
查询选修了课程名为信息系统的学生学号和姓名 *********************************************/
代码如下 复制代码
select sno, sname
from student
where sno in (
select sno from sc
where cno in (
select cno from course
where cname = '信息系统'
)
);
/********************************************
找出每个学生超过他选修课程平均成绩的课程号
*********************************************/
代码如下 复制代码
select sx.sno,sx.cno
from sc sx
where sx.grade > (
select avg(grade)
from sc sy
where sx.sno = sy.sno
);
/*****************************************************
查询其他系中比计算机科学系某一学生年龄小的学生姓名和年龄
*******************************************************/
代码如下 复制代码
select sname, sage
from student
where sage select sage
from student
where sdept='cs'
)
and sdept'cs';
/*****************************************************
查询其他系中比计算机科学系所有学生年龄小的学生姓名和年龄
*******************************************************/
代码如下 复制代码
select sname, sage
from student
where sage select sage
from student
where sdept='cs'
)
and sdept'cs';
/*****************************************************
查询所有选修了1号课程的学生姓名
*******************************************************/
代码如下 复制代码
select sname
from student
where exists (
select * from sc
where sc.sno = student.sno and sc.cno='1'
);
/******************************************
查询选修了全部课程的学生姓名
*******************************************/
代码如下 复制代码
select sname
from student
where not exists (
select *
from course
where not exists (
select *
from sc
where student.sno = sc.sno and course.cno = sc.cno
)
);
/*****************************************************
查询至少选修了学生200215122选修的全部课程的学生号码
*****************************************************/
代码如下 复制代码
select distinct sno
from sc x
where not exists (
select *
from sc y
where sno='200215122' and not exists (
select *
from sc z
where z.sno = x.sno and y.cno = z.cno
)
);
/**********************************************************
从自身表中选择一条记录,修改某个字段再回插到自身表中
这里的insert into 可以用来插入子查询 *********************************************************/
代码如下 复制代码
insert into course(cno,cname,cpno,ccredit)
select '8',cname,cpno,ccredit from course where cno='2';
