练习主要涉及四张表,分别如下:
student(sid,sname,sage,ssex) 学生表
course(cid,cname,tid) 课程表
score(sid,cid,score) 成绩表
teacher(tid,tname) 教师表
首先建立表结构
CREATE TABLE student
(
sid INT,
sname varchar (32),
sage INT,
ssex varchar (8)
);
CREATE TABLE course
(
cid INT,
cname varchar(32),
tid INT
);
CREATE TABLE score
(
sid INT,
cid INT,
score INT
);
CREATE TABLE teacher
(
tid INT,
tname varchar(16)
);
插入数据
--oracle
insert into student select 1,'刘一',18,'男' FROM dual union all
select 2,'钱二',19,'女' FROM dual union all
select 3,'张三',17,'男' FROM dual union all
select 4,'李四',18,'女' FROM dual union all
select 5,'王五',17,'男' FROM dual union all
select 6,'赵六',19,'女' FROM dual;
insert into teacher select 1,'叶平' FROM dual union all
select 2,'贺高' FROM dual union all
select 3,'杨艳' FROM dual union all
select 4,'周磊' FROM dual;
insert into course select 1,'语文',1 FROM dual union all
select 2,'数学',2 FROM dual union all
select 3,'英语',3 FROM dual union all
select 4,'物理',4 FROM dual;
insert into score
select 1,1,56 FROM dual union all
select 1,2,78 FROM dual union all
select 1,3,67 FROM dual union all
select 1,4,58 FROM dual union all
select 2,1,79 FROM dual union all
select 2,2,81 FROM dual union all
select 2,3,92 FROM dual union all
select 2,4,68 FROM dual union all
select 3,1,91 FROM dual union all
select 3,2,47 FROM dual union all
select 3,3,88 FROM dual union all
select 3,4,56 FROM dual union all
select 4,2,88 FROM dual union all
select 4,3,90 FROM dual union all
select 4,4,93 FROM dual union all
select 5,1,46 FROM dual union all
select 5,3,78 FROM dual union all
select 5,4,53 FROM dual union all
select 6,1,35 FROM dual union all
select 6,2,68 FROM dual union all
select 6,4,71 FROM dual;
--mysql
insert into student values (1,'刘一',18,'男'),
(2,'钱二',19,'女'),
(3,'张三',17,'男'),
(4,'李四',18,'女'),
(5,'王五',17,'男'),
(6,'赵六',19,'女');
insert into teacher values (1,'叶平'),
(2,'贺高'),
(3,'杨艳'),
(4,'周磊');
insert into course values (1,'语文',1),
(2,'数学',2),
(3,'英语',3),
(4,'物理',4);
insert into score values
(1,1,56),
(1,2,78),
(1,3,67),
(1,4,58),
(2,1,79),
(2,2,81),
(2,3,92),
(2,4,68),
(3,1,91),
(3,2,47),
(3,3,88),
(3,4,56),
(4,2,88),
(4,3,90),
(4,4,93),
(5,1,46),
(5,3,78),
(5,4,53),
(6,1,35),
(6,2,68),
(6,4,71);
练习题Beginning!!!
1.查询“001”课程比“002”课程成绩高的所有学生的学号
select a.sid from (select sid,score from score where cid=001) a,(select sid,score from score where cid=002) b where a.sid=b.sid and a.score>b.score;
2.查询平均成绩大于60分的同学的学号和平均成绩
--oracle
select sid,avg(nvl(score,0)) from score group by sid having avg(nvl(score,0))>60;
--nvl(字段,为空返回值)
--nvl2(字段,不为空返回值,为空返回值)
--mysql
select sid,avg(ifnull(score,0)) from score group by sid having avg(ifnull(score,0))>60;
--补充:MySQL中如何查询的字段中可能存在null值,可以做一些处理
--1.isnull(exper) 判断exper是否为空,是则返回1,否则返回0
--2.ifnull(exper1,exper2)判断exper1是否为空,是则用exper2代替
--3.nullif(exper1,exper2)如果expr1= expr2 成立,那么返回值为NULL,否则返回值为expr1。
3.查询所有同学的学号、姓名、选课数、总成绩
select t1.sid,t1.sname,count(t2.cid),sum(t2.score) from student t1 left join score t2 on t1.sid=t2.sid group by t1.sid,sname;
4.查询姓“李”的老师的个数
select count(distinct tname) from teacher where tname like '李%';
或
select count(distinct tname) from teacher where substr(tname,0,1)='李';
5.查询没学过“叶平”老师课的同学的学号、姓名
select sid,sname from student where sid not in
(select distinct t1.sid from score t1 left join course t2 on t1.cid=t2.cid where t2.tid=(select tid from teacher where tname='叶平'));
6.查询学过“001”并且也学过编号“002”课程的同学的学号、姓名
select sid,sname from student where sid in (select sid from score where cid=001 or cid=002 group by sid having count(cid)>1);
或
select t1.sid,t1.sname from student t1 left join score t2 on t1.sid=t2.sid where t2.cid=001 and exists (select * from score t3 where t2.sid=t3.sid and t3.cid=002);
7.查询学过“叶平”老师所教的所有课的同学的学号、姓名
select sid,sname from student where sid in
(select sid from score t1,course t2,teacher t3 where t1.cid=t2.cid and t2.tid=t3.tid and t3.tname='叶平' group by t1.sid having count(distinct t2.cid)=
(select count(distinct course.cid) from course left join teacher on course.tid=teacher.tid where teacher.tname='叶平'));
8.查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名
SELECT a.sid,a.sname from
(select t1.sid,t1.sname,t2.score from student t1,score t2 where t1.sid=t2.sid and t2.cid=001) a,
(select t1.sid,t1.sname,t2.score from student t1,score t2 where t1.sid=t2.sid and t2.cid=002) b
where a.sid=b.sid and b.score < a.score;
或
select sid,sname from (
select t1.sid,t1.sname,t2.score,(select t3.score from score t3 where t1.sid=t3.sid and t3.cid=002) score2
from student t1,score t2 where t1.sid=t2.sid and t2.cid=001) where score2 < score;
9.(1)查询所有课程成绩都小于60分的同学的学号、姓名
select t1.sid,t1.sname from student t1 left join score t2 on t1.sid=t2.sid group by t1.sid,sname having max(t2.score)<60;
(2)查询所有课程中,存在成绩小于60分的同学的学号、姓名
select sid,sname from student where sid not in (select t1.sid from student t1 left join score t2 on t1.sid=t2.sid and t2.score>60);
或
select t1.sid,t1.sname from student t1 left join score t2 on t1.sid=t2.sid group by t1.sid,sname having min(t2.score)<60;
10.查询没有学全所有课的同学的学号、姓名
select t1.sid,t1.sname from student t1 left join score t2 on t1.sid=t2.sid group by t1.sid,sname having count(t2.cid)<(select count(cid) from course);
11.查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名
select distinct t1.sid,t1.sname from student t1 left join score t2 on t1.sid=t2.sid where t2.cid in (select distinct cid from score where sid=001) and t1.sid <>001;
12.把“score”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩
update score set score=(select avg(score) from score where cid=
(select t1.cid from course t1,teacher t2 where t1.tid=t2.tid and t2.tname='叶平')) where cid=
(select t1.cid from course t1,teacher t2 where t1.tid=t2.tid and t2.tname='叶平');
13.查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名;
SELECT t1.sid,t1.sname from student t1,score t2 where t1.sid=t2.sid and t2.cid in
(select distinct cid from score where sid=002) and t1.sid != 002 group by t1.sid,t1.sname
having count(distinct t2.cid) = (select count(distinct cid) from score where sid=002);
14.删除学习“叶平”老师课的SC表记录;
delete from score where cid in (select t1.cid from course t1 left join teacher t2 on t1.tid=t2.tid where t2.tname='叶平');
15.向score表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、2号课的平均成绩
insert into score(sid,cid,score) select sid,'002',(select avg(score) from score where cid='002') from student
where sid not in (select sid from score where cid='003');
16.按平均成绩从高到低显示所有学生的“语文”、“数学”、“英语”、“物理”四门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,物理,有效课程数,有效平均分
详细内容点击48道SQL练习题(Oracle+MySQL)