--准备工作:建表、新增测试数据
create table Student(Sid varchar(10),Sname varchar(10),Sage date,Ssex varchar(10));
insert into Student values('01' , '赵雷' , str_to_date('1990-01-01','%Y-%m-%d') , '男');
insert into Student values('02' , '钱电' , str_to_date('1990-12-21','%Y-%m-%d') , '男');
insert into Student values('03' , '孙风' , str_to_date('1990-05-20','%Y-%m-%d') , '男');
insert into Student values('04' , '李云' , str_to_date('1990-08-06','%Y-%m-%d') , '男');
insert into Student values('05' , '周梅' , str_to_date('1991-12-01','%Y-%m-%d') , '女');
insert into Student values('06' , '吴兰' , str_to_date('1992-03-01','%Y-%m-%d') , '女');
insert into Student values('07' , '郑竹' , str_to_date('1989-07-01','%Y-%m-%d') , '女');
insert into Student values('08' , '王菊' , str_to_date('1990-01-20','%Y-%m-%d') , '女');create table Course(Cid varchar(10),Cname varchar(10),Tid varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');create table Teacher(Tid varchar(10),Tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');create table SC(SCid varchar(10),Cid varchar(10),score int);
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);
SELECT t3.* FROM (SELECT t1.*,t2.score FROM Student t1 LEFT JOIN sc t2 ON t1.`Sid`=t2.`SCid` WHERE t2.`Cid`=01) t3
LEFT JOIN (SELECT t1.*,t2.score FROM Student t1 LEFT JOIN sc t2 ON t1.`Sid`=t2.`SCid` WHERE t2.`Cid`=02) t4
ON t3.sid = t4.sid WHERE t3.score > t4.score
SELECT t3.* FROM (SELECT t1.*,t2.score FROM Student t1 LEFT JOIN sc t2 ON t1.`Sid`=t2.`SCid` WHERE t2.`Cid`=01) t3
LEFT JOIN (SELECT t1.*,t2.score FROM Student t1 LEFT JOIN sc t2 ON t1.`Sid`=t2.`SCid` WHERE t2.`Cid`=02) t4
ON t3.sid = t4.sid WHERE t3.score < t4.score
select AVG(score),SCid from sc GROUP BY SCid
答案
select s1.Sname,s2.avgscore,s2.SCid from student s1,
(select AVG(score) avgscore,SCid from sc GROUP BY SCid) s2
where s1.Sid=s2.SCid and s2.avgscore>=70
select s1.Sname,s2.avgscore,s2.SCid from student s1,
(select AVG(score) avgscore,SCid from sc GROUP BY SCid) s2
where s1.Sid=s2.SCid and s2.avgscore<70
select s1.Sid,s1.Sname,SUM(s2.score),COUNT(s2.SCid) from student s1,sc s2
where s1.Sid=s2.SCid
GROUP BY s2.SCid
select COUNT(Tid)
from teacher
where Tname like '李%'
select c.Cid cid
from course c,teacher t
where c.Tid=t.Tid and t.Tname='张三'
select s.SCid
from sc s,
(select c.Cid cid
from course c,teacher t
where c.Tid=t.Tid and t.Tname='张三') s2
where s.Cid=s2.cid
GROUP BY s.SCid
答案
select * from student s5
where s5.Sid in
(select s.SCid
from sc s,
(select c.Cid cid
from course c,teacher t
where c.Tid=t.Tid and t.Tname='张三') s2
where s.Cid=s2.cid
GROUP BY s.SCid)
select * from student s5
where s5.Sid not in
(select s.SCid
from sc s,
(select c.Cid cid
from course c,teacher t
where c.Tid=t.Tid and t.Tname='张三') s2
where s.Cid=s2.cid
GROUP BY s.SCid)
select * from sc
where Cid=01
select * from sc
where Cid=02
select s1.SCid
from
(select SCid from sc
where Cid=01) s1,
(select SCid from sc
where Cid=02) s2
where s1.SCid=s2.SCid
答案
select *
from student
where Sid in
(select s1.SCid
from
(select SCid from sc
where Cid=01) s1,
(select SCid from sc
where Cid=02) s2
where s1.SCid=s2.SCid)
select SCid
from sc
where SCid not in
(select SCid from sc
where Cid=02) and SCid in
(select SCid from sc
where Cid=01)
GROUP BY SCid
答案
select * from student
where Sid in
(select SCid
from sc
where SCid not in
(select SCid from sc
where Cid=02) and SCid in
(select SCid from sc
where Cid=01)
GROUP BY SCid)
答案
select s.*
from student s,
(select SCid,COUNT(Cid) c
from sc
GROUP BY SCid
HAVING c!=(select COUNT(Cid)
from course)) s2
where s.Sid = s2.SCid
答案
select *
from student
where Sid in
(select SCid
from sc
where Cid in
(select Cid
from sc
where SCid=01) and SCid!=01
GROUP BY SCid)
答案
select * from student
where Sid in
(select SCid
from sc s1,(select Cid
from sc
where SCid=01) s2
where s1.Cid=s2.Cid
GROUP BY SCid
HAVING COUNT(s1.Cid)=(select COUNT(Cid)
from sc
where SCid=01
))
答案
select Sname
from student
where Sid not in (select SCid
from sc
where Cid in (select Cid from course
where Tid in (select Tid
from teacher
where Tname = '张三')))
答案
select s1.Sid,s1.Sname,s2.avgScore
from student s1,
(select COUNT(SCid) countSCid,SCid,AVG(score) avgScore
from sc
where score<60
GROUP BY SCid
HAVING countSCid>=2) s2
where s1.Sid=s2.SCid
答案
select * from student
where Sid in (select SCid
from sc
where score<60 and Cid=01
ORDER by score desc)
答案
select scid,
max(case cid WHEN '01' THEN score ELSE 0 end) '语文',
max(case cid WHEN '02' THEN score ELSE 0 end) '数学',
max(case cid WHEN '03' THEN score ELSE 0 end) '英语',
AVG(score) '平均成绩'
from sc
GROUP BY scid
ORDER BY AVG(score) desc
答案
select a.cid,a.cname,b.最高分,b.最低分,b.平均分,b.及格率,b.中等率,b.优良率,b.优秀率
from course A
LEFT JOIN (select cid,MAX(score) '最高分',MIN(score) '最低分',AVG(score) '平均分',
CONCAT(ROUND(sum(case when score>=60 then 1 else 0 end)/COUNT(*)*100,2),'%') '及格率',
CONCAT(ROUND(sum(case when score>=60 AND score<80 then 1 else 0 end)/COUNT(*)*100,2),'%') '中等率',
CONCAT(ROUND(sum(case when score>=80 AND score<90 then 1 else 0 end)/COUNT(*)*100,2),'%') '优良率',
CONCAT(ROUND(sum(case when score>=90 then 1 else 0 end)/COUNT(*)*100,2),'%') '优秀率'
FROM sc
GROUP BY cid) B
on a.cid=b.cid
(SELECT *,@row1:=@row1+1 rank FROM sc,(SELECT @row1:=0) r WHERE cid=01 ORDER BY score DESC) UNION ALL
(SELECT *,@row2:=@row2+1 rank FROM sc,(SELECT @row2:=0) r WHERE cid=02 ORDER BY score DESC) UNION ALL
(SELECT *,@row3:=@row3+1 rank FROM sc,(SELECT @row3:=0) r WHERE cid=03 ORDER BY score DESC)
select *,sum(score),@rownu:=@rownu+1 as rank from sc ,
(select @rownu:=0) r
GROUP BY SCid
ORDER BY sum(score) desc
答案
select course.cname,a.个数
from course
LEFT JOIN (select cid,COUNT(cid) '个数'
from sc
GROUP BY cid ) A on course.cid=a.cid
答案
select sid,sname
from student
where sid in (select scid
from sc
GROUP BY scid
HAVING COUNT(*)=2)
答案
select ssex,COUNT(*) from student GROUP BY ssex
答案
select * from student where sname like '%风%'
答案
select sname,COUNT(*) from student GROUP BY sname,ssex HAVING COUNT(*)>1
答案
select sname from student WHERE YEAR(sage)=1990
答案
select * from sc GROUP BY cid ORDER BY AVG(score) desc ,cid
答案
select s1.sid ,sname ,avg
from student s1,(select scid,AVG(score) avg from sc GROUP BY scid HAVING AVG(score)>=60) s2
where s1.sid=s2.scid
答案
select sname,score from student s1,
(select score ,scid from sc
where cid = (select cid from course where cname = '数学') and score<60) s2
where s1.sid=s2.scid
答案
select scid,
max(case cid WHEN '01' THEN score else 0 end) 语文,
max(case cid WHEN '02' THEN score else 0 end) 数学,
max(case cid WHEN '03' THEN score else 0 end) 英语
from sc GROUP BY scid
答案
select sname,cname,scid,score from student s2,(select cname,scid,score from course c,(select scid,score,cid from sc where score>70) s
where c.cid=s.cid) A
where s2.sid=a.scid
select * from sc where score<60
答案
select sid,sname from student
where sid in (select scid from sc where cid = 01 and score >70)
答案
select cid ,COUNT(*) from sc
GROUP BY cid
答案
select * from student s1,(
select scid,MAX(score) from sc where cid=(select cid from course where tid = (select tid from teacher where tname = '张三'))) s2
where s1.sid=s2.scid
答案
select * from student where MONTH(sage)=MONTH(DATE_ADD(NOW(),INTERVAL 1 MONTH))
select
*
from scores t
where
(select count(1)+1 from scores where class_id=t.class_id and score>t.score)
<= 3
select * from a
where fenshu>80
GROUP BY name HAVING count(*) =
(select count(*) from (select * from a GROUP BY kecheng) a)
select * from (select s.*,@r:=(case when @c=class_id and @d=`subject` then @r+1 else 1 end) as rank,
@c:=class_id c,@d:=`subject`
from scores s
ORDER BY class_id,`subject`,score desc) totle
where totle.rank<=2
select
*
from
syscolumns
where
(
case
colid
when
0
then
1
end
) = 1