(1)查询选修了和王名一样学分数的其它同学姓名。
SELECT sname FROM student
WHERE sno IN (SELECT sno FROM sc,course WHERE sc.cno = course.cno
GROUP BY sno
HAVING sum( ccredit )=(SELECT sum( ccredit ) FROM sc,course
WHERE sc.cno = course.cno AND sno IN ( SELECT sno FROM student WHERE sname ='王名' ) ) AND sname <> '王名' )
(2)查询选修了1号课并且成绩比全班1号课平均成绩低的同学学号及成绩。
SELECT sno ,grade FROM sc
WHERE grade < (SELECT avg(grade) FROM sc where cno= 1) and cno = 1
(3)分别按每年统计出生在1988-1990年三年间的学生人数,平均成绩,(学生人数,平均成绩表示)
select year(student.csrq) as csn,count(student.sno) as 学生人数,avg(grade) as 平均成绩
from student,sc
where student.sno=sc.sno
group by year(student.csrq)
having year(student.csrq) = 1988 or YEAR(csrq) = 1989 or YEAR(csrq) = 1990
(4)查询个人平均成绩比王名同学的最高成绩还要高的同学的账户名和密码。
SELECT user1 ,password1 FROM mm
WHERE sno in (SELECT sno FROM sc
GROUP BY sno
having avg(grade) >(SELECT max(grade) FROM sc WHERE sno in (SELECT sno FROM student
WHERE sname ='王名'
)))
(5)查询选过课的但没有在MM表中注册的同学姓名、
SELECT sname FROM student
where sno in (SELECT sno FROM sc ,course WHERE sc.cno = course.cno)
and sno not in (SELECT sno FROM mm)
(6)查询选修的学分数超过8个学分的同学学号及学分数。
select sc.sno,sum(ccredit) as Expr1001 from sc,course where sc.cno=course.cno group by sno having sum(ccredit)>8;
(7)查询李明,刘晨两位同学的平均成绩,并用学号和平均成绩表示出来。
SELECT sc.sno ,avg(grade) as '平均成绩' FROM student,sc
where sc.sno = student.sno and sname in('李明','刘晨')
GROUP BY sc.sno
(8)查询和李明同一个系,年龄比他大的同学姓名(当前年 2010)
SELECT sname FROM student
WHERE sdept = (SELECT sdept FROM student WHERE sname='李明')
and 2010 - YEAR(csrq) > (SELECT 2010 -YEAR(csrq) FROM student WHERE sname='李明')
(9)查询数据库和数学这两门课的选课人数,并用课程号和选课人数表示出来
SELECT course.cno as '课程号',count(sc.sno) as '选课人数' FROM sc,course
WHERE course.cno in (SELECT cno FROM course WHERE cname in ('数据库','数学'))
and sc.cno =course.cno
GROUP BY course.cno
(10)查询同时选修了1,2,3号课的同学的姓名及系
select sname,sdept
from student
where sno in (select sno from sc where sno in(select sno from sc where sno in(select sno from sc where cno='1' ) and cno='2') and cno='3')