Mysql 查询综合训练,权亮

有的地方会有我查询公式的试写,没有删除,是为了日后再看有思路.
如果有疑问,或者有做错的地方,欢迎交流.
use ceshi;
create table Student1(
Sno varchar(10) NOT NULL PRIMARY KEY,
Sname varchar(10),
Sage datetime,
Ssex char(2)
);
CREATE TABLE teacher1(
tno VARCHAR(8) NOT NULL PRIMARY KEY,
tname VARCHAR(10)
);
CREATE TABLE course1(
cno VARCHAR(8) NOT NULL PRIMARY KEY,
cname VARCHAR(20),
tno VARCHAR(8)
);
create table Score1
(Sno varchar(10),
Cno varchar(8),
score decimal(18,1)
);

insert into student1
values
(‘01’ , ‘丁一’ , ‘1990-01-01’ , ‘男’),
(‘02’ , ‘包二’ , ‘1990-12-21’ , ‘男’),
(‘03’ , ‘张三’ , ‘1990-05-20’ , ‘男’),
(‘04’ , ‘李四’ , ‘1990-08-06’ , ‘男’),
(‘05’ , ‘王五’ , ‘1991-11-01’ , ‘女’),
(‘06’ , ‘赵六’ , ‘1992-03-01’ , ‘女’),
(‘07’ , ‘田七’ , ‘1989-10-29’ , ‘女’),
(‘08’ , ‘汪八’ , ‘1990-01-20’ , ‘女’);
drop table student1;

select * from student1;
drop table student1;

insert into Teacher1 values(‘01’ , ‘赵磊’);
insert into Teacher1 values(‘02’ , ‘钱淼’);
insert into Teacher1 values(‘03’ , ‘孙犇’);

insert into Course1 values(‘01’ , ‘语文’ , ‘02’);
insert into Course1 values(‘02’ , ‘数学’ , ‘01’);
insert into Course1 values(‘03’ , ‘英语’ , ‘03’);

insert into SCORE1 values(‘01’ , ‘01’ , 80);
insert into SCORE1 values(‘01’ , ‘02’ , 90);
insert into SCORE1 values(‘01’ , ‘03’ , 99);
insert into SCORE1 values(‘02’ , ‘01’ , 70);
insert into SCORE1 values(‘02’ , ‘02’ , 60);
insert into SCORE1 values(‘02’ , ‘03’ , 80);
insert into SCORE1 values(‘03’ , ‘01’ , 80);
insert into SCORE1 values(‘03’ , ‘02’ , 80);
insert into SCORE1 values(‘03’ , ‘03’ , 80);
insert into SCORE1 values(‘04’ , ‘01’ , 50);
insert into SCORE1 values(‘04’ , ‘02’ , 30);
insert into SCORE1 values(‘04’ , ‘03’ , 20);
insert into SCORE1 values(‘05’ , ‘01’ , 76);
insert into SCORE1 values(‘05’ , ‘02’ , 87);
insert into SCORE1 values(‘06’ , ‘01’ , 31);
insert into SCORE1 values(‘06’ , ‘03’ , 34);
insert into SCORE1 values(‘07’ , ‘02’ , 89);
insert into SCORE1 values(‘07’ , ‘03’ , 98);

select * from student1;
select * from teacher1;
select * from course1;
select * from score1;

– 1. 查询" 01 “课程比” 02 "课程成绩高的学生的信息及课程分数

– 方法1:
select * from student1 c right join ( select a.sno as ssno,a.score 01成绩 ,b.score 02成绩 from score1 a ,score1 b
where a.sno=b.sno and a.score>b.score and a.cno=‘01’ and b.cno=‘02’) as t on t.ssno=c.sno;

– 方法2:
SELECT a.*,b.Cno,b.score,c.Cno,c.score FROM student1 AS a INNER JOIN (SELECT * FROM score1 WHERE Cno = 01) AS b ON a.Sno = b.Sno
INNER JOIN (SELECT * FROM score1 WHERE Cno = 02) AS c ON a.Sno = c.Sno WHERE b.score > c.score;

– 1.1 查询同时存在" 01 “课程和” 02 "课程的学生的信息
select * from student1 c right join (select a.sno ,a.cno 课程1,b.cno 课程2 from score1 a,score1 b where a.sno=b.sno and a.cno=‘01’ and b.cno=‘02’) as t on c.sno=t.sno;

– 1.2 查询存在" 01 “课程但可能不存在” 02 "课程的学生情况(不存在时显示为 null )
select * from student1 c right join (select a.sno ,a.cno 01课程,b.cno 02课程 from score1 a ,score1 b where a.sno=b.sno and a.cno=‘01’ and b.cno not in (‘02’)) as t on t.sno=c.sno;

select * from (select * from score1 where Cno=‘01’) AS a
left join (select * from score1 where Cno=‘02’) AS b on a.Sno=b.Sno WHERE a.score > b.score or a.Sno NOT IN (SELECT Sno from score1 where Cno=‘02’);

select * from score1 where cno=‘01’;
select * from score1 where cno=‘02’;
select * from student1 a join (select * from score1 c where cno=‘01’) as t on a.sno=t.sno join (select * from score1 b where cno=‘02’) p on p.sno=a.sno;

– 1.3 查询不存在" 01 “课程但存在” 02 "课程的情况

– 2. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩

select b.sno, sname,平均成绩 from student1 b join ( select sno,avg(c.score) 平均成绩 from score1 c group by sno having 平均成绩 >60) t on t.sno=b.sno;

SELECT student1.Sno,sname,a.AVG_score FROM student1 INNER JOIN
(SELECT Sno,avg(score) AS AVG_score FROM score1 GROUP BY sno) as a
ON a.Sno = student1.Sno
WHERE a.AVG_score >= 60;
– 3. 查询在 SC 表存在成绩的学生信息

select * from student1 a left join score1 b on a.sno=b.sno where b.sno is null;

– 4. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
select a.sno,sname,total ,counts from student1 a
left join ( select sno,sum(score) total from score1 group by sno) t on t.sno=a.sno
left join (select sno, count(cno) counts from score1 group by sno) p on p.sno=a.sno;

SELECT student1.Sno,sname,total_c,totoal_sc FROM student1 LEFT JOIN
(SELECT Sno,count(cno) AS total_c,sum(score) AS totoal_sc FROM score1 GROUP BY Sno) AS a
ON student1.Sno = a.Sno;

– 4.1 查有成绩的学生信息

select a.sno,sname,total ,counts from student1 a
right join ( select sno,sum(score) total from score1 group by sno) t on t.sno=a.sno
right join (select sno, count(cno) counts from score1 group by sno) p on p.sno=a.sno;

– 5. 查询「赵」姓老师的数量
select * from course1;
select * from teacher1;
select * from course1 a,teacher1 b where a.tno =b.tno and tname like’赵%’;
select count(*) from course1 a,teacher1 b where a.tno =b.tno and tname like ‘赵%’;

– 6. 查询学过「钱淼」老师授课的同学的信息

select * from teacher1;
select * from score1;
select * from course1;
select a.tno,a.tname,b.cno,b.cname from teacher1 a ,course1 b where a.tno=b.tno and tname=‘钱淼’;
select * from ( select c.sno,c.sname ,c.sage,c.ssex,d.cno,d.score from student1 c,score1 d where c.sno=d.sno and cno=‘01’) p left join (select a.tno,a.tname,b.cno,b.cname from teacher1 a ,course1 b where a.tno=b.tno and tname=‘钱淼’) t on t.cno=p.cno;
SELECT * FROM student1 WHERE Sno IN (SELECT Sno FROM score1 WHERE cno IN
(SELECT cno FROM course1 WHERE tno IN (SELECT tno FROM teacher1 WHERE tname = “钱淼”)));
select * from student1 where sno in (select sno from score1 where cno in (select cno from course1 where tno in (select tno from teacher1 where tname=‘钱淼’)));

– 7. 查询没有学全所有课程的同学的信息
select * from student1 a join (select sno, count(cno) a from score1 group by sno having a=2) t on t.sno =a.sno;

SELECT * FROM student1 WHERE Sno IN
(SELECT a.Sno FROM (SELECT Sno,count(1) AS num FROM score1 GROUP BY Sno) AS a WHERE a.num <
(SELECT count(1) FROM course1));

– 8. 查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
select distinct * from Student1 a, score1 b where a.sno = b.sno and b.cno in (select cno from score1 where sno = ‘01’) and a.sno <> ‘01’;

select * from student1 a join score1 b on a.sno=b.sno where cno = any (select cno from score1 where sno=‘01’) group by a.sno;
SELECT * FROM student1 WHERE Sno IN
(SELECT Sno FROM score1 WHERE Cno IN (SELECT Cno FROM score1 WHERE sno = 01) GROUP BY Sno);

– 9. 查询和" 01 "号的同学学习的课程完全相同的其他同学的信息
select * from student1 a join (select sno,count(cno) from score1 group by sno having count(cno)=(select count(cno) from score1 where sno=‘01’)) t
on t.sno=a.sno where a.sno<>‘01’;

select * from (select sno,group_concat(cno) b from score1 a group by sno ) t, (select group_concat(cno) c from score1 group by sno having sno=01) p where p.c=t.b;

select sno,group_concat(cno) from score1 a group by sno ;

– 查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
select * from score1 a,score1 b ,student1 c where a.sno=b.sno and a.sno=c.sno and a.cno=01 and b.cno=02;
use ceshi;

– 查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
select * from student1 a ,score1 b where a.sno=b.sno and b.cno=‘01’ and a.sno not in (select sno from score1 where cno=‘02c’) ;
select * from score1;
select Student.* from Student , SC where Student.SID = SC.SID and SC.CID = ‘01’ and not exists (Select 1 from SC SC_2 where SC_2.SID = SC.SID and SC_2.CID = ‘02’) order by Student.SID;

– 查询没有学全所有课程的同学的信息

select * from student1 a join (select sno,count() from score1 group by sno having count()❤️) t on t.sno=a.sno;

select Student.* from Student , SC where Student.SID = SC.SID group by Student.SID , Student.Sname , Student.Sage , Student.Ssex having count(CID) < (select count(CID) from Course);
– 10. 查询没学过"张三"老师讲授的任一门课程的学生姓名;
select * from student1 where sno not in ( select sno from score1 a, course1 b,teacher1 c where a.cno=b.cno and b.tno=c.tno and tname =‘赵磊’);

– 11. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select a.sno, a.sname,avg(b.score) from student1 a join (select distinct t.sno
from ( select * from score1 where score<60) t group by sno having count(score)>=2) p
on a.sno=p.sno join score1 b on a.sno=b.sno group by sno;

select student.SID , student.sname , cast(avg(score) as decimal(18,2)) avg_score from student , sc
where student.SID = SC.SID and student.SID in (select SID from SC where score < 60 group by SID having count(1) >= 2)
group by student.SID , student.sname

– 12. 检索" 01 "课程分数小于 60,按分数降序排列的学生信息
select b.* from score1 a ,student1 b where a.sno=b.sno and score<60 order by score desc;

– 13. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select * from score1 b join (select sno,avg(score) a from score1 group by sno ) t on b.sno=t.sno order by a desc;

– 14. 查询各科成绩最高分、最低分和平均分:
use ceshi;

select max(score),min(score),avg(score),
cast((select count() from score1 where score >=60)100/count() as decimal (8,2)) 及格率,
cast( (( select count(score) from score1 where score>=70 and score<80)100/count()) as decimal(8,2) ) 中等率,
cast((select count(
) from score where score>=80 and score <90)100/count() as decimal(6,2)) 优良率 ,
cast((select count(*) from score1 where score>=90)100/count() as decimal(8,2)) 优秀
from score1;

select count(score>60)/count() from score1;
select count(
) from score1;
select count(score>‘60’) from score1;

select ( select count(score<=80 and score>70)/count() from score1)/count() from score1;
select * from score1;
select count(score) from score1 where score>70 and score<=80;

– 以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
– 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
– 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

– 15. 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺
use ceshi;
select a.cno,a.score,count(distinct t.score) rank from score1 a,score1 t where a.score<=t.score
group by a.cno
order by a.score desc;
select * from score1 order by cno, score desc;
select a.,(select count(distinct score)+1 from score1 t where t.score>a.score ) rank from score1 a order by a.cno, rank ;
select a.
,(select count( score)+1 from score1 t where t.score>a.score ) rank from score1 a order by score desc;
select a.* ,count(distinct b.score) from score1 a, score1 b where a.cno=01 and a.score<b.score order by a.score desc;
select a.* ,(select count(*) from (select distinct score p from score1) t where p>=a.score) rank
from score1 a
order by a.score desc;

select a.* ,(select count(*) from (select score p from score1) t where p>=a.score) rank
from score1 a
order by a.score desc;

select a.*,
case when cno =@class then @rank:=@rank+1 else @rank:=1 end as rank,
@class:=cno
from score1 a,(select @calss:=’’,@rank:=0) t
order by cno ,score desc;
select a.sno,a.cno, a.score,count(a.score) from score1 a left join score1 b on a.cno=b.cno and a.score>b.score order by a.cno,a.score desc;
select a.sno,(select count(distinct b.score) from score1 b where b.cno=1 and b.score>a.score)+1 rank from score1 a where a.cno=1 order by a.score desc;
select * from score1;
select * from score1 a, score1 b where a.cno=1 and b.cno=1 and a.score>b.score order by a.score desc;

select a.sno,a.cno,a.score,(select count(distinct b.score)+1 from score1 b where b.cno=01 and b.score>a.score) rank
from score1 a
where a.cno=01
order by a.score desc;

select a.sno,a.cno,a.score,(select count(b.score) from score1 b where b.cno=01 and b.score>a.score)+1 rank
from score1 a
where a.cno=01
order by a.score desc;
select * from score1 a left join score1 b on a.cno=b.cno and a.score>b.score;

– 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次
select sno,cno, (select count(score) +1 from score1 where t.score <score) from score1 t;

– 16. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺
use ceshi;
create table ba (select sno ,sum(score) 总成绩 from score1 a group by sno order by 总成绩 desc);
select * from ba;
select sno,总成绩,(select count(b.sno) from ba b where b.总成绩>a.总成绩)+1 rank from ba a;
select sno,总成绩,(select count(b.总成绩) from ba b where b.总成绩>a.总成绩)+1 rank from ba a;
select * from ba a, ba b where a.总成绩<b.总成绩 order by a.总成绩 desc;
– 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
select sno,sum(score)

– 17. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
create table grade1 (
low int not null,
up int not null,
grade varchar(10) not null
);
insert grade1 value(‘85’,100,‘A’),
(‘70’,‘85’,‘B’),
(‘60’,‘70’,‘C’),
(‘0’,‘60’,‘D’);
select * from grade1;
drop table grade1;
select count(grade=A),count(grade=b),count(grade=c), count(grade=a)/count() a所占比, count(grade=b)/count() b所占比, count(grade=c)/count(*) c所占比 from score1 ,grade1 where cno =01 and score between low and up;
select * from score1 ,grade1 where cno =02 and score between low and up;
select * from score1 ,grade1 where cno =03 and score between low and up;

select b.cno,
sum(case when a.score<=100 and a.score>85 then 1 else 0 end) as “[100-85]”,
sum(case when a.score<=85 and a.score>70 then 1 else 0 end) as “[85-70]”,
sum(case when a.score<=70 and a.score>60 then 1 else 0 end) as “[70-60]”,

sum(case when a.score<=60 and a.score>0 then 1 else 0 end) as “[60-0]”
from score1 a left join course1 b
on a.cno = b.cno
group by a.cno;

select a.cno,b.cno,sum(case when a.score<=100 and a.score>85 then 1 else 0 end) as “[100-85]”
from score1 a left join course1 b on a.cno=b.cno;

select a.cno ,case when a.score<=100 and a.score>85 then 1 else 0 end as “[100-85]”
from score1 a order by cno;
select a.cno ,sum(score<100 and score>85) as “[100-85]” from score1 a order by cno;

– 18. 查询各科成绩前三名的记录
select a.cno,a.score
from score1 a
where (select count(*) from score1 b where b.cno=a.cno and a.score<=b.score)<=3
order by a.cno,a.score desc;

select a.cno,a.score
from score1 a
where (select count(*) from score1 b where b.cno=a.cno and a.score<=b.score)
order by a.cno,a.score desc;
select * from score1 b,score1 a where b.cno=a.cno and a.score<=b.score order by a.cno asc,a.score desc;

select * from score1 where cno=03;

use ceshi;
select sno,cno,score
from score1 b
where (select count(distinct score)+1 from score1 a where a.cno=b.cno and a.score>b.score) between 1 and 2
order by b.cno,b.score desc;

select sno,cno,score
from score1 b
where (select count(score)+1 from score1 a where a.cno=b.cno and a.score>b.score) between 1 and 2
order by b.score desc;

select sno,cno,score
from score1 b where (select count(score)+1 from score1 a where a.cno=b.cno and a.score>b.score)
between 1 and 3 order by b.cno,b.score desc;

select * from score1 a left join score1 b on a.cno=b.cno and a.score <b.score order by a.cno asc,a.score desc,a.sno asc;

– 19. 查询每门课程被选修的学生数
select count(sno) from score1 group by cno;
select count(distinct sno) from score1 group by cno;

– 20. 查询出只选修两门课程的学生学号和姓名
select a.sno,count(cno) ,b.* from score1 a left join student1 b on a.sno=b.sno group by a.sno having count(cno)=2;

– 21. 查询男生、女生人数
select
sum(case when ssex=‘男’ then 1 end) ‘男生人数’,
sum(case when ssex=‘女’ then 1 end) ‘女生人数’
from student1;
select * from student1;

– 22. 查询名字中含有「风」字的学生信息

select * from student1 where sname like ‘%风%’;

– 23. 查询同名同性学生名单,并统计同名人数
select * from student1 a ,student1 b where a.sname =b.sname and a.ssex=b.ssex;

– 24. 查询 1990 年出生的学生名单
select * from student1 where year(sage)=‘1990’;

– 25. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列

select cno ,avg(score) t from score1 group by cno order by t desc,cno asc;

– 26. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
select a.sno ,avg(score) t ,b.* from score1 a,student1 b where a.sno=b.sno group by a.sno having t>=85;

– 27. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
select a.sno,a.score,b.sname,a.cno from score1 a,student1 b,course1 c where a.sno=b.sno and c.cno=a.cno and c.cname=‘数学’ and a.score<60;

– 28. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
select * from student1 a left join score1 b on a.sno=b.sno left join course1 c on b.cno=c.cno;

– 29. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数

select sno from score1 a right join course1 b on a.cno=b.cno group by sno having min(score)>70;

– 30. 查询不及格的课程
select distinct a.cno,b.* from score1 a left join course1 b on a.cno=b.cno where a.score<60;

– 31. 查询课程编号为 01 且课程成绩在 70 分以上的学生的学号和姓名
select * from score1 a ,student1 b where a.sno=b.sno and a.cno=01 and a.score>70;

– 32. 求每门课程的学生人数
select count(sno) from score1 group by cno;

– 33. 成绩不重复,查询选修「赵磊」老师所授课程的学生中,成绩最高的学生信息及其成绩
select * from teacher1;
select a.,max(score) t,b.,c.* ,d.*from score1 a ,course1 b, teacher1 c,student1 d where a.cno=b.cno and b.tno=c.tno and d.sno=a.sno group by a.cno having tname=‘赵磊’;

– 34. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩

– 35. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select * from score1 a,score1 b where a.score =b.score and a.sno<>b.sno and a.cno<>b.cno;

– 36. 查询每门功成绩最好的前两名
select * from score1 a ,student1 b where (select count(score)+1 t from score1 b where a.cno=b.cno and b.score>a.score )<=2 and a.sno=b.sno order by cno,score desc;

– 37. 统计每门课程的学生选修人数(超过 5 人的课程才统计)。
select count(sno) from score1 group by cno having count(sno)>5;

– 38. 检索至少选修两门课程的学生学号
select sno,count(sno) t from score1 group by sno having t>=2;

– 39. 查询选修了全部课程的学生信息
select sno ,count(sno ) t from score1 group by sno having t=3;

– 40. 查询各学生的年龄,只按年份来算
select sno, year(now())-year(sage),sage 年龄 from student1;

select * from student1;

– 41. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
select sno,if (month(now())<month(sage),if(month(now())=month(sage),if(day(now())<day(sage),year(now())-year(sage)-1,year(now())-year(sage)),year(now())-year(sage)),year(now())-year(sage)-1) ,sage from student1;

select current_date() from dual;

– 42. 查询本周过生日的学生
use ceshi;
select current_timestamp();
select curtime();
select curdate();
select current_date();
select week(sage)=week(curdate()) from student1;
select week(curdate()) ;

– 43. 查询下周过生日的学生
select sname from student1
where sage between subdate(curdate(),weekday(curdate())-7) and subdate(curdate(),weekday(curdate())-13);

select weekday(curdate())-7 from student1;
– 44. 查询本月过生日的学生
select sno,month(now())=month(sage) from student1;
select sno from student1 where month(now())=month(sage);

– 45. 查询下月过生日的学生
select sno from student1 where if(month(now())<12, if(month(sage)=month(now())+1,1,0),if(month(sage)=1,1,0));

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章