基本表结构:
student(sno,sname,sage,ssex)学生表
course(cno,cname,tno) 课程表
sc(sno,cno,score) 成绩表
teacher(tno,tname) 教师表
1、查询课程1的成绩比课程2的成绩高的所有学生的学号
select a.sno from(select sno,score from sc where cno=1) a,
(select sno,score from sc where cno=2) bwhere a.score>b.score and a.sno=b.sno
2、查询平均成绩大于60分的同学的学号和平均成绩
select a.sno as "学号", avg(a.score) as"平均成绩"from(select sno,score fromsc) agroup by sno having avg(a.score)>60
3、查询所有同学的学号、姓名、选课数、总成绩
select a.sno as 学号, b.sname as姓名,count(a.cno) as 选课数, sum(a.score) as总成绩fromsc a, student bwhere a.sno =b.snogroup by a.sno, b.sname
或者:
selectstudent.sno as 学号, student.sname as姓名,count(sc.cno) as 选课数, sum(score) as总成绩from student left Outer join sc on student.sno =sc.snogroup by student.sno, sname
4、查询姓“张”的老师的个数
selectcount(distinct(tname)) from teacher where tname like ‘张%‘
或者:
select tname as "姓名", count(distinct(tname)) as"人数"fromteacherwhere tname like‘张%‘
group by tname
5、查询没学过“张三”老师课的同学的学号、姓名
select student.sno,student.sname fromstudentwhere sno not in (select distinct(sc.sno) fromsc,course,teacherwhere sc.cno=course.cno and teacher.tno=course.tno and teacher.tname=‘张三‘)
6、查询同时学过课程1和课程2的同学的学号、姓名
select sno, sname fromstudentwhere sno i