-- 统计每个班级 期末成绩的最高分
select GName,max(SCScore3) Max
from student,sc,grade
where student.SId=sc.SId
and student.GId=grade.GId
group by GName
-- 依据输出样例分析 需要的表
-- 多表连接、等值连接
10-121 显示没有班导师的班级名称、院系名称
sql复制代码
select GName,DName
from grade
left join dept on grade.DId=dept.DId
where TId <=> null
-- 筛选符合输出样例的表
-- 以grade表为主,若TId为空则表示没有班导师
select distinct course.cno,cname
from teachers,teaching,course
where teachers.tno=teaching.tno
and teaching.cno=course.cno
and tsex='女'
-- distinct 去重!!!
10-133 检索出students表、sc表中至少选修2门课程的女生姓名
sql复制代码
select sname
from students
where sno in(
select sno
from sc
group by sno
having count(cno)>=2
)
and ssex = '女'
-- 审题!!!
select round(avg(年龄),1) 平均年龄
from (
select sno,2050 - year(bday) 年龄
from students
where sno in (
select sno -- 1.选修这门课的学号
from sc
where cno='0000011'
)
) temp
-- 不会
select students.sno,sname,cno,score
from students
left join sc on students.sno=sc.sno
-- 将students 作为主表
10-136 检索出students表和sc表中没有选课的学生学号和姓名
sql复制代码
-- select sno,sname
-- from students
-- where sno not in (
-- select sno -- 1.查询选了课的学生
-- from sc
-- )
-- 没有选课的学生
select students.sno,sname
from students
left join sc on students.sno=sc.sno
where cno <=> null
-- 将students表作为主表连接,cno = null为没有选课
10-137 检索出students表和sc表中"19信管2"班的学生所选修的课程号
sql复制代码
select cno
from students,sc
where students.sno=sc.sno
and class='19信管2'
10-138 检索出students表和sc表中"陈晓东"同学所选课程的课号及成绩
sql复制代码
select cno,score
from students,sc
where students.sno=sc.sno
and sname='陈晓东'
10-139 检索出students表中出生日期大于所有女同学出生日期的男同学的姓名及系别
sql复制代码
-- 出生日期 大于所有女同学出生日期 的男同学 的姓名及系别
-- select distinct sname,sdept
-- from students
-- where bday > all(
-- select bday
-- from students
-- where ssex='女'
-- )
-- and ssex='男'
select sname,sdept
from students
where bday > (
select max(bday)
from students
where ssex='女'
)
and ssex='男'
-- 大于所有 或者 比最大的都大
10-140 检索出students表、sc表中信息学院女学生的学生学号、姓名、课号及考试成绩。
sql复制代码
select sc.sno,sname,cno,score
from students,sc
where students.sno=sc.sno
and sdept='信息学院'
and ssex='女'
select distinct cname
from teachers
join teaching on teachers.tno=teaching.tno
join course on teaching.cno=course.cno
where tname='王珊'
-- distinct !!!
10-143 查询所授每门课程平均成绩均在70分以上的教师(MSSQL)
sql复制代码
select CNO cno
from sc
group by CNO
having avg(GRADE)>70
-- 1.先处理sc表
-- 平均成绩大于70的cno
10-144 检索Student表中与'张三'在同一个专业的学生记录(MSSQL字符匹配)
sql复制代码
select distinct sno 学号,sname 姓名
from stu
where mno in(
select mno-- 1.查询张三的专业
from stu
where sname = N'张三'
)
-- and not sname = N'张三'
-- and sname <> N'张三'
and sname != N'张三'
-- MSSQL字符串赋值,需要在字符串前面加 N