Foreword: MySQL is a popular open source relational database management system (RDBMS) originally developed by the Swedish company MySQL AB. It is a lightweight, fast and easy-to-use database system suitable for applications of all sizes.
history:
- MySQL was originally developed in 1994 by Michael Widenius and David Axmark and first released in 1995.
- In subsequent developments, MySQL has gained extensive user base and community support, becoming one of the most popular open source databases.
- In 2008, Sun Microsystems acquired MySQL AB. Subsequently, Sun was acquired by Oracle, making MySQL a product of Oracle Corporation.
- MySQL has gone through multiple version iterations during its development process, continuously enhancing functions and improving performance.
MySQL has become the preferred database system for many web applications, enterprise applications and small applications due to its ease of use, reliability and scalability. It is widely used in web development, data analysis, e-commerce and many other fields. MySQL's continued development and community support have made it one of the important players in the field of database management systems.
01) Query the information and course scores of students whose course scores are higher than "01" course than "02"
Involved tables: t_mysql_student, t_mysql_score
Connection method: inner connection
Row to column: flow control function
SELECT
s.*,
( CASE WHEN t1.cid = '01' THEN t1.score END ) 语文,
( CASE WHEN t2.cid = '02' THEN t2.score END ) 数学
FROM
t_mysql_student s,
( SELECT * FROM t_mysql_score WHERE cid = '01' ) t1,
( SELECT * FROM t_mysql_score WHERE cid = '02' ) t2
WHERE
s.sid = t1.sid
AND t1.sid = t2.sid
AND t1.score > t2.score
02) Query the situation where "01" course and "02" course exist at the same time
Involved tables: t_mysql_student, t_mysql_score
Connection method: inner connection
Row to column: flow control function
SELECT
s.*,
( CASE WHEN t1.cid = '01' THEN t1.score END ) 语文,
( CASE WHEN t2.cid = '02' THEN t2.score END ) 数学
FROM
t_mysql_student s,
( SELECT * FROM t_mysql_score WHERE cid = '01' ) t1,
( SELECT * FROM t_mysql_score WHERE cid = '02' ) t2
WHERE
s.sid = t1.sid
AND t1.sid = t2.sid
03) Query the situation where the "01" course exists but the "02" course may not exist (it will be displayed as null if it does not exist)
Involved tables: t_mysql_student, t_mysql_score
Connection method: inner connection, outer connection
Row to column: flow control function
SELECT
s.*,
( CASE WHEN t1.cid = '01' THEN t1.score END ) 语文,
( CASE WHEN t2.cid = '02' THEN t2.score END ) 数学
FROM
t_mysql_student s
INNER JOIN ( SELECT * FROM t_mysql_score WHERE cid = '01' ) t1 ON s.sid = t1.sid
LEFT JOIN ( SELECT * FROM t_mysql_score WHERE cid = '02' ) t2 ON t1.sid = t2.sid
04) Query the situation where "01" course does not exist but "02" course exists
Involved tables: t_mysql_student, t_mysql_score
Query method: subquery
Row to column: flow control function
SELECT
s.*,
( CASE WHEN sc.cid = '01' THEN sc.score END ) 语文,
( CASE WHEN sc.cid = '02' THEN sc.score END ) 数学
FROM
t_mysql_student s,
t_mysql_score sc
WHERE
s.sid = sc.sid
AND s.sid NOT IN ( SELECT sid FROM t_mysql_score WHERE cid = '01' )
AND sc.cid = '02'
05) Query the student number, student name and average score of students whose average score is greater than or equal to 60 points
Involved tables: t_mysql_student, t_mysql_score
Query method: outer connection
Aggregation functions: rounding ( round() ), average ( avg() )
Having aggregate functions means grouping (GROUP BY)
SELECT
s.sid,
s.sname,
round( avg( sc.score ), 2 ) 平均数
FROM
t_mysql_student s
LEFT JOIN t_mysql_score sc ON s.sid = sc.sid
GROUP BY
s.sid,
s.sname
HAVING
平均数 >= 60
06) Query student information with scores in the t_mysql_score table
Involved tables: t_mysql_student, t_mysql_score
Query method: inner connection
Finally, it is recommended to group once, otherwise there will be too much data.
SELECT
s.sid,
s.sname
FROM
t_mysql_student s
LEFT JOIN t_mysql_score sc ON s.sid = sc.sid
GROUP BY
s.sid,
s.sname
07) Query the student number, student name, total number of courses selected, and total grades of all courses (those without grades are displayed as null)
Tables involved: t_mysql_student, t_mysql_score
Query method: outer join
aggregation function: count (count()), summation (sum())
There are aggregate functions that mean grouping (GROUP BY)
SELECT
s.sid,
s.sname,
count( sc.score ) 选课总数,
sum( sc.score ) 总成绩
FROM
t_mysql_student s
LEFT JOIN t_mysql_score sc ON s.sid = sc.sid
GROUP BY
s.sid,
s.sname
08) Query the number of teachers with the surname "Li"
Assessment: Aggregation Function》Total number. use of like
select count(*) from t_mysql_teacher where tname like '李%'
09) Query the information of students who have studied with teacher "Zhang San"
SELECT
s.*,c.cname,t.tname
FROM
t_mysql_teacher t,t_mysql_student s,t_mysql_course c,t_mysql_score sc
WHERE
t.tid=c.tid and c.cid=sc.cid and sc.sid=s.sid and tname = '张三'
10) Query the information of students who have not completed all courses
SELECT
s.sid,s.sname,count(sc.score) n
FROM
t_mysql_score sc,t_mysql_student s
WHERE
sc.sid=s.sid
GROUP BY
s.sid,s.sname
HAVING
n<(select count(c.cid) from t_mysql_course c )
11) Query the names of students who have not studied any course taught by "Zhang San"
select s.* from t_mysql_student s where s.sid not in(
SELECT
sc.sid
FROM
t_mysql_teacher t,t_mysql_course c,t_mysql_score sc
WHERE
t.tid=c.tid and c.cid=sc.cid and t.tname='张三'
GROUP BY
sc.sid)
12) Query the student ID number, name and average grade of students who failed two or more courses.
SELECT
s.sid,s.sname,ROUND(AVG(sc.score)) 平均成绩,COUNT(sc.cid) n
FROM
t_mysql_student s,t_mysql_score sc
WHERE
s.sid=sc.sid and sc.score<60
GROUP BY
s.sid,s.sname
HAVING
n>=2
13) Retrieve "01" course scores less than 60, student information arranged in descending order of scores
SELECT
s.*
FROM
t_mysql_score sc,t_mysql_student s
WHERE
sc.sid=s.sid and sc.score<60 and cid='01'
ORDER BY sc.score DESC
14) Display the grades of all students in all courses and the average grade from high to low
SELECT
s.sid,s.sname,round(AVG(sc.score),2) avgNum ,
max(case when sc.cid='01' then sc.score end)语文,
max(case when sc.cid='02' then sc.score end)数学,
max(case when sc.cid='03' then sc.score end)英语
FROM
t_mysql_score sc,t_mysql_student s,t_mysql_course c
WHERE
sc.sid=s.sid and sc.cid=c.cid
GROUP BY
s.sid,s.sname
ORDER BY avgNum desc
SELECT
s.sid,s.sname,round(AVG(sc.score),2) avgNum ,
max(if(sc.cid='01',sc.score,0))语文,
max(if(sc.cid='02',sc.score,0))数学,
max(if(sc.cid='03',sc.score,0))英语
FROM
t_mysql_score sc,t_mysql_student s,t_mysql_course c
WHERE
sc.sid=s.sid and sc.cid=c.cid
GROUP BY
s.sid,s.sname
ORDER BY avgNum desc
15) Query the highest, lowest and average scores of each subject:
displayed in the following form: course ID, course name, highest score, lowest score, average score, passing rate, average rate, excellent rate, excellent passing rate >= 60, medium: 70-80, excellent: 80-90, excellent: >=90.
It is required to output the course number and the number of elective students. The query results are sorted in descending order by the number of people. If the number of people is the same, they are sorted in ascending order by the course number.
SELECT
c.cid,c.cname,max(sc.score)最高分,
min(sc.score)最低分,
ROUND(AVG(sc.score))平均分,
count(sc.score)选修人数,
CONCAT(ROUND(sum(if(sc.score>=60,1,0))/count(sc.score)*100),'%')及格率,
CONCAT(ROUND(sum(if(sc.score>=70 and sc.score<80,1,0))/count(sc.score)*100),'%')中等率,
CONCAT(ROUND(sum(if(sc.score>=80 and sc.score<90,1,0))/count(sc.score)*100),'%')优良率,
CONCAT(ROUND(sum(if(sc.score>=90,1,0))/count(sc.score)*100),'%')优秀率
FROM
t_mysql_score sc,t_mysql_course c,t_mysql_student s
WHERE
sc.sid=s.sid and sc.cid=c.cid
GROUP BY
c.cid,c.cname
ORDER BY
选修人数 desc,
Well, that’s all for sharing today! ! ! Hope this helps! ! !