0329作业
#1、查询“001”课程比“002”课程成绩高的所有学生的学号;
SELECT
a.stuid 学号
FROM
tblscore a,
tblscore b
WHERE
a.Score > b.Score
AND a.CourseId = 001
AND b.CourseId = 002
AND a.StuId = b.stuid;
#2、查询平均成绩大于60分的同学的学号和平均成绩;
SELECT
sc.stuid 学号,
AVG( score ) 平均成绩
FROM
tblscore sc
GROUP BY
sc.stuid
HAVING
avg( score ) > 60
ORDER BY
avg( score ) DESC;
#3、查询学习过‘001’课程的男生和女生人数各是多少?
SELECT
st.stusex 性别,
count( 1 ) 人数
FROM
tblstudent st,
tblscore sc
WHERE
sc.CourseId = 001
AND st.StuId = sc.StuId
GROUP BY
st.stusex;
#4、查询姓“李”的老师的个数;
SELECT
count( 1 ) '个数'
FROM
tblteacher tea
WHERE
tea.teaname LIKE '李%';#5、查询“张无忌”的所有学习课程名称和授课老师姓名;
SELECT
coursename 课程名称,
teaname 授课老师
FROM
tblcourse cou,
tblteacher tea
WHERE
cou.teaid = tea.teaid
AND cou.CourseId IN ( SELECT CourseId FROM tblscore WHERE StuId IN ( SELECT stuid FROM tblstudent WHERE stuname = '张无忌' ) ) #6、查询每一门课程的课程名称,授课教师姓名,课程平均成绩;
select coursename 课程名称,teaname 授课老师,avg(score) 平均成绩
from tblcourse cou,tblteacher tea,tblscore sco where cou.teaid=tea.teaid and cou.CourseId=sco.CourseId
GROUP BY cou.courseid
ORDER BY avg(score) desc
;
#7、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
select stu.stuid 学号,stuname 姓名 from tblstudent stu where stu.stuid in(select sco.stuid from tblscore sco where sco.courseid =001) and stu.stuid in(select sco.stuid from tblscore sco where sco.courseid =002);
#8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;
select stu.stuid 学号,stuname 姓名 from tblstudent stu where (select score from tblscore sco2 where courseid=002 and sco2.StuId=stu.StuId) < (select score from tblscore sco1 where courseid=001 and sco1.StuId=stu.StuId )
#9、查询已经学完所有课程的同学的学号、姓名;
select stu.stuid 学号,stuname 姓名 from tblstudent stu where stu.StuId in(select sco.stuid from tblscore sco where sco.CourseId in (select cou.CourseId from tblcourse cou)) = (select count(1) from tblcourse cou)
Select StuId,StuName From tblStudent st
Where (Select Count(*) From tblScore sc Where st.StuId=sc.StuId)=
(Select Count(*) From tblCourse)
#10、查询课程补考过的学生学号,课程号;[同一门课程成绩存在两次代表补考]
select sstu 学号,scou 课程号 from tblstudent stu,(select count(1) coun,sco.stuid sstu,sco.courseid scou from tblscore sco
GROUP BY sco.courseid,sco.stuid) bukao where bukao.coun>1 and bukao.sstu=stu.stuid
#11、查询所有同学的学号、姓名、选课数、总成绩;
#拆分
SELECT stuid 学号,stuname 姓名,() 选课数,() 总成绩 from tblstudent stu
#选课数
select count(1) from tblscore sco1 where sco1.stuid=stu.stuid
#总成绩
select sum(score) from tblscore sco2 where sco2.stuid=stu.stuid
#合成
SELECT stuid 学号,stuname 姓名,(select count(1) from tblscore sco1 where sco1.stuid=stu.stuid) 选课数,(select sum(score) from tblscore sco2 where sco2.stuid=stu.stuid) 总成绩 from tblstudent stu
#12、创建一个查询视图,视图中包括学生学号,学生姓名,授课教师编号,教师姓名,课程编号,课程名称,成绩,查询出视图中的内容
#13、查出”周芷若”同学所有未选课程编号和课程名称
SELECT cou.courseid 课程编号,coursename 课程名称 from tblcourse cou where cou.courseid not in (select sco.courseid from tblscore sco where sco.stuid in(select stu.stuid from tblstudent stu where stuname='周芷若'))
#14、查出和”]周芷若”同学一起上过课的所有同学学号和姓名
#实现思路通过名字查出学号,通过学号,查询出学生学习过的课程,
#使用where in进行筛选学过这些课程的学生信息,去除重复值,去除‘周芷若’
#上过的课程编号
#通过课程编号筛选出学生id
#筛选
select stu.stuid 学号,stuname 姓名 from tblstudent stu where stuid in(select sco.stuid from tblscore sco where sco.CourseId in(SELECT cou.courseid 课程编号 from tblcourse cou where cou.courseid in (select sco.courseid from tblscore sco where sco.stuid in(select stu.stuid from tblstudent stu where stuname='周芷若')))) and stuname<>'周芷若'