在数据库管理系统中,MySQL是最受欢迎的关系型数据库之一。在 MySQL 的面试中,了解常见的问题和解决方法是非常重要的。本文将介绍15道常见的MySQL面试题,并提供详细的解析和答案。
正文:
SELECT
s.*,
s1.score a,
s2.score b
FROM
( SELECT * FROM t_mysql_score WHERE cid = "01" ) s1,
( SELECT * FROM t_mysql_score WHERE cid = "02" ) s2,
t_mysql_student s
WHERE
s1.sid = s2.sid
AND s1.sid = s.sid
AND s1.score > s2.score
s.*,
( CASE WHEN s1.cid = "01" THEN s1.score END ) a,
( CASE WHEN s2.cid = "02" THEN s1.score END ) b
FROM
( SELECT * FROM t_mysql_score WHERE cid = "01" ) s1,
( SELECT * FROM t_mysql_score WHERE cid = "02" ) s2,
t_mysql_student s
WHERE
s1.sid = s2.sid
AND s1.sid = s.sid
SELECT
s1.*,
s2.score
FROM
( SELECT * FROM t_mysql_score sc WHERE sc.cid = "01" ) s1
LEFT JOIN ( SELECT * FROM t_mysql_score sc WHERE sc.cid = "02" ) s2 ON s1.sid = s2.sid
SELECT
*
FROM
t_mysql_score s
WHERE
s.sid NOT IN ( SELECT sid FROM t_mysql_score WHERE cid = "01" )
AND s.cid = "02"
SELECT
s.sid,
s.sname,
avg( c.score )
FROM
t_mysql_student s,
t_mysql_score c
WHERE
s.sid = c.sid
GROUP BY
s.sid,
s.sname
HAVING
avg( c.score ) >= 60
SELECT
s.*
FROM
t_mysql_score sc,
t_mysql_student s
WHERE
sc.sid = s.sid
GROUP BY
s.sid
SELECT
s.sid,
s.sname,
count( sc.cid ),
sum( sc.score )
FROM
t_mysql_student s,
t_mysql_score sc
WHERE
s.sid = sc.sid
GROUP BY
s.sid,
s.sname
SELECT
*
FROM
t_mysql_teacher t
WHERE
tname LIKE '李%'
SELECT
*
FROM
t_mysql_student
WHERE
sid IN (
SELECT
sc.sid
FROM
t_mysql_teacher t,
t_mysql_score sc,
t_mysql_course c
WHERE
t.tid = c.tid
AND sc.cid = c.cid
AND t.tname = "张三"
GROUP BY
sc.sid
)
SELECT
s.*,
count( sc.score )
FROM
t_mysql_student s,
t_mysql_score sc
WHERE
s.sid = sc.sid
GROUP BY
s.sid,
s.sname
HAVING
count( sc.score ) < ( SELECT count( 1 ) FROM t_mysql_course )
SELECT
sname
FROM
t_mysql_student
WHERE
sid NOT IN (
SELECT
sc.sid
FROM
t_mysql_score sc,
t_mysql_course c,
t_mysql_teacher t
WHERE
t.tid = c.tid
AND sc.cid = c.cid
AND t.tname = "张三"
)
SELECT
s.sid,
s.sname,
ROUND( AVG( sc.score ), 2 )
FROM
t_mysql_student s,
t_mysql_score sc
WHERE
sc.sid = s.sid
AND sc.score < 60 GROUP BY s.sid, s.sname HAVING count( sc.score ) >=2
SELECT
s.*,
sc.score
FROM
t_mysql_student s,
t_mysql_score sc
WHERE
s.sid = sc.sid
AND sc.cid = "01"
AND sc.score < 60
ORDER BY
sc.score DESC
SELECT
s.sid,
s.sname,
ROUND(AVG(sc.score),2),
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_student s,
t_mysql_score sc
WHERE
s.sid = sc.sid
GROUP BY
s.sname,
s.sid
ORDER BY
AVG( sc.score ) DESC
以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT
c.cid,
c.cname,
max( sc.score ) 最高分,
min( sc.score ) 最低分,
ROUND( avg( sc.score ), 2 ) 平均分,
CONCAT(ROUND(sum( IF ( sc.score > 60, 1, 0 ))/count(sc.score)*100,2) ,'%') 及格率,
CONCAT(ROUND(sum( IF ( sc.score >= 70 and sc.score<80, 1, 0 ))/count(sc.score)*100,2) ,'%') 中等率,
CONCAT(ROUND(sum( IF ( sc.score >= 70 and sc.score<80, 1, 0 ))/count(sc.score)*100,2) ,'%') 优良率,
CONCAT(ROUND(sum( IF ( sc.score >= 90 , 1, 0 ))/count(sc.score)*100,2) ,'%') 优秀
FROM
t_mysql_student s,
t_mysql_score sc,
t_mysql_course c
WHERE
s.sid = sc.sid
AND c.cid = sc.cid
GROUP BY
c.cid,
c.cname
通过本文,我们回顾了面试中常见的15道MySQL查询语句面试题,并提供了每个问题的简洁解答。在准备MySQL面试时,了解这些问题并熟练运用相关语句将为您赢得更多竞争优势。愿这篇博客对您的MySQL学习和面试准备有所帮助!