数据库常见的面试题

发布时间:2024年01月08日

1.查询"01"课程比"02"课程成绩高的学生的信息及课程分数:

SELECT s.student_id, s.student_name, sc1.course_score AS score1, sc2.course_score AS score2
FROM student s
JOIN score sc1 ON s.student_id = sc1.student_id AND sc1.course_id = '01'
JOIN score sc2 ON s.student_id = sc2.student_id AND sc2.course_id = '02'
WHERE sc1.course_score > sc2.course_score;

2.查询同时存在"01"课程和"02"课程的情况:

SELECT DISTINCT s.student_id, s.student_name
FROM student s
JOIN score sc1 ON s.student_id = sc1.student_id AND sc1.course_id = '01'
JOIN score sc2 ON s.student_id = sc2.student_id AND sc2.course_id = '02';

3.查询存在"01"课程但可能不存在"02"课程的情况(不存在时显示为 null )?

SELECT s.student_id, s.student_name, COALESCE(sc2.course_score, NULL) AS score2
FROM student s
LEFT JOIN score sc2 ON s.student_id = sc2.student_id AND sc2.course_id = '02';

4.查询不存在"01"课程但存在"02"课程的情况:

SELECT s.student_id, s.student_name
FROM student s
LEFT JOIN score sc1 ON s.student_id = sc1.student_id AND sc1.course_id = '01'
WHERE sc1.course_score IS NULL;

5.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩:

SELECT student_id, student_name, AVG(course_score) AS average_score
FROM student s
JOIN score sc ON s.student_id = sc.student_id
GROUP BY student_id, student_name
HAVING average_score >= 60;

6.查询在t_mysql_score表存在成绩的学生信息:

SELECT DISTINCT s.student_id, s.student_name
FROM student s
JOIN score sc ON s.student_id = sc.student_id;

7.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ):

SELECT s.student_id, s.student_name, COUNT(DISTINCT sc.course_id) AS course_count, SUM(COALESCE(sc.course_score, 0)) AS total_score
FROM student s
LEFT JOIN score sc ON s.student_id = sc.student_id
GROUP BY s.student_id, s.student_name;

8.查询「李」姓老师的数量:

SELECT COUNT(*) AS count
FROM teacher t
WHERE t.teacher_name LIKE '李%';

?9.查询学过「张三」老师授课的同学的信息:

SELECT DISTINCT s.student_id, s.student_name
FROM student s
JOIN score sc ON s.student_id = sc.student_id
JOIN course c ON sc.course_id = c.course_id
JOIN teacher t ON c.teacher_id = t.teacher_id
WHERE t.teacher_name = '张三';

?10.查询没有学全所有课程的同学的信息

SELECT s.student_id, s.student_name
FROM student s
WHERE NOT EXISTS (
    SELECT * FROM course c
    WHERE NOT EXISTS (
        SELECT * FROM score sc WHERE sc.student_id = s.student_id AND sc.course_id = c.course_id
    )
);

11.查询没学过"张三"老师讲授的任一门课程的学生姓名:

SELECT s.student_name
FROM student s
WHERE NOT EXISTS (
    SELECT * FROM course c
    JOIN score sc ON c.course_id = sc.course_id
    JOIN teacher t ON c.teacher_id = t.teacher_id
    WHERE t.teacher_name = '张三' AND sc.student_id = s.student_id
);

12.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩:

SELECT s.student_id, s.student_name, AVG(sc.course_score) AS average_score
FROM student s
JOIN score sc ON s.student_id = sc.student_id
WHERE sc.course_score < 60
GROUP BY s.student_id, s.student_name
HAVING COUNT(sc.course_id) >= 2;

13.检索"01"课程分数小于 60,按分数降序排列的学生信息:

SELECT s.student_id, s.student_name, sc.course_score
FROM student s
JOIN score sc ON s.student_id = sc.student_id AND sc.course_id = '01'
WHERE sc.course_score < 60
ORDER BY sc.course_score DESC;

14.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩:

SELECT s.student_id, s.student_name, sc.course_id, sc.course_score, (SELECT AVG(course_score) FROM score WHERE student_id = s.student_id) AS average_score
FROM student s
JOIN score sc ON s.student_id = sc.student_id
ORDER BY average_score DESC;

15.查询各科成绩最高分、最低分和平均分:

SELECT course_id, MAX(course_score) AS highest_score, MIN(course_score) AS lowest_score, AVG(course_score) AS average_score,
       SUM(CASE WHEN course_score >= 60 THEN 1 ELSE 0 END) / COUNT(*) AS pass_rate,
       SUM(CASE WHEN course_score >= 70 AND course_score < 80 THEN 1 ELSE 0 END) / COUNT(*) AS medium_rate,
       SUM(CASE WHEN course_score >= 80 AND course_score < 90 THEN 1 ELSE 0 END) / COUNT(*) AS good_rate,
       SUM(CASE WHEN course_score >= 90 THEN 1 ELSE 0 END) / COUNT(*) AS excellent_rate
FROM score
GROUP BY course_id;
文章来源:https://blog.csdn.net/djssubddbj/article/details/135466641
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。