文章目录
- 面试题 (9-15)
面试题 (9-15)
09)查询学过「张三」老师授课的同学的信息
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)查询没有学全所有课程的同学的信息
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)查询没学过"张三"老师讲授的任一门课程的学生姓名
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)查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
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)检索" 01 "课程分数小于 60,按分数降序排列的学生信息
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)按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
SELECT
s.sid,
s.sname,
sum(
IF
( sc.cid = ‘01’, sc.score, 0 ))语文,
sum(
IF
( sc.cid = ‘02’, sc.score, 0 ))数学,
sum(
IF
( sc.cid = ‘03’, sc.score, 0 ))英语,
ROUND( avg( sc.score ), 2 )
FROM
t_mysql_score sc
RIGHT JOIN t_mysql_student s ON sc.sid = s.sid
GROUP BY
s.sid,
s.sname
15)查询各科成绩最高分、最低分和平均分:
SELECT
c.cid,
c.cname,
count( sc.sid ) 人数,
max( sc.score ) 最高分,
min( sc.score ) 最低分,
ROUND( avg( sc.score ), 2 ) 平均分,
CONCAT(
ROUND( sum( IF ( sc.score >= 90, 1, 0 ))/( SELECT count( 1 ) FROM t_mysql_student )* 100, 2 ),
‘%’
) 优秀率,
CONCAT(
ROUND(
sum(
IF
( sc.score >= 80 AND sc.score < 90, 1, 0 ))/(
SELECT
count( 1 )
FROM
t_mysql_student
)* 100,
2
),
‘%’
) 优良率,
CONCAT(
ROUND(
sum(
IF
( sc.score >= 70 AND sc.score < 80, 1, 0 ))/(
SELECT
count( 1 )
FROM
t_mysql_student
)* 100,
2
),
‘%’
) 中等率,
CONCAT(
ROUND( sum( IF ( sc.score >= 60, 1, 0 ))/( SELECT count( 1 ) FROM t_mysql_student )* 100, 2 ),
‘%’
) 及格率
FROM
t_mysql_score sc
LEFT JOIN t_mysql_course c ON sc.cid = c.cid
GROUP BY
c.cid,
c.cname