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));

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