时间:2021-07-01 10:21:17 帮助过:2人阅读
-----插入数据
1 /*******初始化学生表的数据**********/ 2 insert into student values (‘s001‘,‘张三‘,23,‘男‘); 3 insert into student values (‘s002‘,‘李四‘,23,‘男‘); 4 insert into student values (‘s003‘,‘吴鹏‘,25,‘男‘); 5 insert into student values (‘s004‘,‘琴沁‘,20,‘女‘); 6 insert into student values (‘s005‘,‘王丽‘,20,‘女‘); 7 insert into student values (‘s006‘,‘李波‘,21,‘男‘); 8 insert into student values (‘s007‘,‘刘玉‘,21,‘男‘); 9 insert into student values (‘s008‘,‘萧蓉‘,21,‘女‘); 10 insert into student values (‘s009‘,‘陈萧晓‘,23,‘女‘); 11 insert into student values (‘s010‘,‘陈美‘,22,‘女‘); 12 insert into student values (‘s011‘,‘陈美‘,23,‘女‘); 13 commit; 14 /******************初始化教师表***********************/ 15 insert into teacher values (‘t001‘, ‘刘阳‘); 16 insert into teacher values (‘t002‘, ‘谌燕‘); 17 insert into teacher values (‘t003‘, ‘胡明星‘); 18 commit; 19 /***************初始化课程表****************************/ 20 insert into course values (‘c001‘,‘J2SE‘,‘t002‘); 21 insert into course values (‘c002‘,‘Java Web‘,‘t002‘); 22 insert into course values (‘c003‘,‘SSH‘,‘t001‘); 23 insert into course values (‘c004‘,‘Oracle‘,‘t001‘); 24 insert into course values (‘c005‘,‘SQL SERVER 2005‘,‘t003‘); 25 insert into course values (‘c006‘,‘C#‘,‘t003‘); 26 insert into course values (‘c007‘,‘JavaScript‘,‘t002‘); 27 insert into course values (‘c008‘,‘DIV+CSS‘,‘t001‘); 28 insert into course values (‘c009‘,‘PHP‘,‘t003‘); 29 insert into course values (‘c010‘,‘EJB3.0‘,‘t002‘); 30 commit; 31 /***************初始化成绩表***********************/ 32 insert into sc values (‘s001‘,‘c001‘,78.9); 33 insert into sc values (‘s002‘,‘c001‘,80.9); 34 insert into sc values (‘s003‘,‘c001‘,81.9); 35 insert into sc values (‘s004‘,‘c001‘,60.9); 36 insert into sc values (‘s001‘,‘c002‘,82.9); 37 insert into sc values (‘s002‘,‘c002‘,72.9); 38 insert into sc values (‘s003‘,‘c002‘,81.9); 39 insert into sc values (‘s001‘,‘c003‘,‘59‘); 40 insert into sc values (‘s001‘,‘c007‘,‘90‘); 41 insert into sc values (‘s001‘,‘c010‘,‘90‘); 42 insert into sc values (‘s002‘,‘c007‘,72.9); 43 insert into sc values (‘s002‘,‘c010‘,72.9); 44 insert into sc values (‘s002‘,‘c003‘,72.9); 45 insert into sc values (‘s006‘,‘c003‘,76); 46 insert into sc values (‘s001‘,‘c005‘,‘58‘); 47 commit;
-----题目
-----答案
1 --1、查询“c001”课程比“c002”课程成绩高的所有学生的学号; 2 select s1.sno 3 from sc s1 4 join sc s2 on s1.sno = s2.sno 5 where s1.cno = ‘c001‘ 6 and s2.cno=‘c002‘ 7 and s1.score>s2.score; 8 9 --2、查询平均成绩大于60 分的同学的学号和平均成绩; 10 select sno,avg(score) 11 from sc 12 group by sno 13 having avg(score)>60; 14 15 --3、查询所有同学的学号、姓名、选课数、总成绩; 16 select s2.sno,s1.sname,count(*),sum(score) 17 from student s1,sc s2 18 where s1.sno = s2.sno 19 group by s2.sno,s1.sname; 20 21 --4、查询姓“刘”的老师的个数; 22 select count(*) 23 from teacher 24 where tname like ‘刘%‘; 25 26 --5、查询没学过“谌燕”老师课的同学的学号、姓名; 27 select s1.sno,s1.sname 28 from student s1 29 where s1.sno not in (--得到学习这个老师课程的学生 30 select distinct s2.sno 31 from sc s2 32 where s2.cno in (--得到这个老师的课程 33 select c.cno 34 from course c 35 where c.tno = (--得到这个老师的tno 36 select tno 37 from teacher 38 where tname =‘谌燕‘))); 39 40 --6、查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名; 41 select s2.sno,s1.sname 42 from student s1,sc s2,sc s3 43 where s1.sno = s2.sno 44 and s2.cno = ‘c002‘ 45 and s2.sno = s3.sno 46 and s3.cno = ‘c001‘; 47 48 --7、查询学过“谌燕”老师所教的所有课的同学的学号、姓名; 49 select s1.sno,s1.sname from student s1 50 where not exists ( 51 select * 52 from (select cno from course--查询得到这个老师的所有课程 53 where tno = (--查询得到这个老师的tno 54 select tno from teacher 55 where tname = ‘谌燕‘)) c 56 where not exists ( 57 select * from sc s2 58 where s2.sno = s1.sno 59 and s2.cno = c.cno)) 60 61 --8、查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名; 62 select sno,sname 63 from student 64 where sno in ( 65 select s1.sno 66 from sc s1,sc s2 67 where s1.sno= s2.sno 68 and s1.cno = ‘c001‘ 69 and s2.cno = ‘c002‘ 70 and s1.score>s2.score 71 ) 72 73 --9、查询所有 课程成绩小于60 分的同学的学号、姓名; 74 select sno,sname 75 from student 76 where sno in ( 77 select sno 78 from sc s2 79 where s2.score<60); 80 /************第二种*******************/ 81 select s2.sno,s1.sname---更优化 82 from student s1 83 join sc s2 on s1.sno = s2.sno 84 where s2.score<60; 85 86 --10、查询没有学全所有课的同学的学号、姓名; 87 select sno,sname 88 from student 89 where sno not in ( 90 select sno 91 from sc 92 group by sno 93 having count(*) >= ( 94 select count(distinct cno) 95 from course) 96 ) order by sno 97 98 --11、查询至少有一门课与学号为“s001”的同学所学相同的同学的学号和姓名; 99 select distinct s2.sno,s1.sname 100 from student s1 101 join sc s2 102 on s1.sno = s2.sno 103 where s2.cno in ( 104 select cno 105 from sc 106 where sno = ‘s001‘) 107 108 --12、查询至少学过学号为“s001”同学所有一门课的其他同学学号和姓名; 109 select distinct s2.sno,s1.sname 110 from student s1 111 join sc s2 112 on s1.sno = s2.sno 113 where s2.sno <> ‘s001‘ 114 and s2.cno in ( 115 select cno 116 from sc 117 where sno = ‘s001‘) 118 119 --13、把“SC”表中“谌燕”老师教的课的成绩都更改为此课程的平均成绩; 120 update sc s1 121 set score = ( 122 select round(avg(s2.score),1) 123 from sc s2 124 where s2.cno = s1.cno) 125 where cno in ( 126 select cno 127 from course 128 where tno = ( 129 select tno 130 from teacher 131 where tname=‘谌燕‘)); 132 133 --14、查询和“s001”号的同学学习的课程完全相同的其他同学学号和姓名; 134 select s1.sno,s1.sname 135 from student s1 136 where not exists( 137 select * 138 from (select cno 139 from sc 140 where sno = ‘s001‘) c 141 where not exists( 142 select * 143 from sc s2 144 where s1.sno = s2.sno 145 and s2.cno = c.cno 146 and s2.sno <> ‘s001‘ 147 ) 148 ) 149 150 --15、删除学习“谌燕”老师课的SC 表记录; 151 delete sc 152 where cno in ( 153 select cno 154 from course 155 where tno = ( 156 select tno 157 from teacher 158 where tname=‘谌燕‘)); 159 160 --16、向SC 表中 插入一些记录,这些记录要求符合以下条件:没有上过编号“c002”课程的同学学号、“c002”号课的平均成绩; 161 insert into sc (sno,cno,score) 162 select distinct st.sno,sc.cno,( 163 select avg(score) 164 from sc 165 where cno=‘c002‘) 166 from student st,sc 167 where not exists 168 (select * 169 from sc 170 where cno=‘c002‘ 171 and sc.sno=st.sno) 172 and sc.cno=‘c002‘; 173 174 commit; 175 176 --17、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分 177 select cno,max(score),min(score) 178 from sc 179 group by cno; 180 181 --18、按各科平均成绩从低到高和及格率的百分数从高到低顺序 182 select cno,avg(score), 183 sum(case when score>=60 then 1 else 0 end)/count(*) as 及格率 184 from sc 185 group by cno 186 order by avg(score) , 及格率 desc 187 188 --19、查询不同老师所教不同课程平均分从高到低显示 189 select s.cno,avg(s.score) 190 from course c 191 join sc s on c.cno = s.cno 192 group by c.tno,s.cno 193 order by avg(score) desc--MY 194 /***********第二种**************/ 195 <