(这一块很差的)
(select sid from sc group by sid having max(score)<=60) a
用表连接
select * from student b
inner join
(select sid from sc group by sid having max(score)<=60) a
on b.sid=a.sid;
用in
select * from student
where sid
in (select sid from sc group by sid having max(score)<=60);
用取反的做法
select sid from sc where score > 60;
select * from student
where sid
not in(select sid from sc where score > 60);
select count(*) from course; /*求得总课程数*/
/*求得没有学全总课程数的sid*/
(select sid from sc group by sid having count(*)<(select count(*) from course)) a
select * from
(select sid from sc group by sid having count(*)<(select count(*) from course)) a
inner join student b on a.sid=b.sid;
先统计个数,再连接课程表
(select cid, count(*) from sc group by cid) a
select totalcount,cname from
(select cid, count(*) totalcount from sc group by cid) a
inner join course b on a.cid=b.cid;
先连接课程表,再统计个数
select a.cid,cname,count(*)
from sc a
inner join course b
on a.cid=b.cid group by a.cid, cname;
select * from
(select sid from sc group by sid having count(*)=(select count(*) from course)) a
inner join student b on a.sid=b.sid;
select sid from sc
where score<60 group by sid having count(*)>=2;
select sid,avg(score) from sc where sid in
(select sid from sc where score<60 group by sid having count(*)>=2)
group by sid;
(select sid,cid,score from sc where cid=1) a
(select sid,cid,score from sc where cid=2) b
(select a.sid from (select sid,cid,score from sc where cid=1)a inner join
(select sid,cid,score from sc where cid=2)b on a.sid=b.sid and a.score>b.score)c
select * from
(select a.sid from (select sid,cid,score from sc where cid=1) a
inner join (select sid,cid,score from sc where cid=2) b
on a.sid=b.sid and a.score>b.score) c
inner join student d where c.sid=d.sid;
select * from
(select a.sid from (select sid,cid,score from sc where cid=1) a
inner join (select sid,cid,score from sc where cid=2)b on a.sid=b.sid)c
inner join student d where c.sid=d.sid;
-- 找到的是叶平教过的学生的sid
select sid from teacher a
inner join course b on a.tid=b.tid
inner join sc c on b.cid=c.cid where a.tname='叶平';
-- 再利用not in 排除这些sid
select * from student
where sid not in
(select sid from teacher a
inner join course b on a.tid=b.tid
inner join sc c on b.cid=c.cid
where a.tname='叶平');
select stu.sid,stu.sname,
(select count(cid) from scores sc where sc.sid=stu.sid) '选课数',
(select sum(score) from scores sc where sc.sid=stu.sid) '总成绩'
from student stu;
select stu.sname,a.* from (select sid,count(cid),sum(score) from scores group by sid) a
inner join student stu on a.sid=stu.sid;
select stu.sid,stu.sname,
(select avg(score) from scores sc where stu.sid=sc.sid group by stu.sid) v
from student stu
having v > 80;
select a.*,stu.sname from
(select sid,avg(score) '平均成绩' from scores group by sid having (avg(score) > 80)) a
inner join student stu on a.sid=stu.sid;
select b.sid,b.cid,b.score
from (select cid,score from scores sc group by cid,score having (count(*) > 1)) a
inner join scores b on (a.cid=b.cid and a.score=b.score);
select b.* from (select deptno,max(sal) msal from emp group by deptno) a
inner join emp b on a.deptno=b.deptno and a.msal=b.sal;
select * from emp e
where sal = (select max(sal) from emp where e.deptno=deptno group by deptno);