title: SQL练习
time: 2019年8月16日16:09:54
tags: SQL
SQL 练习
- 查询至少有一门课与学号为“01”的学生所学课程相同的学生的学号和姓名(重点)
SELECT
s_id,
s_name
FROM
student
WHERE
s_id IN (
SELECT DISTINCT
s_id
FROM
score
WHERE
c_id IN ( SELECT c_id FROM score WHERE s_id = '01' )
AND s_id != '01'
)
- 查询和“01”号同学所学课程完全相同的其他同学的学号(重点)
SELECT
s_id
FROM
score
GROUP BY
s_id
HAVING
count( c_id ) = ( SELECT count( c_id ) FROM score WHERE s_id = '01' )
AND s_id != '01'
- 查询没学过"张三"老师讲授的任一门课程的学生姓名(重点,能做出来)
-- 查询没学过"张三"老师讲授的任一门课程的学生姓名 和47题一样(重点,能做出来)
SELECT
s_name
FROM
student
WHERE
s_id NOT IN (
SELECT
s_id
FROM
score AS s
INNER JOIN course AS c ON c.c_id = s.c_id
INNER JOIN teacher AS t ON t.t_id = c.t_id
WHERE
t_name = '张三'
)
- 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩(重点)
SELECT
s.s_id,
s.s_name,
a.avg
FROM
student AS s
INNER JOIN ( SELECT s_id, avg( s_score ) AS avg FROM score WHERE s_score < 60 GROUP BY s_id HAVING count( c_id ) >= 2 ) AS a ON s.s_id = a.s_id
- 检索"01"课程分数小于60,按分数降序排列的学生信息
SELECT
s.s_id,
s.s_name,
s