1.查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
#解题技巧,先写框架
select*
from sc a
left join student d
on a.sid=d.sid
inner join sc b
on a.sid=b.sid and a.cid='01'and b.cid='02'
wherea.score>b.score;
2.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为null)
select a.sid,a.sname,
count(b.cid) as cons,
sum(b.score) as cours from
student a left join sc b on a.sid=b.sid
group by a.sid;
3.查询学过「张三」老师授课的同学的信息
select b.* from
sc a left join student b
on a.sid=b.sid
where cid in (select cid from course
where tid=(select tid from teacher where tname='张三')
)group by sid;
4.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
select distinct b.*from
sc a left join student b
on a.sid=b.sid
where cid in(select cid
from sc where sid='01');
5.查询没学过"张三"老师讲授的任一门课程的学生姓名
select * from student a
where a.sid not in (select sid from
sc a left join course c on a.cid=c.cid inner join teacher
d on c.tid=d.tid and d.tname='张三');
6.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select*,
avg(score) as avg_sco
from sc a left join student b
on a.sid=b.sid
where a.score<60
group by a.sid having count(cid)>=2;
7.按各科成绩进行排序,并显示排名, Score 重复时也继续排名
select sid,cid,score,
@rank:=@rank+1 as rn
from sc , (select @rank:=0) as t order by score desc;
8.查询每门课程被选修的学生数
select cid,count(1)
as cons from
sc group by cid;
9.查询出只选修两门课程的学生学号和姓名
select student.SId,student.Sname
from sc,student where student.SId=sc.SId
group by sc.SId having count(*)=2;
10.查询男生、女生人数
select count (1) as cons,ssex
from student groupby ssex;
11.查询名字中含有「风」字的学生信息
select * from student where sname like'%风%';
12.查询 1990 年出生的学生名单
select * from student where year(sage)='1990';
13.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
select * from sc a
left join student b
on a.sid=b.sid
left join course c
on a.cid=c.cid
where a.score>70;
14.求每门课程的学生人数
select sc.CId,count(*) as 学生人数 from
sc group by sc.CId;
15.查询各学生的年龄,只按年份来算
select *, year (now())-year(sage) as age
fromstudent;