【一周入门MySQL—4】数据库进阶练习
數據庫進階練習
?
數據庫進階查詢練習
素材:來自以下四個Table
數據準備(創建表,導入數據)
-- 數據準備,創建Tableuse test;-- 學生表create table stu(s_id varchar(10) primary key,s_name varchar(10) not null,s_birth date,s_sex varchar(10));-- 課程表create table co(c_id varchar(10) primary key,c_name varchar(10),t_id varchar(10));-- 教師表create table te(t_id varchar(10) primary key,t_name varchar(10));-- 成績表create table sc(s_id varchar(10),c_id varchar(10),score int);數據導入:insert into te values('01','張三'),('02','李四'),('03','王五');select * from te;insert into co values('1','語文','02'),('2','數學','01'),('3','英語','03');select * from co;insert into stu values('1','趙雷','1990-01-01','男'),('2','錢電','1990-12-21','男'),('3','孫風','1990-05-20','男'),('4','李云','1990-08-06','男'),('5','周梅','1991-12-01','女'),('6','吳蘭','1992-03-01','女'),('7','鄭竹','1992-04-21','女'),('8','王菊','1990-01-20','女');select * from stu;????insert into sc values('1','1',80),('1','2',90),??('1','3',99),('2','1',70),('2','2',60),('2','3',80),('3','1',80),('3','2',80),('3','3',80),('4','1',50),('4','2',30),('4','3',20),('5','1',76),('5','2',87),('6','1',31),('6','3',34),('7','2',89),('7','3',98);select * from sc;??????????
?【進階練習】
-- 查詢1#課程比2#課程成績高的學生的信息及課程分數(選修的每一門分數)
select * from sc where c_id = '1';??????????select * from sc where c_id = '2';select stu.*,sc.c_id,sc.scorefrom (select * from sc where c_id = '1') t1join (select * from sc where c_id = '2') t2 on t1.s_id = t2.s_idjoin stu on t1.s_id = stu.s_idjoin sc on stu.s_id = sc.s_idwhere t1.score > t2.score;?????????-- 查詢平均成績大于等于60分的同學的學生編號、學生姓名和平均成績
select stu.s_id,s_name,avg(score)from stu left join sc on stu.s_id = sc.s_idgroup by stu.s_idhaving avg(score)>=60;-- 查詢姓“李”的老師教授的課程數量
select count(c_id)from te left join co on te.t_id = co.t_idwhere t_name like '李%';-- 查詢名字中含有“風”字的學生信息
select *from stuwhere s_name like '%風%';-- 查詢學過張三老師課程的學生信息
# 首先找出滿足條件的學生IDselect * from stuwhere s_id in (select s_idfrom scleft join co on sc.c_id = co.c_idleft join te on co.t_id = te.t_idwhere t_name = '張三');-- 查詢沒有學過張三老師課程的學生信息
select * from stuwhere s_id not in (select s_idfrom scleft join co on sc.c_id = co.c_idleft join te on co.t_id = te.t_idwhere t_name = '張三');-- 查詢選修張三老師所授課程的學生中,成績最高的學生信息及成績
select stu.*,scorefrom stuleft join sc on stu.s_id = sc.s_idleft join co on sc.c_id = co.c_idleft join te on co.t_id = te.t_idwhere t_name = '張三'order by score desclimit 1;#如果出現并列第一的情況,上面的查詢方法就行不通了#子查詢的方式select stu.*,scorefrom stuleft join sc on stu.s_id = sc.s_idleft join co on sc.c_id = co.c_idleft join te on co.t_id = te.t_idwhere t_name = '張三' and score = (select max(score)from stuleft join sc on stu.s_id = sc.s_idleft join co on sc.c_id = co.c_idleft join te on co.t_id = te.t_idwhere t_name = '張三' );-- 查詢學過課程1和課程2的學生信息
# 利用模糊查詢匹配課程1和課程2select stu.*,group_concat(c_id order by c_id)from stuleft join sc on stu.s_id = sc.s_idgroup by stu.s_idhaving group_concat(c_id order by c_id) like '1,2%';-- 查詢學過課程1但是沒有學過課程2的學生信息
select stu.*,group_concat(c_id order by c_id)from stuleft join sc on stu.s_id = sc.s_idgroup by stu.s_idhaving group_concat(c_id order by c_id) like '%1%'and group_concat(c_id order by c_id) not like '%2%';select *from stuwhere s_id in (select s_id from sc where c_id = '1') and s_id not in (select s_id from sc where c_id = '2');select stu.*from stuleft join sc on stu.s_id = sc.s_idwhere c_id in ('1','2')group by stu.s_idhaving group_concat(c_id) = '1';-- 查詢選修了全部課程的學生信息
select stu.*,group_concat(c_id order by c_id)from stuleft join sc on stu.s_id = sc.s_idgroup by stu.s_idhaving group_concat(c_id order by c_id) = (select group_concat(c_id) from co);select *from stuwhere s_id in (select s_id from sc group by s_id having count(s_id) = (select count(*) from co));-- 查詢沒有學全全部課程的學生信息
select stu.*,count(c_id)from stuleft join sc on stu.s_id = sc.s_idgroup by stu.s_idhaving count(c_id) < (select count(*) from co);-- 查詢至少有一門課程與學號為“1”的學生相同的學生信息
select distinct stu.*from stuleft join sc on stu.s_id = sc.s_idwhere c_id in (select c_id from sc where s_id='1')and stu.s_id <> '1';-- 查詢與學號為“1”的學生所學課程完全相同的其他學生的信息
select stu.*,group_concat(c_id order by c_id)from stuleft join sc on stu.s_id = sc.s_idwhere stu.s_id <> '1'group by stu.s_idhaving group_concat(c_id order by c_id) = (select group_concat(c_id order by c_id) from sc where s_id='1');-- 查詢所有學生的課程和分數的情況(一維表轉為二維表)
select stu.s_id,sum(if(c_id='1',score,0)) '1#',sum(if(c_id='2',score,0)) '2#',sum(if(c_id='3',score,0)) '3#'from stuleft join sc on stu.s_id = sc.s_idgroup by stu.s_id;select stu.s_id,sum(case when c_id='1' then score else 0 end) '1#',sum(case when c_id='2' then score else 0 end) '2#',sum(case when c_id='3' then score else 0 end) '3#'from stuleft join sc on stu.s_id = sc.s_idgroup by stu.s_id;select stu.s_id,ifnull(sum((c_id='1')*score),0) '1#',ifnull(sum((c_id='2')*score),0) '2#',ifnull(sum((c_id='3')*score),0) '3#'from stuleft join sc on stu.s_id = sc.s_idgroup by stu.s_id;-- 按平均成績從高到低顯示所有學生的所有課程的考試成績和平均成績
select stu.s_id,sum(if(c_id='1',score,0)) '1#',sum(if(c_id='2',score,0)) '2#',sum(if(c_id='3',score,0)) '3#',ifnull(avg(score),0) 平均成績from stuleft join sc on stu.s_id = sc.s_idgroup by stu.s_idorder by 平均成績 desc;-- 查詢各科成績最高分、最低分、平均分
-- 以如下形式顯示課程ID、課程Name、最高分、最低分、平均分、及格率、中等率、優良率、優秀率
# 及格>=60,中等70-80,優良80-90,優秀>=90
# 及格率 = 及格人數/考試總人數
select co.c_id,c_name,max(score) 最高分,min(score) 最低分,avg(score) 平均分,sum(score>=60)/count(sc.c_id) 及格率, # avg(score>=60) 同一個思路sum(score>=70 and score<80)/count(sc.c_id) 中等率, # avg(score>=70 and score<80) 同一個思路sum(score>=80 and score<90)/count(sc.c_id) 優良率, # avg(score>=80 and score<90) 同一個思路sum(score>=90)/count(sc.c_id) 優秀率 # avg(score>=90) 同一個思路from coleft join sc on co.c_id = sc.c_idgroup by co.c_id-- 查詢學生的總成績并進行排名
select s_id 學號,sum(score) 總成績,row_number() over(order by sum(score) desc) 排名from scgroup by s_id;-- 查詢每個學生的平均成績以及排名
select s_id 學號,avg(score) 平均成績,row_number() over(order by avg(score) desc) 排名from scgroup by s_id;-- 按各科成績進行排序,并顯示排名
select * ,rank() over(partition by c_id order by score desc) 排名from sc;-- 查詢各科成績前三名的記錄
select * from(select * ,dense_rank() over(partition by c_id order by score desc) 排名from sc) twhere 排名<=3;-- 查詢每門功課成績最好的前兩名
select * from(select * ,dense_rank() over(partition by c_id order by score desc) 排名from sc) twhere 排名<=2;-- 查詢每門課程成績在第二名到第三名的學生信息及該課程成績
select stu.*,c_id,score,排名from stuleft join(select * ,dense_rank() over(partition by c_id order by score desc) 排名from sc) t on stu.s_id = t.s_idwhere 排名 between 2 and 3;-- 查詢每門課程被選修的學生數
select *,count(s_id) 課程選修人數from scgroup by c_id;-- 查明同名同姓的學生名單,并統計同名人數
select *,count(s_name)-1 同名人數from stugroup by s_name;-- 查詢任何一門課程成績在70分以上的學生姓名、課程名稱和分數
select s_name,c_name,scorefrom stuleft join sc on stu.s_id = sc.s_idleft join co on sc.c_id = co.c_idwhere score > 70;-- 查詢出現過學生考試成績不及格的課程
select c_name,sc.c_id,scorefrom sc join co on sc.c_id = co.c_idwhere score < 60;-- 查詢課程不同但是成績相同的學生的編號、課程編號和成績
select distinct t1.*from sc t1 join sc t2 on t1.s_id = t2.s_id and t1.c_id <> t2.c_id and t1.score = t2.score;-- 查詢本周過生日的學生信息
select *,week(s_birth)from stuwhere week(s_birth) = week(curdate()) ;-- 查詢下周過生日的學生信息
select *,week(s_birth)from stuwhere week(s_birth) = if(week(curdate())=54,1,week(curdate())+1);-- 查詢本月過生日的學生信息
select *,month(s_birth)from stuwhere month(s_birth) = month(curdate()) ;總結
以上是生活随笔為你收集整理的【一周入门MySQL—4】数据库进阶练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【一周入门MySQL—3】多表查询、子查
- 下一篇: 【一周入门MySQL—5】