当前位置:网站首页>50 MySQL exercises + Answers
50 MySQL exercises + Answers
2022-07-28 05:57:00 【xiexiexie0520】
MySQL Exercises + answer
50 Taoist classics SQL The answer to the exercise
Data sheet introduction
1. learn ⽣ surface Student(SId,Sname,Sage,Ssex)
SId learn ⽣ Number
Sname learn ⽣ full name
Sage Out ⽣ year ⽉
Ssex learn ⽣ Gender
2. The curriculum Course(CId,Cname,TId)
CId Course number
Cname Course name
TId Teacher number
3. Teachers list Teacher(TId,Tname)
TId Teacher number
Tname Teacher's name
4. League tables SC(SId,CId,score)
SId learn ⽣ Number
CId Course number
score fraction
Create table statement
learn ⽣ surface Student
create table Student( SId varchar(10), Sname varchar(10), Sage datetime, Ssex varchar(10) );The curriculum Course
create table Course( CId varchar(10), Cname varchar(10), TId varchar(10) );Teachers list Teacher
create table Teacher( TId varchar(10), Tname varchar(10) );League tables SC
create table SC( SId varchar(10), CId varchar(10), score decimal(18,1) );
insert data
learn ⽣ surface Student
-- Student list Student insert into Student values('01' , ' Zhao Lei ' , '1990-01-01' , ' male '); insert into Student values('02' , ' Qian Dian ' , '1990-12-21' , ' male '); insert into Student values('03' , ' Sun Feng ' , '1990-12-20' , ' male '); insert into Student values('04' , ' Li Yun ' , '1990-12-06' , ' male '); insert into Student values('05' , ' Zhou Mei ' , '1991-12-01' , ' Woman '); insert into Student values('06' , ' Wu Lan ' , '1992-01-01' , ' Woman '); insert into Student values('07' , ' Zheng Zhu ' , '1989-01-01' , ' Woman '); insert into Student values('09' , ' Zhang San ' , '2017-12-20' , ' Woman '); insert into Student values('10' , ' Li Si ' , '2017-12-25' , ' Woman '); insert into Student values('11' , ' Li Si ' , '2012-06-06' , ' Woman '); insert into Student values('12' , ' Zhao Liu ' , '2013-06-13' , ' Woman '); insert into Student values('13' , ' Sun Qi ' , '2014-06-01' , ' Woman ');The curriculum Course
-- Families, ⽬ surface Course insert into Course values('01' , ' Chinese language and literature ' , '02'); insert into Course values('02' , ' mathematics ' , '01'); insert into Course values('03' , ' English ' , '03');Teachers list Teacher
-- Teachers list Teacher insert into Teacher values('01' , ' Zhang San '); insert into Teacher values('02' , ' Li Si '); insert into Teacher values('03' , ' Wang Wu ');League tables SC
-- League tables SC 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);
Exercise questions
1. Inquire about " 01 “ Course ⽐” 02 " Course results ⾼ Science ⽣ Information and course scores
select ttt1.SId
,ttt1.chinese_score
,ttt1.math_score
,ttt2.SName
from(
select tt1.SId
,ifnull(tt1.chinese_score,0) as chinese_score
,ifnull(tt1.math_score,0) as math_score
from
(
select t1.SId
,t1.score as chinese_score
,t2.score as math_score
from(
select SId
,CId
,score
from SC
where CId = '01'
) t1
left join (
select SId
,CId
,score
from SC
where CId = '02'
) t2
on t1.SId = t2.SId
union
select t2.SId
,t1.score as chinese_score
,t2.score as math_score
from(
select SId
,CId
,score
from SC
where CId = '01'
) t1
right join (
select SId
,CId
,score
from SC
where CId = '02'
) t2
on t1.SId = t2.SId
) tt1
where if(tt1.chinese_score is null,0,tt1.chinese_score) > ifnull(tt1.math_score,0)
) ttt1
left join Student ttt2
on ttt1.SId = ttt2.SId;
2. Queries exist at the same time " 01 “ Courses and ” 02 " Course situation
select tt1.SId
,tt2.SName
,tt2.SAge
,tt2.SSex
from(
select t1.SId
from(
select SId
from SC
where CId = '01'
) t1
join (
select SId
from SC
where CId = '02'
) t2
on t1.SId = t2.SId
) tt1 left join Student tt2 on tt1.SId = tt2.SId;
3. Query exists " 01 “ The course may not exist ” 02 " Course situation ( If it does not exist, it will be displayed as null )
select t1.SId
,t2.SId
from(
select SId
from SC
where CId = '01'
) t1
left join (
select SId
from SC
where CId = '02'
) t2
on t1.SId = t2.SId;
4. Query does not exist " 01 “ Course but there is ” 02 " Course situation
select t1.SId
,t2.SId
from(
select SId
from SC
where CId = '01'
) t1
right join (
select SId
from SC
where CId = '02'
) t2
on t1.SId = t2.SId;
5. Check the grade average ⼤ Is equal to 60 The study of students with different grades ⽣ Numbering and learning ⽣ Name and average score
select t1.SId
,t2.SName
,t1.avg_score
from(
select SId
,avg(score) as avg_score
from SC
group by SId
having avg_score>=60
) t1 left join Student t2
on t1.SId = t2.SId;
6. The query in SC There are academic achievements in the table ⽣ Information
select SId
,SName
from Student
where SId in (
select distinct SId
from SC
);
7. Check all students' learning ⽣ Number 、 learn ⽣ full name 、 The total number of selected courses 、 Total grade of all courses ( No results are shown as null )
select t1.SId
,t1.SName
,t2.ccnt
,t2.sum_score
from Student t1
left join (
select SId
,count(CId) ccnt
,sum(score) sum_score
from SC
group by SId
) t2 on t1.SId = t2.SId;
8. Inquire about 「 Li 」 surname ⽼ Number of divisions
select ' Surname Li ' as col1
,count(*) as cnt
from Teacher
where Tname like ' Li %';
9. I have learned to query 「 Zhang San 」⽼ Information about the students taught by the teacher
select t1.SId
,t2.SName
from (
select SId
from SC
where CId = (
select CId
from Course
where TId = (
select TId
from Teacher
where TName = ' Zhang San '
)
)
) t1 left join Student t2
on t1.SId = t2.SId;
select SId
,SName
from Student
where SId in (
select SId
from SC
where CId = (
select CId
from Course
where TId = (
select TId
from Teacher
where TName = ' Zhang San '
)
)
);
10. Check the information of students who have not learned all the courses
select SId
,SName
from Student
where SId not in (
select SId
from (
select SId
,count(CId) as cnt
from SC
group by SId
having cnt = (
select count(CId)
from Course
)
) t1
);
11. Inquire about ⾄ There is little ⼀⻔ The class and student number are " 01 " Of the students who learn the same information
select t1.SId
,t2.SName
from (
select distinct SId
from SC
where SId != '01'
and CId in (
select CId
from SC
where SId = '01'
)
) t1 left join Student t2
on t1.SId = t2.SId;
12. Query and " 01 " The course of study of No Exactly the same information from other students
select SId
,SName
from Student
where SId in (
select t1.SId
from (
select SId
,count(CId) as cnt
from SC
where SId != '01'
and CId in (
select CId
from SC
where SId = '01'
) group by SId
having cnt = (
select count(CId)
from SC
where SId = '01'
)
) t1 join (
select SId
,count(CId) as cnt
from SC
where SId != '01'
group by SId
) t2 on t1.SId = t2.SId and t1.cnt = t2.cnt
);
13. I didn't learn how to query " Zhang San "⽼ The teacher teaches ⼀⻔ Course learning ⽣ full name
select SId
,SName
from Student
where SId not in (
select distinct
SId
from SC
where CId in (
select CId
from Course
where TId in (
select TId
from Teacher
where TName = ' Zhang San '
)
)
);
14. Query two ⻔ The student number of students who fail the course or above , Name and average score
select t1.SId
,t2.SName
,t1.avg_score
from (
select SId
,count(CId) as cnt
,avg(score) as avg_score
from SC
where score <60
group by SId
having cnt >= 2
) t1 left join Student t2
on t1.SId = t2.SId;
15. retrieval " 01 " Course grade ⼩ On 60, In descending order of scores ⽣ Information
select t1.SId
,t2.SName
,t1.score
from (
select SId
,score
from SC
where CId = '01'
and score < 60
) t1 left join Student t2
on t1.SId = t2.SId
order by t1.score desc;
16. From... On average ⾼ To low shows all learning ⽣ Grades and GPA of all courses
select t1.SId
,t1.CId
,t1.score
,t2.avg_score
from SC t1
left join (
select SId
,round(avg(score),2) as avg_score
from SC
group by SId
) t2 on t1.SId = t2.SId
order by t2.avg_score desc;
17. Inquire about the results of each subject ⾼ branch 、 Minimum and average points : Show as follows : Course ID, Course name, most ⾼ branch , Lowest score , average , pass rate , Medium rate , Excellent rate , Excellence rate Pass is >=60, The average is :70-80, Good is :80-90, Excellence is :>=90 It is required to output the course number and elective courses ⼈ Count , The query result is displayed by ⼈ The numbers are arranged in descending order , if ⼈ The same number , In ascending order of course number
select t1.CId
,t1.max_score
,t1.min_score
,t1.avg_score
,t1.pass_percent
,t1.med_percent
,t1.good_percent
,t1.best_percent
from(
select CId
,max(score) as max_score
,min(score) as min_score
,avg(score) as avg_score
,concat(round(sum(if(score>=60,1,0)) * 100/count(SId),2),'%') as pass_percent
,concat(round(sum(if(score>=70 and score < 80,1,0)) * 100/count(SId),2),'%') as med_percent
,concat(round(sum(if(score>=80 and score < 90,1,0)) * 100/count(SId),2),'%') as good_percent
,concat(round(sum(if(score>=90 ,1,0)) * 100/count(SId),2),'%') as best_percent
,count(SId) as cnt
from SC
group by CId
) t1 left join Course t2
on t1.CId = t2.CId
order by t1.cnt desc,t1.CId;
18. Enter according to the average score of each subject ⾏ Sort , And show ranking , Score Keep the vacancy when repeat
1 2 2 4 5 6
set @i := 0;
set @j := 0;
set @avg_score_front := 0;
set @avg_score_last := 0;
select tt1.CId
,tt1.avg_score
,tt1.dense_rk
from (
select t1.CId
,t1.avg_score
,@j := @j + 1
,@avg_score_front := t1.avg_score
,if(@avg_score_front = @avg_score_last,@i,@i := @j) as rk
,@avg_score_last := @avg_score_front
from
(
select CId
,round(avg(score),2) as avg_score
from SC
group by CId
order by avg_score desc
) t1
) tt1;
19. Enter according to the average score of each subject ⾏ Sort , And show ranking , Score There is no vacancy when repeating
1 2 2 3 4 5 6
set @i := 0;
set @avg_score_front := 0;
set @avg_score_last := 0;
select tt1.CId
,tt1.avg_score
,tt1.dense_rk
from (
select t1.CId
,t1.avg_score
,@avg_score_front := t1.avg_score
,if(@avg_score_front = @avg_score_last,@i,@i := @i + 1) as dense_rk
,@avg_score_last := @avg_score_front
from
(
select CId
,round(avg(score),2) as avg_score
from SC
group by CId
order by avg_score desc
) t1
) tt1;
20. Inquiry ⽣ The total score of , Go in parallel ⾏ ranking , Keep the position vacancy when the total score is repeated
set @i:=0;
set @j:=0;
set @sum_score_front:=0;
set @sum_score_back:=0;
select tt1.SId
,tt1.sum_score
,tt1.rk
from (
select t1.SId
,t1.sum_score
,@j := @j + 1
,@sum_score_front := t1.sum_score
,if(@[email protected]_score_back,@i,@i:[email protected]) as rk
,@sum_score_back := @sum_score_front
from (
select SId
,sum(score) as sum_score
from SC
group by SId
order by sum_score desc
) t1
) tt1;
21. Inquiry ⽣ The total score of , Go in parallel ⾏ ranking , If the total score is repeated, no vacancy will be reserved
set @i:=0;
set @sum_score_front:=0;
set @sum_score_back:=0;
select tt1.SId
,tt1.sum_score
,tt1.rk
from (
select t1.SId
,t1.sum_score
,@sum_score_front := t1.sum_score
,if(@[email protected]_score_back,@i,@i:[email protected] + 1) as rk
,@sum_score_back := @sum_score_front
from (
select SId
,sum(score) as sum_score
from SC
group by SId
order by sum_score desc
) t1
) tt1;
22. Count the scores of all subjects and grades ⼈ Count : Course number , Course name ,[100-85],[85-70],[70-60],[60-0] And the percentage ⽐
select t1.CId
,t2.CName
,t1.85to100_cnt
,t1.85to100_cnt_percent
,t1.70to85_cnt
,t1.70to85_cnt_percent
,t1.60to70_cnt
,t1.60to70_cnt_percent
,t1.0to60_cnt
,t1.0to60_cnt_percent
from(
select CId
,sum(if(score>=85 and score<=100,1,0)) as 85to100_cnt
,round(sum(if(score>=85 and score<=100,1,0))/count(SId),2) as 85to100_cnt_percent
,sum(if(score>=70 and score<=85,1,0)) as 70to85_cnt
,round(sum(if(score>=70 and score<=85,1,0))/count(SId),2) as 70to85_cnt_percent
,sum(if(score>=60 and score<=70,1,0)) as 60to70_cnt
,round(sum(if(score>=60 and score<=70,1,0))/count(SId),2) as 60to70_cnt_percent
,sum(if(score>=0 and score<=60,1,0)) as 0to60_cnt
,round(sum(if(score>=0 and score<=60,1,0))/count(SId),2) as 0to60_cnt_percent
from SC
group by CId
)t1 left join Course t2
on t1.CId = t2.CId;
23. Check the records of the top three students in each subject
-- Within the group TopN
24. Query each ⻔ The course is an elective course ⽣ Count
select CId
,count(SId) as cnt
from SC
group by CId;
25. Only two elective courses are found ⻔ Course learning ⽣ Student number and name
select SId
,SName
from Student
where SId in (
select t1.SId
from (
select SId
,count(CId) as cnt
from SC
group by SId
having cnt = 2
) t1
);
26. Inquiry man ⽣、⼥⽣⼈ Count
select SSex
,count(SId) as cnt
from Student
group by SSex;
27. The query name contains 「⻛」 Word learning ⽣ Information
-- like
select SId
,SName
from Student
where SName like '% wind %';
-- re
select SId
,SName
from Student
where SName rlike '.* wind .*';
28. Query homonymy ⽣ list , And statistics of the same name ⼈ Count
select SName
,count(SId) as cnt
from Student
group by SName
having cnt>1;
29. Inquire about 1990 New year's day ⽣ Science ⽣ list
select SId
,SName
,SAge
from Student
where year(SAge) = '1990';
30. Query each ⻔ The average grade of the course , The results are in descending order of average score , The average is the same , In ascending order of course number
select CId
,round(avg(score),2) as avg_score
from SC
group by CId
order by avg_score desc,CId;
31. Check the grade average ⼤ Is equal to 85 All my studies ⽣ The student id 、 Name and average score
select t1.SId
,t2.SName
,t1.avg_score
from (
select SId
,round(avg(score),2) as avg_score
from SC
group by SId
having avg_score>=85
) t1 left join Student t2
on t1.SId = t2.SId;
32. Query the course name as 「 mathematics 」, And the score is lower than 60 Science ⽣ Name and score
select t1.SId
,t2.SName
,t1.score
from (
select SId
,score
from SC
where CId in (
select CId
from Course
where CName = ' mathematics '
)
) t1 left join Student t2
on t1.SId = t2.SId;
33. Query all learning ⽣ Courses and scores ( Existentialism ⽣ No results , No class selection )
select t1.SId
,t1.SName
,t2.CId
,t3.CName
,t2.score
from Student t1
left join SC t2
on t1.SId = t2.SId
left join Course t3
on t2.CId = t3.CId
order by SId;
34. Query any ⼀⻔ Course grades are 70 Score the above names 、 Course name and score
-- As long as one subject exceeds 70 branch , Others do not exceed 70 Our subjects also need to be displayed
select t1.SId
,t1.SName
,t3.CName
,t2.score
from(
select SId
,SName
from Student
where SId in (
select distinct
SId
from SC
where score > 70
)
) t1 left join SC t2
on t1.SId = t2.SId
left join Course t3
on t2.CId = t3.CId
order by SId;
35. Check for failed courses
select distinct
CId
from SC
where score < 60;
36. The course number is 01 And the course results are in 80 More than 10 points ⽣ Your student number and name
select SId
,SName
from Student
where SId in (
select SId
from SC
where CId = '01' and score > 80
);
37. Seek every ⻔ Course learning ⽣⼈ Count
select CId
,count(SId) as cnt
from SC
group by CId;
38. The results are not repeated , Check options 「 Zhang San 」⽼ The learning of the courses taught by teachers ⽣ in , The most ⾼ Science ⽣ Information and achievements
set @i := 0;
set @score_front := 0;
set @score_back := 0;
select tt1.SId
,tt1.score
,tt2.SName
from (
select t1.SId
,t1.score
,@score_front := t1.score
,if(@score_front = @score_back,@i,@i := @i+1) as rk
,@score_back := @score_front
from (
select SId
,score
from SC
where CId in (
select CId
from Course
where TId in (
select TId
from Teacher
where TName = ' Zhang San '
)
) order by score desc
) t1
) tt1 join Student tt2
on tt1.SId = tt2.SId and tt1.rk = 1;
39. If there is a repetition of the results , Check options 「 Zhang San 」⽼ The learning of the courses taught by teachers ⽣ in , The most ⾼ Science ⽣ Information and achievements
set @i := 0;
set @score_front := 0;
set @score_back := 0;
select tt1.SId
,tt1.score
,tt2.SName
from (
select t1.SId
,t1.score
,@score_front := t1.score
,if(@score_front = @score_back,@i,@i := @i+1) as rk
,@score_back := @score_front
from (
select SId
,score
from SC
where CId in (
select CId
from Course
where TId in (
select TId
from Teacher
where TName = ' Zhang San '
)
) order by score desc
) t1
) tt1 join Student tt2
on tt1.SId = tt2.SId and tt1.rk = 1;
40. Query the students who have the same grades in different courses ⽣ Science ⽣ Number 、 Course number 、 learn ⽣ achievement
select t2.SId
,t1.CId
,t2.score
from SC t1
join (
select SId
,score
,count(CId) as cnt
from SC
group by SId,score
having cnt > 1
) t2 on t1.SId = t2.SId and t1.score = t2.score;
41. Query each ⻔ The top two in the course
-- Rank within the group TopN
42. Count every ⻔ Course learning ⽣ Elective ⼈ Count ( exceed 5 ⼈ The course Statistics )
select CId
,count(SId) as cnt
from SC
group by CId
having cnt > 5;
43. retrieval ⾄ Take two less courses ⻔ Course learning ⽣ Student number
select SId
from SC
group by SId
having count(CId) > 2;
44. I have inquired about all the elective courses ⽣ Information
select SId
,SName
from Student
where SId in (
select SId
from SC
group by SId
having count(CId) = (
select count(*)
from Course
)
);
45. Inquire about each school ⽣ In the age of , Only by year
select SId
,SName
,year(current_timestamp()) - year(SAge) as age
from Student;
46. According to ⽣⽇ Period to calculate , At present ⽉⽇ < Out ⽣ year ⽉ Of ⽉⽇ Then the age decreases ⼀
select SId
,SName
,if(now_date>born_date,age,age-1) as new_age
,age
from (
select SId
,SName
,year(current_timestamp()) - year(SAge) as age
,date_format(current_timestamp(),"%m%d") as now_date
,date_format(SAge,"%m%d") as born_date
from Student
) t1;
47. Query this week ⽣⽇ Science ⽣
select SId
,SName
,SAge
from Student
where week(SAge) = week(current_timestamp());
48. Check next week ⽣⽇ Science ⽣
select SId
,SName
,SAge
from Student
where week(SAge) = week(DATE_ADD(current_timestamp(),INTERVAL 7 day));
49. Inquiry Book ⽉ too ⽣⽇ Science ⽣
select SId
,SName
,SAge
from Student
where month(SAge) = month(current_timestamp());
50. Under query ⽉ too ⽣⽇ Science ⽣
select SId
,SName
,SAge
from Student
where month(SAge) = month(DATE_ADD(current_timestamp(),INTERVAL 1 mon));
边栏推荐
猜你喜欢
随机推荐
Cad-gis data conversion
第七章 单行函数
Flex elastic box item properties
Sorting out problems related to ArcMap join table
渐进增强和优雅降级
话题功能实现
1:开启慢查询日志 与 找到慢SQL
蓝桥杯 方格填数
Zotero - a document management tool
数字藏品以虚强实,赋能实体经济发展
MySQL练习题50道+答案
(php毕业设计)基于php甘肃旅游网站管理系统获取
扩展欧几里得定理
mysql多表查询
微信公众号-授权登录
Books - investment ideas and Strategies
HDU-1284:钱币兑换问题 推理+动态规划(dp)
Help document making based on easy CHM and vs
CVE_2017_11882漏洞复现(Metasploit开启NT远程桌面添加账户)
js-promise实现逻辑









