当前位置:网站首页>MySQL classic 50 practice questions and the most detailed analysis of the whole network
MySQL classic 50 practice questions and the most detailed analysis of the whole network
2022-08-02 06:57:00 【hungry very hungry】
MySQL练习
文章目录
- MySQL练习
- 50道经典SQLExercises so most detailed analytic
- 数据表介绍
- 建表语句
- 插入数据
- 练习题目
- 1.查询" 01 “课程比” 02 "课程成绩高的学生的信息及课程分数
- 2.查询同时存在" 01 “课程和” 02 "课程的情况
- 3.查询存在" 01 “课程但可能不存在” 02 "课程的情况(不存在时显示为 null )
- 4.查询不存在" 01 “课程但存在” 02 "课程的情况
- 5.查询平均成绩?于等于 60 分的同学的学生编号和学生姓名和平均成绩
- 6.查询在 SC 表存在成绩的学生信息
- 7.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩,没成绩的显示为 null
- 8.查询「李」姓老师的数量
- 9.查询学过「张三」老师授课的同学的信息
- 10.查询没有学全所有课程的同学的信息
- 11.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
- 12.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息
- 13.查询没学过"张三"老师讲授的任一门课程的学生姓名
- 14.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
- 15.检索" 01 "课程分数小于 60,按分数降序排列的学生信息
- 16.按平均成绩从?To the low shows all students of all course grades and gpa
- 17.查询各科成绩最?分、最低分和平均分: 以如下形式显示:课程 ID,课程 name,最?分,最低分,平均分,及格率,中等率,优良率,优秀率 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
- 18.按各科平均成绩进行排序,并显示排名
- 19.按各科平均成绩进行排序,并显示排名,
重复时不保留名次空缺
- Three common ranking
- ROW_NUMBER
- DENSE_RANK
- RANK
- ROW_NUMBER
- DENSE_RANK
- RANK
- 20.查询学生的总成绩,并进行排名,总分重复时保留名次空缺
- 21.查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
- 22.统计各科成绩各分数段人数:课程编号,课程名称,[100-85),[85-70),[70-60),[60-0)及所占百分
- 23.查询各科成绩前三名的记录
- 24.查询每门课程被选修的学生数
- 25.查询出只选修两门课程的学生学号和姓名
- 26.查询男生、女生人数
- 27.查询名字中含有「风」字的学生信息
- 28.查询同名同性学生名单,并统计同名同性人数
- 29.查询 1990 年出生的学生名单
- 30.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
- 31.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
- 32.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
- 33.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
- 34.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
- 35.查询不及格的课程
- 36.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名
- 37.求每门课程的学生人数
- 38.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
- 39.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
- 40.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
- 41.查询每门课程成绩最好的前两名
- 42.统计每门课程的学生选修人数(超过 5 人的课程才统计).
- 43.检索至少选修两门课程的学生学号
- 44.查询选修了全部课程的学生信息
- 45.查询各学生的年龄,只按年份来算
- 46.按照出生日期来算,当前月日 < 出生年月的月日,则年龄减一
- 47.查询本周过生日的学生
- 48.查询下周过生日的学生
- 49.查询本月过生日的学生
- 50.查询下月过生日的学生
50道经典SQLExercises so most detailed analytic
数据表介绍
1.Learn to watch Student(SId,Sname,Sage,Ssex)
SId Learning Numbers
Sname Learn the name
Sage A year
Ssex Learn the gender
2.课程表 Course(CId,Cname,TId)
CId 课程编号
Cname 课程名称
TId 教师编号
3.教师表 Teacher(TId,Tname)
TId 教师编号
Tname 教师姓名
4.成绩表 SC(SId,CId,score)
SId Learning Numbers
CId 课程编号
score 分数
建表语句
Learn to watch Student
create table Student( SId varchar(10), Sname varchar(10), Sage datetime, Ssex varchar(10) );
课程表 Course
create table Course( CId varchar(10), Cname nvarchar(10), TId varchar(10) );
教师表 Teacher
create table Teacher( TId varchar(10), Tname varchar(10) );
成绩表 SC
create table SC( SId varchar(10), CId varchar(10), score decimal(18,1) );
插入数据
注意这里插入数据的时候,It may contain hidden characters,Couldn't show the data manually inserted again can you type again
Learn to watch Student
-- 学生表 Student -- 学生表 Student insert into Student values('01' , '赵雷' , '1990-01-01' , '男'); insert into Student values('02' , '钱电' , '1990-12-21' , '男'); insert into Student values('03' , '孙风' , '1990-12-20' , '男'); insert into Student values('04' , '李云' , '1990-12-06' , '男'); insert into Student values('05' , '周梅' , '1991-12-01' , '女'); insert into Student values('06' , '吴兰' , '1992-01-01' , '女'); insert into Student values('07' , '郑竹' , '1989-01-01' , '女'); insert into Student values('09' , '张三' , '2017-12-20' , '女'); insert into Student values('10' , '李四' , '2017-12-25' , '女'); insert into Student values('11' , '李四' , '2012-06-06' , '女'); insert into Student values('12' , '赵六' , '2013-06-13' , '女'); insert into Student values('13' , '孙七' , '2014-06-01' , '女');
课程表 Course
-- 科?表 Course insert into Course values('01' , '语文' , '02'); insert into Course values('02' , '数学' , '01'); insert into Course values('03' , '英语' , '03');
教师表 Teacher
-- 教师表 Teacher insert into Teacher values('01' , '张三'); insert into Teacher values('02' , '李四'); insert into Teacher values('03' , '王五');
成绩表 SC
-- 成绩表 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);
练习题目
1.查询" 01 “课程比” 02 "课程成绩高的学生的信息及课程分数
分析:
1、找出有01Achievement of student achievement information
2、找出有02Achievement of student achievement information
3、通过SIdWill take two table aliast1、t2进行左连接
4、加上满足01‘语文’ > 02’数学’的条件
找出有01Achievement of student achievement information
SELECT * FROM SC WHERE CId=‘01’;
找出有02Achievement of student achievement information
SELECT * FROM SC WHERE CId=‘02’;
通过SIdWill take two table aliast1、t2进行左连接
SELECT t1.SId,
t1.CId,
t1.score as ‘语文’,
t2.score as ‘数学’
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;
加上满足01‘语文’ > 02’数学’的条件
SELECT t1.SId,
t1.CId,
t1.score as ‘语文’,
t2.score as ‘数学’
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
WHERE t1.score > t2.score;
Finally the above table as a child tablett1Will we want to query table relate,Take out want to query field
SELECT tt1.SId
,tt2.Sname
,tt3.CId
,tt3.score
FROM (
SELECT t1.SId
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
WHERE t1.Score > t2.Score
) tt1
JOIN Student tt2 ON tt1.SId = tt2.SId
JOIN SC tt3 ON tt1.SId = tt3.SId;
2.查询同时存在" 01 “课程和” 02 "课程的情况
分析:
满足条件的SC表中:
1、Select course number is01的全部信息 AS命名为 t1
2、Select course number is02的全部信息 AS命名为 t2
3、使用joinConnection to take out the exist01课程和02课程的SId
Select course number is01的全部信息
SELECT SId FROM SC WHERE CId = ‘01’;
Select course number is02的全部信息
SELECT SId FROM SC WHERE CId = ‘02’;
使用joinConnection to take out the exist01课程和02课程的SId
SELECT t1.SId
FROM(
SELECT SId
FROM SC
WHERE CId=‘01’
)AS t1 JOIN (
SELECT SId
FROM SC
WHERE CId=‘01’
)AS t2
ON t1.SId = t2.SId;
3.查询存在" 01 “课程但可能不存在” 02 "课程的情况(不存在时显示为 null )
分析:
满足条件的SC表中:
1、Select course number is01的全部信息 AS命名为 t1
2、Select course number is02的全部信息 AS命名为 t2
3、左连接
Select course number is01的全部信息
SELECT SId,CId,score FROM SC WHERE CId = ‘01’;
Select course number is02的全部信息
SELECT SId,CId,score FROM SC WHERE CId = ‘02’;
左连接
SELECT t1.SId
,t1.CId
,t1.score
,t2.CId AS t2CId
,t2.score AS t2Score
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;
4.查询不存在" 01 “课程但存在” 02 "课程的情况
分析:
满足条件的SC表中:
1、Select course number is01的全部信息 AS命名为 t1
2、Select course number is02的全部信息 AS命名为 t2
3、右连接
Select course number is01的全部信息
SELECT SId,CId,score FROM SC WHERE CId = ‘01’;
Select course number is02的全部信息
SELECT SId,CId,score FROM SC WHERE CId = ‘02’;
右连接
SELECT t1.SId
,t1.CId
,t1.score
,t2.CId AS t2CId
,t2.score AS t2Score
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;
5.Average query in equal to 60 分的同学的学生编号和学生姓名和平均成绩
分析:
1、To find out the average scores greater than60分的SId,并用ROUND(X,D)保留两位小数
2、Then average as tablet1与Student表t2Make the connection the results
To find out the average scores greater than60分的SId,并用ROUND(X,D)保留两位小数
SELECT SId
,ROUND(AVG(score),2)AS avg_score
FROM SC
GROUP BY SId
HAVING avg_score>=60;
Then average as tablet1与Student表t2Make the connection the results
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>=60
)t1 JOIN Student t2
ON t1.SId = t2.SId;
6.查询在 SC 表存在成绩的学生信息
分析:
1、首先DISTINCTSC表的SId的数据
2、In the table as the tablet1与Student表t2Join query student information
首先DISTINCTSC表的SId的数据
SELECT
DISTINCT SId
FROM SC;
In the table as the tablet1与Student表t2Join query student information
SELECT t1.SId
,t2.Sname
FROM(
SELECT
DISTINCT SId
FROM SC
)t1 JOIN Student t2
ON t1.SId = t2.SId;
7.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩,没成绩的显示为 null
分析:
1、首先统计出SC表SIdThe total number of course of study and overall
2、In the table as the tablet1与Student表t2Do the right connection query out all classmates learn code、Learn the name、选课总数、所有课程的总成绩,没成绩的显示为 null
首先统计出SC表SIdThe total number of course of study and overall
SELECT SId
,COUNT(CId) AS cnt
,SUM(score) AS sum_score
FROM SC
GROUP BY SId;
In the table as the tablet1与Student表t2Do the right connection query out all classmates learn code、Learn the name、选课总数、所有课程的总成绩,没成绩的显示为 null
SELECT t2.SId
,t2.Sname
,t1.cnt AS ‘选课总数’
,t1.sum_score AS ‘总成绩’
FROM(
SELECT SId
,COUNT(CId) AS cnt
,SUM(score) AS sum_score
FROM SC
GROUP BY SId
)t1 RIGHT JOIN Student t2
ON t1.SId = t2.SId;
8.查询「李」姓老师的数量
分析:直接使用COUNT(*)统计LIKE模糊查询查询「李」The number of name is t
直接使用COUNT(*)统计LIKE模糊查询查询「李」The number of name is t
SELECT COUNT(*)
FROM Teacher
WHERE Teacher.Tname
LIKE (‘李%’);
9.查询学过「张三」老师授课的同学的信息
分析:
1、First of all query zhang teacherTId
2、Then the query zhang teacher teaching informationCId
3、Query studied zhang teacher taught students againSId
4、Finally will be studied zhang teacher taught studentsSId作为表t1和Student表t2Join query student information
First of all query zhang teacherTId
SELECT TId
FROM Teacher
WHERE Tname = ‘张三’;
Then the query zhang teacher teaching informationCId
SELECT CId
FROM Course
WHERE TId =(
SELECT TId
FROM Teacher
WHERE Tname = ‘张三’
);
Query studied zhang teacher taught students againSId
SELECT SId
FROM SC
WHERE CId = (
SELECT CId
FROM Course
WHERE TId =(
SELECT TId
FROM Teacher
WHERE Tname = ‘张三’
)
);
Finally will be studied zhang teacher taught studentsSId作为表t1和Student表t2Join query student information
SELECT t2.SId
,t2.Sname
FROM(
SELECT SId
FROM SC
WHERE CId = (
SELECT CId
FROM Course
WHERE TId =(
SELECT TId
FROM Teacher
WHERE Tname = ‘张三’
)
))t1 JOIN Student t2
ON t1.SId = t2.SId;
10.查询没有学全所有课程的同学的信息
分析:
1、Query all course information before
2、Then all course information as the tablet1与Student表t2Students join queries in information
3、The number of queries all course information
4、The final table astt1Table to the course information filtering is less than the query out did not learn all of the total course of students of information
Query all course information before
SELECT SId
,CId
FROM SC;
Then all course information as the tablet1与Student表t2Students join queries in information
SELECT t1.SId
,t1.SId
,t2.Sname
FROM(
SELECT SId
,CId
FROM SC
) t1 JOIN Student t2
ON t1.SId = t2.SId;
The number of queries all course information
SELECT count(*)
FROM Course;
The final table astt1Table to the course information filtering is less than the query out did not learn all of the total course of students of information
SELECT tt1.SId
,tt1.SName
,count(tt1.CId) as cnt
FROM (
SELECT t1.SId
,t1.CId
,t2.SName
FROM SC t1 JOIN Student t2
ON t1.SId = t2.SID
) tt1 GROUP BY tt1.SId,tt1.SName
Having cnt < (SELECT count(*) FROM Course);
11.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
分析:
1、Query student id, first of all, for01Students learn the course number
2、Then queries at least one course with01Students learned the same student number
3、The above-mentioned results astt2与Student表tt2进行左连接查询,找到学生信息
Query student id, first of all, for01Students learn the course number
SELECT CId
FROM SC
WHERE SId = ‘01’;
Then queries at least one course with01Students learned the same student number
SELECT t2.SId
,COUNT(t2.SId)
FROM(
SELECT CId
FROM SC
WHERE SId = ‘01’
)t1 JOIN SC t2
ON t1.CId = t2.CId
GROUP BY t2.SId;
The above-mentioned results astt2与Student表tt2进行左连接查询,找到学生信息
SELECT tt2.SId
,tt2.Sname
FROM(
SELECT t2.SId
,COUNT(t2.SId)
FROM(
SELECT CId
FROM SC
WHERE SId = ‘01’
)t1 JOIN SC t2
ON t1.CId = t2.CId
GROUP BY t2.SId
) tt1 LEFT JOIN Student tt2
ON tt1.SId = tt2.SId;
解法2
分析:
1、Query student id, first of all, for01Students learn the course number
2、Then of course, is not01All the students' course information filtering
3、最后将SC表作为t1表与Student表t2表进行关联,找到学生信息
Query student id, first of all, for01Students learn the course number
SELECT CId
FROM SC
WHERE SId = ‘01’;
Then of course, is not01All the students' course information filtering
SELECT DISTINCT SId
FROM SC
WHERE SId != ‘01’
AND CId IN(
SELECT CId
FROM SC
WHERE SId = ‘01’
);
最后将SC表作为t1表与Student表t2表进行关联,找到学生信息
SELECT DISTINCT t1.SId
,t2.Sname
FROM
SC t1 JOIN Student t2
ON t1.SId = t2.SId
WHERE t1.SId != ‘01’
AND CId IN(
SELECT CId
FROM SC
WHERE SId = ‘01’
);
12.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息
分析:
1、首先查询出学号为01的课程信息
2、And then query out student id is not as01的课程信息
3、The above two tables respectively ast1、t2进行关联,得出01Students learn all course information and all the other students information
4、然后将SC表作为t1表与Student表t2表进行关联、表tt1Group of student number for01The student to carry on the screen
首先查询出学号为01的课程信息
SELECT SId
,CId
FROM SC
WHERE SId=‘01’;
And then query out student id is not as01的课程信息
SELECT SId
,CId
FROM SC
WHERE SId!=‘01’;
The above two tables respectively ast1、t2进行关联,得出01Students learn all course information and all the other students information
SELECT t2.SId
,t1.CId AS t1CId
,t2.CId AS t2CId
FROM(
SELECT SId
,CId
FROM SC
WHERE SId=‘01’
) t1 JOIN(
SELECT SId
,CId
FROM SC
WHERE SId!=‘01’) t2
ON t1.CId = t2.CId;
然后将SC表作为t1表与Student表t2表进行关联、表tt1Group of student number for01The student to carry on the screen
SELECT tt1.SId
,COUNT(tt1.t1CId) AS t1Cnt
,COUNT(tt1.t2CId) as t2Cnt
FROM(
SELECT t2.SId
,t1.CId AS t1CId
,t2.CId AS t2CId
FROM(
SELECT SId
,CId
FROM SC
WHERE SId=‘01’
) t1 JOIN(
SELECT SId
,CId
FROM SC
WHERE SId!=‘01’) t2
ON t1.CId = t2.CId) tt1 GROUP BY tt1.SId
HAVING t1Cnt AND t2Cnt = (SELECT COUNT(*)
FROM SC
WHERE SId = ‘01’);
The final table asttt1与Student表ttt2进行关联,找到学生信息
SELECT ttt1.SId
,ttt2.Sname
FROM(
SELECT tt1.SId
,COUNT(tt1.t1CId) AS t1Cnt
,COUNT(tt1.t2CId) as t2Cnt
FROM(
SELECT t2.SId
,t1.CId AS t1CId
,t2.CId AS t2CId
FROM(
SELECT SId
,CId
FROM SC
WHERE SId=‘01’
) t1 JOIN(
SELECT SId
,CId
FROM SC
WHERE SId!=‘01’) t2
ON t1.CId = t2.CId) tt1 GROUP BY tt1.SId
HAVING t1Cnt AND t2Cnt = (SELECT COUNT(*)
FROM SC
WHERE SId = ‘01’)
) ttt1 JOIN Student ttt2
ON ttt1.SId = ttt2.SId;
13.查询没学过"张三"老师讲授的任一门课程的学生姓名
1、First query out zhang teacher teach
2、And then query out zhang teacher teach any number of course
3、To query the zhang teacher taught any of course number correspondingSId
4、Haunted by the last query learned"张三"老师讲授的任一门课程的学生姓名
First query out zhang teacher teach
SELECT TId
FROM Teacher
WHERE Tname = ‘张三’;
And then query out zhang teacher teach any number of course
SELECT CId
FROM Course
WHERE TId = (
SELECT TId
FROM Teacher
WHERE Tname = ‘张三’
);
To query the zhang teacher taught any of course number correspondingSId
SELECT SId
FROM SC
WHERE CId in(
SELECT CId
FROM Course
WHERE TId = (
SELECT TId
FROM Teacher
WHERE Tname = ‘张三’)
);
Haunted by the last query learned"张三"老师讲授的任一门课程的学生姓名
SELECT SId
,Sname
FROM Student
WHERE SId
NOT IN (
SELECT SId
FROM SC
WHERE CId in(
SELECT CId
FROM Course
WHERE TId = (
SELECT TId
FROM Teacher
WHERE Tname = ‘张三’)
)
);
14.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
分析:
1、Failed in the first query classmate student id and course number
2、再以SIdGroups will be more than two doors and failed the course student number and average scores query out
3、Finally the table as tablett1与Student表tt2做关联,Take out to query information
Failed in the first query classmate student id and course number
SELECT SId
,CId
,score
FROM SC
WHERE score < 60;
再以SIdGroups will be more than two doors and failed the course student number and average scores query out
SELECT t1.SId
,COUNT(t1.CId) AS cnt
,AVG(t1.score) AS avg_score
FROM(
SELECT SId
,CId
,score
FROM SC
WHERE score < 60) t1
GROUP BY SId HAVING cnt >=2;
Finally the table as tablett1与Student表tt2做关联,Take out to query information
SELECT tt2.SId,tt2.Sname,tt1.avg_score FROM
(SELECT t1.SId
,COUNT(t1.CId) AS cnt
,AVG(t1.score) AS avg_score
FROM(
SELECT SId
,CId
,score
FROM SC
WHERE score < 60) t1
GROUP BY SId HAVING cnt >=2) tt1 JOIN Student tt2
ON tt1.SId = tt2.SId;
15.检索" 01 "课程分数小于 60,按分数降序排列的学生信息
分析:
1、首先先将SC表t1与Student表t2进行关联
2、The last searching01课程分数小于60,按分数降序排列的学生信息
首先先将SC表t1与Student表t2进行关联
SELECT t2.SId
,t2.Sname
,t1.score
FROM
SC t1 JOIN Student t2
ON t1.SId = t2.SId;
The last searching01课程分数小于60,按分数降序排列的学生信息
SELECT t2.SId
,t2.Sname
,t1.score
FROM
SC t1 JOIN Student t2
ON t1.SId = t2.SId
AND t1.CId = ‘01’ AND t1.score < 60
ORDER BY t1.score DESC;
16.According to the average scores from low to show all students of all course grades and gpa
分析:
1、First of all find out first’01’语文、'02’数学、'03’English courses, the corresponding result
2、And then find out all course grade point average
3、Because of the language、数学、All three courses to the average English group
4、Finally the table after grouping astt1Table respectively with’01’语文、'02’数学、'03’English left connection,Find out all the average score of each course students,Then ascending according to average
First of all find out first’01’语文、'02’数学、'03’English courses, the corresponding result
SELECT SId,score FROM SC WHERE SId = ‘01’;
SELECT SId,score FROM SC WHERE SId = '02';
SELECT SId,score FROM SC WHERE SId = '03';
And then find out all course grade point average
SELECT SId
,AVG(score) as avg_socre
FROM SC
GROUP BY SId;
Because of the language、数学、All three courses to the average English group
SELECT t1.SId
,AVG(t1.score) as avg_score
FROM
SC t1 GROUP BY t1.SId;
Finally the table after grouping astt1Table respectively with’01’语文、'02’数学、'03’English left connection,Find out all the average score of each course students,Then ascending according to average
SELECT tt1.SId ,tt1.avg_score AS '平均分' ,tt2.score AS '语文' ,tt3.score AS '数学' ,tt4.score AS '英语'
FROM(
SELECT t1.SId
,AVG(t1.score) as avg_score
FROM
SC t1 GROUP BY t1.SId
) tt1
LEFT JOIN (select SId,score from SC where CId = ‘01’) tt2 on tt1.SId = tt2.SId
LEFT JOIN (select SId,score from SC where CId = ‘02’) tt3 on tt1.SId = tt3.SId
LEFT JOIN (select SId,score from SC where CId = ‘03’) tt4 on tt1.SId = tt4.SId
ORDER BY tt1.avg_score DESC;
17.The query subjects the points、最低分和平均分: 以如下形式显示:课程 ID,课程 name,The most points,最低分,平均分,及格率,中等率,优良率,优秀率 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
首先先将SC表和CourseCorrelation and grouped
SELECT t1.CId
,t2.Cname
FROM
SC t1 JOIN Course t2
ON t1.CId = t2.CId
GROUP BY t1.CId,t2.Cname;
And then calculate the highest respectively、最低分、平均分、及格率、中等率、优良率、优秀率,And the query results in descending order number,若人数相同,按课程号升序排列
SELECT t1.CId
,t2.Cname
,MAX(score) AS ‘最高分’
,MIN(score) AS ‘最低分’
,AVG(score) AS ‘平均分’
,CONCAT(ROUND(SUM(IF(score >= 60,1,0))*100/COUNT(score),2),“%”) AS ‘及格率’
,CONCAT(ROUND(SUM(IF(score >= 70,1,0))*100/COUNT(score),2),“%”) AS ‘中等率’
,CONCAT(ROUND(SUM(IF(score >= 80,1,0))*100/COUNT(score),2),“%”) AS ‘优良率’
,CONCAT(ROUND(SUM(IF(score >= 90,1,0))*100/COUNT(score),2),“%”) AS ‘优秀率’
,COUNT(score) AS ‘人数’
FROM
SC t1 JOIN Course t2
ON t1.CId = t2.CId
GROUP BY t1.CId,t2.Cname;
ORDER BY ‘人数’ DESC,CId ASC;
18.按各科平均成绩进行排序,并显示排名
分析:
1、First query the various branches of average and sorting
2、Finally define a [email protected] the table as at1Ranking table calculation,查询出结果
First query the various branches of average and sorting
SELECT CId
,AVG(score) avg_score
FROM SC
GROUP BY CId
ORDER BY avg_score DESC;
Finally define a [email protected] the table as at1Ranking table calculation,查询出结果
SET @i :=0;-- 定义一个变量
SELECT t1.CId
,t1.avg_score
,@i := @i + 1 AS ‘排名’
FROM(
SELECT CId
,AVG(score) avg_score
FROM SC
GROUP BY CId
ORDER BY avg_score DESC
) t1;
19.按各科平均成绩进行排序,并显示排名,重复时不保留名次空缺
分析:
1、First query the various branches of average and sorting
2、Finally define a [email protected] the table as at1Ranking table calculation,查询出结果
First query the various branches of average and sorting
SELECT CId
,AVG(score) avg_score
FROM SC
GROUP BY CId
ORDER BY avg_score DESC;
Finally define a [email protected] the table as at1Ranking table calculation,查询出结果
SET @i :=0;-- 定义一个变量
SELECT t1.CId
,t1.avg_score
,@i := @i + 1 AS ‘排名’
FROM(
SELECT CId
,AVG(score) avg_score
FROM SC
GROUP BY CId
ORDER BY avg_score DESC
) t1;
Three common ranking
row_number、dense_rank、rank在MySQL 5.7中的实现
对SC中的学生scoreIn the overall ranking
ROW_NUMBER
1 2 3 4 5 6 7 No repeat ranking,依次递增
SET @i := 0;
SELECT t1.SId
,t1.CId
,t1.score
,@i := @i + 1 as row_number
from (
SELECT SId
,CId
,score
from SC
order by score desc
) t1;
DENSE_RANK
1 2 3 3 3 4 5 6 7 Have a repeat for ranking,The ranking is also continuous
SET @i := 0;
SET @p := 0;
SET @q := 0;
SELECT t1.SId
,t1.CId
,t1.score
,@p := t1.score
,if(@[email protected],@i,@i := @i+1) as dense_rank
,@q :[email protected]
from (
SELECT SId
,CId
,score
from SC
order by score desc
) t1;
RANK
1 2 3 3 3 6 7 8 Have a repeat for ranking,The ranking of discontinuity
SET @i := 0;
SET @j := 0;
SET @p := 0;
SET @q := 0;
SELECT t1.SId
,t1.CId
,t1.score
,@j := @j + 1
,@p := t1.score
,if(@[email protected],@i,@i := @j) as rank
,@q :[email protected]
from (
SELECT SId
,CId
,score
from SC
order by score desc
) t1;
To group ranked
ROW_NUMBER
The top three query each course grade one of the best 1 2 3 4 5 6 7 No repeat ranking,依次递增
SET @i := 0;
SET @p := 0;
SET @q := 0;
SELECT tt1.SId
,tt2.SName
,tt1.CId
,tt1.score
,tt1.rn
from (
select t1.SId
,t1.CId
,t1.score
,@p := t1.CId
,if(@[email protected],@i := @i + 1,@i :=1) as rn
,@q := @p
from (
select SId
,CId
,score
from SC
order by CId,score DESC
) t1
) tt1 join Student tt2 on tt1.rn<=3 and tt1.SId = tt2.SId
order by tt1.CId,tt1.rn;
DENSE_RANK
The top three query each course grade one of the best 1 2 3 3 3 4 5 6 7 Have a repeat for ranking,The ranking is also continuous
SET @i := 0;
SET @p := 0;
SET @q := 0;
SET @j := 0;
SET @k := 0;
SELECT tt1.SId
,tt2.SName
,tt1.CId
,tt1.score
,tt1.rn
from (
select t1.SId
,t1.CId
,t1.score
,@p := t1.CId
,@j := t1.score
,if(@[email protected],if(@[email protected],@i,@i := @i + 1),@i :=1) as rn
,@q := @p
,@k := @j
from (
select SId
,CId
,score
from SC
order by CId,score DESC
) t1
) tt1 join Student tt2 on tt1.rn<=3 and tt1.SId = tt2.SId
order by tt1.CId,tt1.rn;
RANK
The top three query each course grade one of the best 1 2 3 3 3 6 7 8 Have a repeat for ranking,The ranking of discontinuity
SET @i := 0;
SET @p := 0;
SET @q := 0;
SET @j := 0;
SET @k := 0;
SET @m := 1;
SELECT tt1.SId
,tt2.SName
,tt1.CId
,tt1.score
,tt1.rn
from (
select t1.SId
,t1.CId
,t1.score
,@p := t1.CId
,@j := t1.score
,if(@[email protected],@m := @m + 1,@m := 1)
,if(@[email protected],if(@[email protected],@i,@i := @m),@i :=1) as rn
,@q := @p
,@k := @j
from (
select SId
,CId
,score
from SC
order by CId,score DESC
) t1
) tt1 join Student tt2 on tt1.rn<=3 and tt1.SId = tt2.SId
order by tt1.CId,tt1.rn;
20.查询学生的总成绩,并进行排名,总分重复时保留名次空缺
122345
分析:
1、First query first grade,以SId进行分组,并进行排序
2、The final table ast1Table and then define [email protected]、@j进行排名、@p、@qWhen used to control total repeat 保留名次空缺
First query first grade,以SId进行分组,并进行排序
SELECT SId
,SUM(score)
AS sum_score
FROM SC
GROUP BY SId
ORDER BY sum_score DESC;
*The final table ast1Table and then define [email protected]、@j进行排名、@p、@qWhen used to control total repeat 保留名次空缺
SET @i :=0;
SET @j :=0;
SET @p :=0;
SET @q :=0;
SELECT t1.SId
,t1.sum_score AS '总分'
,@j :[email protected] + 1
,@p :=t1.sum_score
,IF(@[email protected],@j,@i :[email protected]) AS '排名'
,@q :[email protected]
FROM(
SELECT SId
,SUM(score)
AS sum_score
FROM SC
GROUP BY SId
ORDER BY sum_score DESC
) t1;
21.查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
122456
First query first grade,以SId进行分组,并进行排序
SELECT SId
,SUM(score)
AS sum_score
FROM SC
GROUP BY SId
ORDER BY sum_score DESC;
*The final table ast1Table and then define [email protected]进行排名、@p、@qWhen used to control the total score repeated don't keep vacancy place
SET @i :=0;
SET @p :=0;
SET @q :=0;
SELECT t1.SId
,t1.sum_score AS '总分'
,@p := t1.sum_score
,if(@[email protected],@i,@i := @i+1) as '排名'
,@q :[email protected]
FROM(
SELECT SId
,SUM(score)
AS sum_score
FROM SC
GROUP BY SId
ORDER BY sum_score DESC
) t1;
22.统计各科成绩各分数段人数:课程编号,课程名称,[100-85),[85-70),[70-60),[60-0)及所占百分
分析:
1、首先将SC表t1与Course表t2进行关联,并以CId进行分组
2、Calculated for each block final score,显示出来
首先将SC表t1与Course表t2进行关联,并以CId进行分组
SELECT t1.CId
,t2.Cname
FROM
SC t1 JOIN Course t2
ON t1.CId = t2.CId
GROUP BY t1.CId,t2.Cname;
Calculated for each block final score,显示出来
SELECT t1.CId
,t2.Cname
,CONCAT(ROUND(SUM(IF(score<=100 AND score>85,1,0)),2),“%”) AS ‘[100-85)’
,CONCAT(ROUND(SUM(IF(score<=85 AND score>70,1,0)),2),“%”) AS ‘[85-70)’
,CONCAT(ROUND(SUM(IF(score<=70 AND score>60,1,0)),2),“%”) AS ‘[70-60)’
,CONCAT(ROUND(SUM(IF(score<=60 AND score>0,1,0)),2),“%”) AS ‘[60-0)’
FROM
SC t1 JOIN Course t2
ON t1.CId = t2.CId
GROUP BY t1.CId,t2.Cname;
23.查询各科成绩前三名的记录
分析:
1、First of all find out two subjects and sorting
2、Then the table as at1表定义@i控制排序,@q、@pRepetitive control keep vacancy
3、The final table astt1表与Student表tt2Correlation to remove the record of the top three results in all the subjects
First of all find out two subjects and sorting
SELECT SId
,CId
,score
FROM SC
ORDER BY CId,score DESC;
Then the table as at1表定义@i控制排序,@q、@pRepetitive control keep vacancy
SET @i := 0;
SET @p := 0;
SET @q := 0;
SELECT t1.SId
,t1.CId
,t1.score
,@p := t1.CId
,if(@[email protected],@i := @i + 1,@i :=1) as rn
,@q := @p
FROM (
SELECT SId
,CId
,score
FROM SC
ORDER BY CId,score DESC
) t1
The final table astt1表与Student表tt2Correlation to remove the record of the top three results in all the subjects
SET @i := 0;
SET @p := 0;
SET @q := 0;
SELECT tt1.SId
,tt2.Sname
,tt1.CId
,tt1.score
,tt1.rn
FROM(
SELECT t1.SId
,t1.CId
,t1.score
,@p := t1.CId
,if(@[email protected],@i := @i + 1,@i :=1) as rn
,@q := @p
FROM (
SELECT SId
,CId
,score
FROM SC
ORDER BY CId,score DESC
) t1) tt1 JOIN Student tt2
ON tt1.rn<=3 AND tt1.SId = tt2.SId
ORDER BY tt1.CId,rn;
24.查询每门课程被选修的学生数
分析:
以CIdGrouping query out each course is the number of students who take
以CIdGrouping query out each course is the number of students who take
SELECT CId
,COUNT(CId)
AS cnt
FROM SC
GROUP BY CId;
25.查询出只选修两门课程的学生学号和姓名
分析:
1、First query of students of two courses have courses and student id
2、Then the table as at1表与Studen表t2Associated to take out the student's name,找出结果
First query of students of two courses have courses and student id
SELECT SId
,COUNT(CId) AS cnt
FROM SC
GROUP BY SId
HAVING cnt = 2;
Then the table as at1表与Studen表t2Associated to take out the student's name,找出结果
SELECT t1.SId
,t2.Sname
FROM(
SELECT SId
,COUNT(CId) AS cnt
FROM SC
GROUP BY SId
HAVING cnt = 2
) t1 JOIN Student t2
ON t1.SId = t2.SId;
26.查询男生、女生人数
分析:
Gender group respectively for the boys、女生的人数
Gender group respectively for the boys、女生的人数
SELECT Ssex
,COUNT(1)
FROM Student
GROUP BY Ssex;
27.查询名字中含有「风」字的学生信息
分析:
使用 LIKE Fuzzy query name contains「风」字的学生信息
使用 LIKE Fuzzy query name contains「风」字的学生信息
SELECT * FROM Student WHERE Sname LIKE ‘%风%’;
Through observation found that there is no name phoenix word student information is contained in
28.查询同名同性学生名单,并统计同名同性人数
分析:
To the student's name,性别分组,然后countThe number of the same name gay
To the student's name,性别分组,然后countThe number of the same name gay
SELECT Sname
,Ssex
,COUNT(1)
AS cnt
FROM Student
GROUP BY Sname,Ssex
HAVING cnt > 1;
29.查询 1990 年出生的学生名单
分析:该题有两种解法
1、使用LIKE模糊查询
2、使用BETWEEN…AND…查询
使用LIKE模糊查询
SELECT * FROM Student
WHERE Sage
LIKE ‘1990%’;
使用BETWEEN…AND…查询
SELECT * FROM Student
WHERE Sage
BETWEEN ‘1990-1-1’ AND ‘1990-12-31’;
30.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
分析:
1、将SC表与CourseAssociated query table do scored an average of each course
2、Finally according to the above-mentioned will average descending order,平均成绩相同时,按课程编号升序排列
将SC表与CourseAssociated query table do scored an average of each course
SELECT SC.CId
,Course.Cname
,AVG(SC.score) AS avg_score
FROM SC
JOIN Course
ON SC.CId = Course.CId
GROUP BY SC.CId,Course.Cname;
Finally according to the above-mentioned will average descending order,平均成绩相同时,按课程编号升序排列
SELECT SC.CId
,Course.Cname
,AVG(SC.score) AS avg_score
FROM SC
JOIN Course
ON SC.CId = Course.CId
GROUP BY SC.CId,Course.Cname
ORDER BY avg_score DESC,SC.CId;
31.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
分析:
1、First query result is greater than or equal to85The average grades of course information
2、Then the table as at1表与Studen表t2Link to take out the student information
First query result is greater than or equal to85The average grades of course information
SELECT SId
,ROUND(AVG(score),2) AS avg_score
FROM SC
GROUP BY SId
HAVING avg_score>=85;
Then the table as at1表与Studen表t2Link to take out the student information
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 JOIN Student t2
ON t1.SId = t2.SId;
32.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
分析:
1、First of all query a course called mathCId
2、Then the above-mentioned as word table query a course called mathematics,且分数低于60的课程信息
3、Finally the table as tablet1与Student表t2Link to take out the student information
First of all query a course called mathCId
SELECT CId
FROM Course
WHERE CName = ‘数学’;
Then the above-mentioned as word table query a course called mathematics,且分数低于60的课程信息
SELECT SId
,Score
FROM SC
WHERE CId =(
SELECT CId
FROM Course
WHERE CName = ‘数学’
)AND score<60;
Finally the table as tablet1与Student表t2Link to take out the student information
SELECT t2.Sname
,t1.Score
FROM(
SELECT SId
,Score
FROM SC
WHERE CId =(
SELECT CId
FROM Course
WHERE CName = ‘数学’
)AND score<60
) t1 JOIN Student t2
ON t1.SId = t2.SId;
33.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
分析:
1、First query out all course grades,并以SId分组(存在学生没成绩,The situation of the course for0分)
2、然后将Student表t1With the above as a tablet2做左连接,取出学生姓名
First query out all course grades,并以SId分组(存在学生没成绩,The situation of the course for0分)
SELECT SId
,SUM(CASE CId WHEN ‘01’ THEN score ELSE 0 END) AS ‘语文成绩’
,SUM(CASE CId WHEN ‘02’ THEN score ELSE 0 END) AS ‘数学成绩’
,SUM(CASE CId WHEN ‘03’ THEN score ELSE 0 END) AS ‘英语成绩’
FROM SC
GROUP BY SId;
然后将Student表t1With the above as a tablet2做左连接,取出学生姓名
SELECT t1.Sname
,t2.语文成绩
,t2.数学成绩
,t2.英语成绩
FROM
Student t1 LEFT JOIN (
SELECT SId
,SUM(CASE CId WHEN ‘01’ THEN score ELSE 0 END) AS ‘语文成绩’
,SUM(CASE CId WHEN ‘02’ THEN score ELSE 0 END) AS ‘数学成绩’
,SUM(CASE CId WHEN ‘03’ THEN score ELSE 0 END) AS ‘英语成绩’
FROM SC
GROUP BY SId
) t2
ON t1.SId = t2.SId;
34.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
分析:
1、First query the result in70分以上的课程信息
2、Then the table as at1表与Student表t2关联,再与Course表t3Be associated remove any course grades in 70 分以上的姓名、Course name and score information
First query the result in70分以上的课程信息
SELECT SId
,CId
,score
FROM SC
WHERE score>70;
Then the table as at1表与Student表t2关联,再与Course表t3Be associated remove any course grades in 70 分以上的姓名、Course name and score information
SELECT t2.Sname
,t3.Cname
,t1.Score
FROM(
SELECT SId
,CId
,score
FROM SC
WHERE score>70
) t1 JOIN Student t2
ON t1.SId = t2. SId
JOIN Course t3
ON t1.CId = t3.CId;
35.查询不及格的课程
分析:
1、First query the results below60分的课程信息
2、Then the table as a tablet1与Student表t2表做连接,再与Course表t3做连接,Failed to find students、课程编号、课程名称、成绩
First query the results below60分的课程信息
SELECT DISTINCT CId
,score
FROM SC
WHERE score < 60;
*Then the table as a tablet1与Student表t2表做连接,再与Course表t3做连接,Failed to find students、课程编号、课程名称、成绩
SELECT DISTINCT
t2.Sname
,t1.CId
,t3.CName
,t1.score
FROM(
SELECT SId
,CId
,score
FROM SC
WHERE score<60
) t1 JOIN Student t2
ON t1.SId = t2.SId
JOIN Course t3
ON t1.CId = t3.CId;
36.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名
分析:
1、The query out the Numbers for the course01且课程成绩在80分以上的课程信息
2、Then the table as a tablet1与Student表t2做关联,Take out the student's name and student id
The query out the Numbers for the course01且课程成绩在80分以上的课程信息
SELECT SId
,score
FROM SC
WHERE CId = ‘01’
AND score >= 80;
Then the table as a tablet1与Student表t2做关联,Take out the student's name and student id
SELECT t1.SId
,t2.Sname
FROM(
SELECT SId
,score
FROM SC
WHERE CId = ‘01’
AND score >= 80
) t1 JOIN Student t2
ON t1.SId = t2.SId;
37.求每门课程的学生人数
将SC表t1与Course表t2做关联,并按CId分组,Take out the course number、课程名称、每门课程
SELECT t1.CId
,t2.cname
,COUNT(t1.CId) AS ‘人数’
FROM SC t1 JOIN Course t2
ON t1.CId = t2.CId
GROUP BY t1.CId,t2.Cname;
38.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
分析:
1、First query zhang teacher taught coursesTId
2、然后在CourseIn the table number query zhang teacher taught coursesCId
3、再在SC表查询,Grades don't repeat course information
4、And then will result descending order take out the first resultLIMIT 1,Is the highest grade
5、The final table ast1表与Student表t2连接,Remove the top student information
First query zhang teacher taught coursesTId
SELECT TId
FROM Teacher
WHERE Tname = ‘张三’;
然后在CourseIn the table number query zhang teacher taught coursesCId
SELECT CId
FROM Course
WHERE TId = (
SELECT TId
FROM Teacher
WHERE Tname = ‘张三’
);
再在SC表查询,Grades don't repeat course information
SELECT SId
,CId
,score
FROM SC
WHERE CId IN (
SELECT CId
FROM Course
WHERE TId = (
SELECT TId
FROM Teacher
WHERE Tname = ‘张三’
)
);
And then will result descending order take out the first resultLIMIT 1,Is the highest grade
SELECT SId
,CId
,score
FROM SC
WHERE CId IN (
SELECT CId
FROM Course
WHERE TId = (
SELECT TId
FROM Teacher
WHERE Tname = ‘张三’
)
) ORDER BY score
LIMIT 1;
The final table ast1表与Student表t2连接,Remove the top student information
SELECT t1.SId
,t2.Sname
,t1.CId
,t1.score
FROM(
SELECT SId
,CId
,score
FROM SC
WHERE CId IN (
SELECT CId
FROM Course
WHERE TId = (
SELECT TId
FROM Teacher
WHERE Tname = ‘张三’
)
) ORDER BY score
LIMIT 1) t1 JOIN Student t2
ON t1.SId = t2.SId;
39.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
分析:
1、First query zhang teacher taught coursesTId
2、然后在CourseIn the table number query zhang teacher taught coursesCId
3、再在SC表查询,Grades don't repeat course information,并降序排列
4、The table as a tablet1再定义@i、@p、@q三个变量@i控制排序,@p、@qControl scores repeat reserved place
5、Then the table as att1表与Student表tt2做连接,Take out the highest score,Information and students
First query zhang teacher taught coursesTId
SELECT TId
FROM Teacher
WHERE Tname = ‘张三’;
然后在CourseIn the table number query zhang teacher taught coursesCId
SELECT CId
FROM Course
WHERE TId = (
SELECT TId
FROM Teacher
WHERE Tname = ‘张三’
);
再在SC表查询,Grades don't repeat course information,并降序排列
SELECT SId
,CId
,score
FROM SC
WHERE CId IN (
SELECT CId
FROM Course
WHERE TId = (
SELECT TId
FROM Teacher
WHERE Tname = ‘张三’
)
) ORDER BY score DESC;
The table as a tablet1再定义@i、@p、@q三个变量@i控制排序,@p、@qControl scores repeat reserved place
SET @i := 0;
SET @p := 0;
SET @q := 0;
SELECT t1.SId
,t1.CId
,t1.score
,@p := t1.score
,IF(@[email protected],@i,@i := @i+1) AS dense_rank
,@q :[email protected]
FROM (
SELECT SId
,CId
,score
FROM SC
WHERE CId IN
(
SELECT CId
FROM Course
WHERE TId = (
SELECT TId
FROM Teacher
WHERE Tname = ‘张三’
)
) ORDER BY score DESC) t1;
Then the table as att1表与Student表tt2做连接,Take out the highest score,Information and students
SET @i := 0;
SET @p := 0;
SET @q := 0;
SELECT tt2.Sname
,tt1.SId
,tt1.CId
,tt1.score
FROM(
SELECT t1.SId
,t1.CId
,t1.score
,@p := t1.score
,IF(@[email protected],@i,@i := @i+1) AS dense_rank
,@q :[email protected]
FROM (
SELECT SId
,CId
,score
FROM SC
WHERE CId IN
(
SELECT CId
FROM Course
WHERE TId = (
SELECT TId
FROM Teacher
WHERE Tname = ‘张三’
)
) ORDER BY score DESC) t1
) tt1 JOIN Student tt2
ON tt1.dense_rank = 1
AND tt1.SId = tt2.SId;
40.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
分析:
1、首先现将SC表t1与自己SC表t2进行关联,Take out the result of the same student number、课程编号、学生成绩
2、然后再与Student表t3Correlation to take out the student information
首先现将SC表t1与自己SC表t2进行关联,Take out the result of the same student number、课程编号、学生成绩
SELECT t1.CId
,t2.score
FROM SC t1 JOIN SC t2
ON t1.score = t2.score
AND t1.CId != t2.CId;
然后再与Student表t3Correlation to take out the student information
SELECT t3.Sname
,t1.CId
,t2.score
FROM SC t1 JOIN SC t2
ON t1.score = t2.score
AND t1.CId != t2.CId
JOIN Student t3
ON t1.SId = t3.SId;
41.查询每门课程成绩最好的前两名
分析:
1、首先在SCQuery the grades for each course in the table and pressCId分数降序排列
2、定义函数@i、@p、@q,According to the results obtained by each course sorting,And will the above-mentioned ast1表,然后再作为tt1表与Student表tt2To connect to take out the best top two each course grade
首先在SCQuery the grades for each course in the table and pressCId分数降序排列
SELECT SId
,CId
,score
FROM SC
ORDER BY CId,score DESC;
定义函数@i、@p、@q,According to the results obtained by each course sorting,And will the above-mentioned ast1表,然后再作为tt1表与Student表tt2To connect to take out the best top two each course grade
SET @i := 0;
SET @p := 0;
SET @q := 0;
SELECT tt1.SId
,tt2.SName
,tt1.CId
,tt1.score
,tt1.rn
FROM (
SELECT t1.SId
,t1.CId
,t1.score
,@p := t1.CId
,IF(@[email protected],@i := @i + 1,@i :=1) AS rn
,@q := @p
FROM (
SELECT SId
,CId
,score
FROM SC
ORDER BY CId,score DESC
) t1
) tt1 JOIN Student tt2 ON tt1.rn<=2 AND tt1.SId = tt2.SId
ORDER BY tt1.CId,rn;
42.统计每门课程的学生选修人数(超过 5 人的课程才统计).
分析:
1、以CId进行分组,统计出超过5人的课程信息
以CId进行分组,统计出超过5人的课程信息
SELECT CId
,COUNT(SId) AS ‘选课人数’
FROM SC
GROUP BY CId
HAVING 选课人数 > 5;
43.检索至少选修两门课程的学生学号
分析:
1、以SId进行分组,Statistics of at least two courses students student id
以SId进行分组,Statistics of at least two courses students student id
SELECT SId
,COUNT(CID) AS ‘选课数量’
FROM SC
GROUP BY SId
HAVING 选课数量 >= 2;
44.查询选修了全部课程的学生信息
分析:
1、First query the total course attributes as tablet3
2、然后将Student表t1与SC表t2进行关联,以SId、Sname进行分组,用表t3Selected students enrolled in the whole course information
First query the total course attributes as tablet3
SELECT COUNT(t3.CId)
FROM Course t3;
然后将Student表t1与SC表t2进行关联,以SId、Sname进行分组,用表t3Selected students enrolled in the whole course information
SELECT t1.SId
,t1.Sname
,COUNT(t2.CId) AS ‘选课数量’
FROM Student t1 JOIN SC t2
ON t2.SId = t1.SId
GROUP BY t1.SId,t1.Sname
HAVING COUNT(t2.CId) = (
SELECT COUNT(t3.CId)
FROM Course t3
);
45.查询各学生的年龄,只按年份来算
分析:Get the current date minus the students date of birth
SELECT YEAR(now()) - date_format(Sage,'%Y') FROM Student;
46.按照出生日期来算,当前月日 < 出生年月的月日,则年龄减一
分析:
1、首先先在StudentQuery the date of birth in the table、当前月日、出生年月的月日
2、Then the table as a tablet1,Calculate the current date < 出生年月的月日,则年龄减一
首先先在StudentQuery the date of birth in the table、当前月日、出生年月的月日
SELECT SId
,Sname
,(YEAR(now()) - date_format(Sage,‘%Y’) ) AS age
,date_format(Sage,‘%m-%d’) AS month_day
,date_format(now(),‘%m-%d’) AS now_month_day
FROM Student;
Then the table as a tablet1,Calculate the current date < 出生年月的月日,则年龄减一
SELECT SId
,SName
,CASE WHEN now_month_day<month_day THEN age-1 ELSE age END AS new_age
,age
FROM(
SELECT SId
,Sname
,(YEAR(now()) - date_format(Sage,‘%Y’) ) AS age
,date_format(Sage,‘%m-%d’) AS month_day
,date_format(now(),‘%m-%d’) AS now_month_day
FROM Student
) t1;
47.查询本周过生日的学生
分析:Query current weeks and weeks in the table are equal to
SELECT SId
,SName
,Sage
,WEEK(Sage)
FROM Student
WHERE WEEK(Sage) = WEEK(now());
Nobody's birthday this week so there is no data
48.查询下周过生日的学生
分析:Query the current week+1And in the table weeks whether is equal to
SELECT SId
,SName
,Sage
,WEEK(Sage)
FROM Student
WHERE WEEK(Sage) = WEEK(date_add(now(),INTERVAL 1 WEEK));
Nobody's birthday next week so there is no data
49.查询本月过生日的学生
分析:Query whether the current month and the month in the table number is equal to
SELECT SId
,SName
,Sage
,MONTH(Sage)
FROM Student
WHERE MONTH(Sage) = MONTH(now());
50.查询下月过生日的学生
分析:Query the current month+1With the number in the table are equal to
SELECT SId
,SName
,Sage
,MONTH(Sage)
FROM Student
WHERE MONTH(Sage) = MONTH(date_add(now(),INTERVAL 1 MONTH));
Nobody in January birthday so there is no data
week()Function to refer to the following url
https://blog.csdn.net/moakun/article/details/82528773
date_format()Function to refer to the following url
https://www.w3school.com.cn/sql/func_date_format.asp
**终于到底啦!This is a cut in the finest the most detailed analytical,编了5w5q字,Handsome is not easy,关注一下吧!**??
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在.深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小.自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前.因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担.添加下方名片,即可获取全套学习资料哦
边栏推荐
- 秒杀系统小demo
- Nodejs安装教程
- MySql 5.7.38下载安装教程 ,并实现在Navicat操作MySql
- 国际顶会OSDI首度收录淘宝系统论文,端云协同智能获大会主旨演讲推荐
- Leading the demand and justifying the HR value - the successful launch of the "Human Resource Leading Model HRLM"
- Launch Space on-premises deployment (local) Beta!
- Node的安装与环境变量的配置
- MySQL Advanced SQL Statements
- 虚拟现实房产展示系统提前预见未来装修效果
- MySQL 5.7 安装教程(全步骤、保姆级教程)
猜你喜欢
How to install the specified version package with NPM and view the version number
使用jOOQ 3.14合成外键在视图上写隐式连接
Nacos安装详细过程
The stock price has repeatedly hit new lows, and the real estate SaaS giant is in trouble. How should Mingyuan Cloud transform and save itself?
nacos安装配置和单机部署教程
View source and switch mirrors in two ways: npm and nrm
Thread Basics (1)
APT + Transform 实现多模块应用Application生命周期分发
MySQL(3)
Redis(十二) - Redis消息队列
随机推荐
pytorch常用函数
BGP+MPLS综合实验
Deep learning - CNN realizes the recognition of MNIST handwritten digits
[Cartoon] 2021 full score programmer behavior comparison table (latest version)
Not annotated parameter overrides @NonNullApi parameter
人工神经网络
Practice on optimizing startup performance of VS Code
Difference between npm and yarn
秒杀系统小demo
直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
MySQL高级学习笔记
双重for循环案例(用js打印九九乘法表)
金蝶国际:半年亏掉去年一年,疯狂烧钱的商业模式如何持续
触发器简单解释
NPM ---- 安装yarn
BGP实验(路由反射器,联邦,路由优化)
驱动页面性能优化的3个有效策略
Write implicit join on view with jOOQ 3.14 synthetic foreign key
回文串求解的进阶方法
Nacos客户端启动出现9848端口错误分析(非版本升级问题)