1.查询emp表中的全部数据
select*from emp;2、查询平均成绩大于60 分的同学的学号和平均成绩;
selectavg(score),sno from sc groupby sno havingavg(score)>603、查询所有同学的学号、姓名、选课数、总成绩;
select sname, c.*from(select sno,count(cno),sum(score)from sc groupby sno) c
join student
on student.sno = c.sno
4、查询姓“刘”的老师的个数;
selectcount(1)from teacher where tname like'刘%'
7、查询学过“谌燕”老师所教的所有课的同学的学号、姓名;
select sno, sname
from student
where sno in(select sno
from sc
where cno in(select cno
from course
where tno in(select tno from teacher where tname ='谌燕'))groupby sno
havingcount(cno)=(selectcount(cno)from course
where tno =(select tno
from teacher
where tname ='谌燕')))8、查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名;
select sno, sname
from student
where sno in(select s1.sno
from sc s1
join sc s2
on s1.cno ='c001'and s2.cno ='c002'and s1.sno = s2.sno
and s1.score > s2.score)9、查询所有课程成绩小于60 分的同学的学号、姓名;
select sno, sname
from student
where sno in(select sno
from sc
where score <60groupby sno
havingcount(cno)=(selectcount(1)from course))10、查询没有学全所有课的同学的学号、姓名;
select sno, sname
from student
where sno in(select sno
from sc
groupby sno
havingcount(cno)=(selectcount(1)from course))
11、查询至少有一门课与学号为“s001”的同学所学相同的同学的学号和姓名
select sno, sname
from student
where sno in(select sno
from sc
where cno in(select cno from sc where sno ='s001')and sno !='s001')12、查询至少学过学号为“s001”同学所有一门课的其他同学学号和姓名;
13、查询和“s002”号的同学学习的课程完全相同的其他同学学号和姓名;
select*from sc
select sno, sname
from student
where sno in(select sno
from sc
where cno in(select cno from sc where sno ='s002')groupby sno
havingcount(cno)=(selectcount(cno)from sc where sno ='s002')intersectselect sno
from sc
groupby sno
havingcount(cno)=(selectcount(cno)from sc where sno ='s002')and sno !='s002')
14、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
select cno,max(score),min(score)from sc groupby cno
15、按各科平均成绩从低到高和及格率的百分数从高到低顺序
selectavg(score) 平均成绩,sum(casewhen score>60then1else0end)/count(sno)*100||'%' 及格率
from sc groupby cno orderby 平均成绩 asc,及格率 desc16、查询不同老师所教不同课程平均分从高到低显示
selectavg(score), cname, tname
from course
leftjoin teacher
on course.tno = teacher.tno
leftjoin sc
on course.cno = sc.cno
groupby cname, tname
orderby117、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[<60]select cname,course.cno,sum(casewhen score<=100and score>=85then1else0end) A,sum(casewhen score<85and score>=70then1else0end) B,sum(casewhen score<70and score>=60then1else0end) C,sum(casewhen score<60then1else0end) D
from course leftjoin sc on course.cno=sc.cno
groupby cname,course.cno
18、查询各科成绩前三名的记录:(不考虑成绩并列情况)SELECT*FROM(SELECT SC.*,
row_number()over(partitionby CNO orderby SCORE desc) R
FROM SC)WHERE R <419、查询每门课程被选修的学生数
SELECT CNAME,COUNT(SNO)FROM COURSE
LEFTJOIN SC
ON COURSE.CNO=SC.CNO
GROUPBY CNAME
20、查询出只选修了一门课程的全部学生的学号和姓名
SELECT SNO,SNAME FROM STUDENT WHERE SNO IN(SELECT SNO FROM SC GROUPBY SNO HAVINGCOUNT(CNO)=1)21、查询男生、女生人数
SELECTSUM(CASEWHEN SSEX='男'then1else0end) 男,sum(casewhen ssex='女'then1else0end) 女
from student
select*from student
22、查询姓“张”的学生名单
select*from student where sname like'张%'23、查询同名同姓学生名单,并统计同名人数
selectcount(sname),sname from student groupby sname havingcount(sname)>1
24、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
selectavg(score), cno from sc groupby cno orderbyavg(score), cno desc25、查询平均成绩大于70 的所有学生的学号、姓名和平均成绩
select sname, s.*from student
join(select sno,avg(score)from sc groupby sno havingavg(score)>70) s
on student.sno = s.sno
26、查询课程名称为“SSH”,且分数低于60 的学生姓名和分数
select sname, score
from student
join(select sno, score
from sc
where cno =(select cno from course where cname ='SSH')and score <60) s
on student.sno=s.sno
27、查询所有学生的选课情况;
select sname, cname
from student
leftjoin sc
on student.sno = sc.sno
leftjoin course
on sc.cno = course.cno
28、查询任何一门课程成绩在70 分以上的姓名、课程名称和分数;
select cname, sname, score
from sc
join student
on sc.sno = student.sno
join course
on sc.cno = course.cno
where score >70
29、查询不及格的课程,并按课程号从大到小排列
select cno from sc where score<60orderby cno desc30、查询课程编号为c001 且课程成绩在80 分以上的学生的学号和姓名;
select sno, sname
from student
where sno in(select sno
from sc
where cno ='c001'and score >80)31、求选了课程的学生人数
selectcount(distinct sno)from sc
32、查询选修“谌燕”老师所授课程的学生中,成绩最高的学生姓名及其成绩
select sname,score from student join(select sno, score
from sc
where cno in(select cno
from course
where tno =(select tno from teacher where tname ='谌燕'))orderby score desc) s
on student.sno=s.sno
where rownum=133、查询各个课程及相应的选修人数
select cname,count(sno)from course leftjoin sc
on course.cno=sc.cno
groupby cname
34、查询不同课程成绩相同的学生的学号、课程号、学生成绩
select s1.*from sc s1
join sc s2
on s1.score = s2.score
and s1.cno != s2.cno
35、查询每门功课成绩最好的前两名
select*from(select cno,
score,
row_number()over(partitionby cno orderby score desc) r
from sc)where r <336、统计每门课程的学生选修人数(超过2 人的课程才统计)。\
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select cno,count(sno)from sc
groupby cno
havingcount(sno)>2orderbycount(sno)desc,cno
37、检索至少选修两门课程的学生学号
select sno from sc groupby sno havingcount(cno)>=238、查询>=3学生都选修的课程的课程号和课程名
select cno, cname
from course
where cno in(select cno from sc groupby cno havingcount(sno)>=3)39、查询没学过“谌燕”老师讲授的任一门课程的学生姓名
select sno, sname
from student
where sno notin(select sno
from sc
where cno in(select cno
from course
where tno in(select tno from teacher where tname ='谌燕')))40、查询两门以上<80课程的同学的学号及其平均成绩
select sno,avg(score)from sc
where score <80groupby sno
havingcount(cno)>=241、检索“c001”课程分数小于80,按分数降序排列的同学学号
select sno
from sc
where score <80and cno ='c001'orderby score desc