你必知必会的SQL面试题
写在前面的话
本文参考原博《走向面试之数据库基础:一、你必知必会的SQL语句练习-Part 1》和《走向面试之数据库基础:一、你必知必会的SQL语句练习-Part 2》进行练习,部分题目在不变化其练习目的的情况下进行了题意改动,并删除部分重复和无表的题目。
涉及的表结构和测试数据
Part 1 练习题和参考解
SELECT
s1.StudentNo,
s1.score AS '001',
s2.score AS '002'
FROM
score s1,
(
SELECT
*
FROM
score s
WHERE
s.CourseNo = 2
) s2
WHERE
s1.CourseNo = 1
AND
s1.StudentNo = s2.StudentNo
AND
s1.score < s2.score
ORDER BY s1.StudentNo
SELECT
s1.StudentNo,
s1.score AS '001',
s2.score AS '002'
FROM
score s1,
(
SELECT
*
FROM
score s
WHERE
s.CourseNo = 2
) s2
WHERE
s1.CourseNo = 1
AND
s1.StudentNo = s2.StudentNo
AND
s1.score < s2.score
ORDER BY s1.StudentNo
SELECT
s1.StudentNo,
s1.score AS '001',
s2.score AS '002'
FROM
score s1,
score s2
WHERE
s1.CourseNo = 001
AND
s2.CourseNo = 002
AND
s1.StudentNo = s2.StudentNo
AND
s1.score < s2.score
ORDER BY s1.StudentNo
SELECT
s1.StudentNo,
s1.score AS '001',
s2.score AS '002'
FROM
score s1,
score s2
WHERE
s1.CourseNo = 001
AND
s2.CourseNo = 002
AND
s1.StudentNo = s2.StudentNo
AND
s1.score < s2.score
ORDER BY s1.StudentNo
SELECT
s1.StudentNo,
AVG(s1.score)
FROM
score s1
GROUP BY s1.StudentNo
HAVING AVG(s1.score)>60
SELECT
s1.StudentNo,
AVG(s1.score)
FROM
score s1
GROUP BY s1.StudentNo
HAVING AVG(s1.score)>60
SELECT
s1.StudentNo,
stu1.name,
COUNT(*),
SUM(s1.score)
FROM
score s1,
student stu1
WHERE
s1.StudentNo = stu1.StudentNo
GROUP BY s1.StudentNo
SELECT
s1.StudentNo,
stu1.name,
COUNT(*),
SUM(s1.score)
FROM
score s1,
student stu1
WHERE
s1.StudentNo = stu1.StudentNo
GROUP BY s1.StudentNo
SELECT
COUNT(*)
FROM
teacher t1
WHERE
t1.name like '李%'
SELECT
COUNT(*)
FROM
teacher t1
WHERE
t1.name like '李%'
SELECT
stu1.StudentNo,
stu1.name
FROM
student stu1
WHERE
stu1.StudentNo NOT IN
(
SELECT DISTINCT
s1.StudentNo
FROM
score s1,
course c1,
teacher t1
WHERE
s1.courseNo = c1.CourseNo
AND
c1.teacherNo = t1.teacherNo
AND
t1.name = '叶平'
)
SELECT
stu1.StudentNo,
stu1.name
FROM
student stu1
WHERE
stu1.StudentNo NOT IN
(
SELECT DISTINCT
s1.StudentNo
FROM
score s1,
course c1,
teacher t1
WHERE
s1.courseNo = c1.CourseNo
AND
c1.teacherNo = t1.teacherNo
AND
t1.name = '叶平'
)
-- 这个算法比普通的要有想法
SELECT
s1.StudentNo,
stu1.name
FROM
score s1,
student stu1
WHERE
s1.StudentNo = stu1.StudentNo
AND
s1.CourseNo IN (1, 2)
GROUP BY s1.StudentNo
HAVING COUNT(*) = 2
-- 这个算法比普通的要有想法
SELECT
s1.StudentNo,
stu1.name
FROM
score s1,
student stu1
WHERE
s1.StudentNo = stu1.StudentNo
AND
s1.CourseNo IN (1, 2)
GROUP BY s1.StudentNo
HAVING COUNT(*) = 2
SELECT
s1.StudentNo,
stu1.name
FROM
score s1,
student stu1
WHERE
s1.StudentNo = stu1.StudentNo
AND
s1.CourseNo = 1
AND
s1.StudentNo IN
(
SELECT
s2.StudentNo
FROM
score s2
WHERE
s2.CourseNo = 2
)
SELECT
s1.StudentNo,
stu1.name
FROM
score s1,
student stu1
WHERE
s1.StudentNo = stu1.StudentNo
AND
s1.CourseNo = 1
AND
s1.StudentNo IN
(
SELECT
s2.StudentNo
FROM
score s2
WHERE
s2.CourseNo = 2
)
-- 利用主键值不相同,它们的和一定各不相同,叶平老师的课程的主键值和如果与学生所学的叶平老师的课程的主键值和相等,那么说明学了叶平老师所有课程
SELECT
stu1.StudentNo,
stu1.name
FROM
score s1,
student stu1,
course c1,
teacher t1
WHERE
s1.StudentNo = stu1.StudentNo
AND
s1.CourseNo = c1.CourseNo
AND
c1.teacherNo = t1.teacherNo
AND
t1.name = '叶平'
GROUP BY s1.StudentNo
HAVING SUM(s1.CourseNo)=
(
SELECT
SUM(c2.CourseNo)
FROM
course c2,
teacher t2
WHERE
c2.teacherNo = t2.teacherNo
AND
t2.name = '叶平'
)
-- 利用主键值不相同,它们的和一定各不相同,叶平老师的课程的主键值和如果与学生所学的叶平老师的课程的主键值和相等,那么说明学了叶平老师所有课程
SELECT
stu1.StudentNo,
stu1.name
FROM
score s1,
student stu1,
course c1,
teacher t1
WHERE
s1.StudentNo = stu1.StudentNo
AND
s1.CourseNo = c1.CourseNo
AND
c1.teacherNo = t1.teacherNo
AND
t1.name = '叶平'
GROUP BY s1.StudentNo
HAVING SUM(s1.CourseNo)=
(
SELECT
SUM(c2.CourseNo)
FROM
course c2,
teacher t2
WHERE
c2.teacherNo = t2.teacherNo
AND
t2.name = '叶平'
)
-- 如果学生学习叶平老师的课程数量,与叶平老师所教学课程的数量相同,那么说明该同学学了叶平老师的所有课程
SELECT
stu1.StudentNo,
stu1.name
FROM
score s1,
student stu1,
course c1,
teacher t1
WHERE
s1.StudentNo = stu1.StudentNo
AND
s1.CourseNo = c1.CourseNo
AND
c1.teacherNo = t1.teacherNo
AND
t1.name = '叶平'
GROUP BY s1.StudentNo
HAVING COUNT(*) =
(
SELECT
COUNT(*)
FROM
course c2,
teacher t2
WHERE
c2.teacherNo = t2.teacherNo
AND
t2.name = '叶平'
)
-- 如果学生学习叶平老师的课程数量,与叶平老师所教学课程的数量相同,那么说明该同学学了叶平老师的所有课程
SELECT
stu1.StudentNo,
stu1.name
FROM
score s1,
student stu1,
course c1,
teacher t1
WHERE
s1.StudentNo = stu1.StudentNo
AND
s1.CourseNo = c1.CourseNo
AND
c1.teacherNo = t1.teacherNo
AND
t1.name = '叶平'
GROUP BY s1.StudentNo
HAVING COUNT(*) =
(
SELECT
COUNT(*)
FROM
course c2,
teacher t2
WHERE
c2.teacherNo = t2.teacherNo
AND
t2.name = '叶平'
)
SELECT
stu1.studentNo,
stu1.name
FROM
score s1,
(
SELECT
s2.StudentNo,
s2.score
FROM
score s2
WHERE
s2.CourseNo = 1
) t2,
student stu1
WHERE
s1.CourseNo = 2
AND
s1.StudentNo = t2.StudentNo
AND
s1.score < t2.score
AND
s1.StudentNo = stu1.studentNo
SELECT
stu1.studentNo,
stu1.name
FROM
score s1,
(
SELECT
s2.StudentNo,
s2.score
FROM
score s2
WHERE
s2.CourseNo = 1
) t2,
student stu1
WHERE
s1.CourseNo = 2
AND
s1.StudentNo = t2.StudentNo
AND
s1.score < t2.score
AND
s1.StudentNo = stu1.studentNo
SELECT DISTINCT
s1.StudentNo,
stu1.name
FROM
score s1,
student stu1
WHERE
s1.StudentNo = stu1.studentNo
AND
s1.score < 60
SELECT DISTINCT
s1.StudentNo,
stu1.name
FROM
score s1,
student stu1
WHERE
s1.StudentNo = stu1.studentNo
AND
s1.score < 60
SELECT
stu1.StudentNo,
stu1.name
FROM
score s1,
student stu1
WHERE
s1.StudentNo = stu1.StudentNo
GROUP BY s1.StudentNo
HAVING COUNT(*) <
(
SELECT
COUNT(*)
FROM
course c1
)
SELECT
stu1.StudentNo,
stu1.name
FROM
score s1,
student stu1
WHERE
s1.StudentNo = stu1.StudentNo
GROUP BY s1.StudentNo
HAVING COUNT(*) <
(
SELECT
COUNT(*)
FROM
course c1
)
SELECT DISTINCT
stu1.StudentNo,
stu1.name
FROM
score s1,
student stu1
WHERE
s1.StudentNo = stu1.StudentNo
AND
s1.CourseNo IN
(
SELECT
s2.CourseNo
FROM
score s2
WHERE
s2.StudentNo = 1
)
SELECT DISTINCT
stu1.StudentNo,
stu1.name
FROM
score s1,
student stu1
WHERE
s1.StudentNo = stu1.StudentNo
AND
s1.CourseNo IN
(
SELECT
s2.CourseNo
FROM
score s2
WHERE
s2.StudentNo = 1
)
SELECT DISTINCT
stu1.StudentNo,
stu1.name
FROM
score s1,
student stu1
WHERE
s1.StudentNo = stu1.StudentNo
AND
s1.StudentNo != 1
AND
s1.CourseNo IN
(
SELECT
s2.CourseNo
FROM
score s2
WHERE
s2.StudentNo = 1
)
SELECT DISTINCT
stu1.StudentNo,
stu1.name
FROM
score s1,
student stu1
WHERE
s1.StudentNo = stu1.StudentNo
AND
s1.StudentNo != 1
AND
s1.CourseNo IN
(
SELECT
s2.CourseNo
FROM
score s2
WHERE
s2.StudentNo = 1
)
-- 涉及将两表联合,将本表某字段的值按条件设置为另个表的某个字段的值 (参考链接:MySQL:把一个表中的数据按键值更新(update)到另一个表)
UPDATE
score s,
(
SELECT
s1.CourseNo as courseNo,
AVG(s1.score) as avgScore
FROM
score s1,
course c1,
teacher t1
WHERE
s1.CourseNo = c1.CourseNo
AND
c1.teacherNo = t1.teacherNo
AND
t1.name = '叶平'
GROUP BY s1.CourseNo
) as t
SET
s.score = t.avgScore
WHERE
s.CourseNo = t.courseNo
-- 涉及将两表联合,将本表某字段的值按条件设置为另个表的某个字段的值 (参考链接:MySQL:把一个表中的数据按键值更新(update)到另一个表)
UPDATE
score s,
(
SELECT
s1.CourseNo as courseNo,
AVG(s1.score) as avgScore
FROM
score s1,
course c1,
teacher t1
WHERE
s1.CourseNo = c1.CourseNo
AND
c1.teacherNo = t1.teacherNo
AND
t1.name = '叶平'
GROUP BY s1.CourseNo
) as t
SET
s.score = t.avgScore
WHERE
s.CourseNo = t.courseNo
SELECT
stu.studentNo,
stu.name
FROM
score s,
student stu
WHERE
s.StudentNo != 2
AND
s.StudentNo = stu.studentNo
GROUP BY s.StudentNo
HAVING SUM(s.CourseNo)=
(
SELECT
SUM(s1.CourseNo)
FROM
score s1
WHERE
s1.StudentNo = 2
)
SELECT
stu.studentNo,
stu.name
FROM
score s,
student stu
WHERE
s.StudentNo != 2
AND
s.StudentNo = stu.studentNo
GROUP BY s.StudentNo
HAVING SUM(s.CourseNo)=
(
SELECT
SUM(s1.CourseNo)
FROM
score s1
WHERE
s1.StudentNo = 2
)
DELETE FROM
score s
WHERE
s.CourseNo IN
(
SELECT
c.CourseNo
FROM
course c,
teacher t
WHERE
c.teacherNo = t.teacherNo
AND
t.name = '叶平'
)
DELETE FROM
score s
WHERE
s.CourseNo IN
(
SELECT
c.CourseNo
FROM
course c,
teacher t
WHERE
c.teacherNo = t.teacherNo
AND
t.name = '叶平'
)
-- 本题采用插入子查询的方式,三个字段中后两个字段为常量(基本格式:INSERT INTO R(A1, A2 ... ,An) 子查询)
INSERT INTO
score(StudentNo, CourseNo, score)
(
SELECT
stu.studentNo,
2,
(SELECT AVG(s3.score) FROM score s3 WHERE s3.CourseNo = 2)
FROM
student stu
WHERE
stu.studentNo
NOT IN
(
SELECT
s2.StudentNo
FROM
score s2
WHERE
s2.CourseNo = 2
)
)
-- 本题采用插入子查询的方式,三个字段中后两个字段为常量(基本格式:INSERT INTO R(A1, A2 ... ,An) 子查询)
INSERT INTO
score(StudentNo, CourseNo, score)
(
SELECT
stu.studentNo,
2,
(SELECT AVG(s3.score) FROM score s3 WHERE s3.CourseNo = 2)
FROM
student stu
WHERE
stu.studentNo
NOT IN
(
SELECT
s2.StudentNo
FROM
score s2
WHERE
s2.CourseNo = 2
)
)
-- 用了个极蠢的办法,虽然很瓜很绕但是也算是温习了下相关子查询、CASE WHEN、EXISTS了,另外对自己也有所启发,就留下了
-- 然后做到这里的时候感慨,随着练习总是越来越熟练的,尽管自己写得很绕,但以往是根本想不到用什么CASE WHEN、EXISTS之类的,也算是成长吧
SELECT
stu.studentNo,
CASE WHEN EXISTS (SELECT * FROM score s1 WHERE s1.CourseNo = 1 AND s1.studentNo = stu.studentNo) THEN (SELECT s.score FROM score s WHERE s.CourseNo = 1 AND s.studentNo = stu.studentNo) ELSE NULL END AS "语文",
CASE WHEN EXISTS (SELECT * FROM score s2 WHERE s2.CourseNo = 2 AND s2.studentNo = stu.studentNo) THEN (SELECT s.score FROM score s WHERE s.CourseNo = 2 AND s.studentNo = stu.studentNo) ELSE NULL END AS "数学",
CASE WHEN EXISTS (SELECT * FROM score s3 WHERE s3.CourseNo = 3 AND s3.studentNo = stu.studentNo) THEN (SELECT s.score FROM score s WHERE s.CourseNo = 3 AND s.studentNo = stu.studentNo) ELSE NULL END AS "英语",
t1.validateCount AS '有效科目数',
t2.validateAVG AS '有效平均分'
FROM
student stu,
(SELECT s.studentNo, COUNT(*) AS validateCount FROM score s WHERE s.CourseNo IN (1, 2, 3) GROUP BY s.StudentNo) as t1,
(SELECT s.studentNo, AVG(s.score) AS validateAVG FROM score s WHERE s.CourseNo IN (1, 2, 3) GROUP BY s.StudentNo) as t2
WHERE
stu.studentNo = t1.studentNo
AND
stu.studentNo = t2.studentNo
ORDER BY stu.StudentNo
-- 用了个极蠢的办法,虽然很瓜很绕但是也算是温习了下相关子查询、CASE WHEN、EXISTS了,另外对自己也有所启发,就留下了
-- 然后做到这里的时候感慨,随着练习总是越来越熟练的,尽管自己写得很绕,但以往是根本想不到用什么CASE WHEN、EXISTS之类的,也算是成长吧
SELECT
stu.studentNo,
CASE WHEN EXISTS (SELECT * FROM score s1 WHERE s1.CourseNo = 1 AND s1.studentNo = stu.studentNo) THEN (SELECT s.score FROM score s WHERE s.CourseNo = 1 AND s.studentNo = stu.studentNo) ELSE NULL END AS "语文",
CASE WHEN EXISTS (SELECT * FROM score s2 WHERE s2.CourseNo = 2 AND s2.studentNo = stu.studentNo) THEN (SELECT s.score FROM score s WHERE s.CourseNo = 2 AND s.studentNo = stu.studentNo) ELSE NULL END AS "数学",
CASE WHEN EXISTS (SELECT * FROM score s3 WHERE s3.CourseNo = 3 AND s3.studentNo = stu.studentNo) THEN (SELECT s.score FROM score s WHERE s.CourseNo = 3 AND s.studentNo = stu.studentNo) ELSE NULL END AS "英语",
t1.validateCount AS '有效科目数',
t2.validateAVG AS '有效平均分'
FROM
student stu,
(SELECT s.studentNo, COUNT(*) AS validateCount FROM score s WHERE s.CourseNo IN (1, 2, 3) GROUP BY s.StudentNo) as t1,
(SELECT s.studentNo, AVG(s.score) AS validateAVG FROM score s WHERE s.CourseNo IN (1, 2, 3) GROUP BY s.StudentNo) as t2
WHERE
stu.studentNo = t1.studentNo
AND
stu.studentNo = t2.studentNo
ORDER BY stu.StudentNo
-- 另外,参考博客中博主给出的答案如下,比我的就简洁多了,
-- 其次,他在这里的有效课程数和有效平均分是针对学生所有的成绩,而并非此处的仅仅三科
-- 因为题意也不是很清楚,也就作罢,正好算是两种形式吧
SELECT
s.StudentNo,
(SELECT s1.score FROM score s1 WHERE s1.CourseNo=1 AND s1.StudentNo = s.StudentNo) AS "语文",
(SELECT s2.score FROM score s2 WHERE s2.CourseNo=2 AND s2.StudentNo = s.StudentNo) AS "数学",
(SELECT s3.score FROM score s3 WHERE s3.CourseNo=3 AND s3.StudentNo = s.StudentNo) AS "英语",
COUNT(s.CourseNo) AS "有效课程数",
AVG(s.score) AS "有效平均分"
FROM
score s
GROUP BY s.StudentNo
ORDER BY s.StudentNo
-- 另外,参考博客中博主给出的答案如下,比我的就简洁多了,
-- 其次,他在这里的有效课程数和有效平均分是针对学生所有的成绩,而并非此处的仅仅三科
-- 因为题意也不是很清楚,也就作罢,正好算是两种形式吧
SELECT
s.StudentNo,
(SELECT s1.score FROM score s1 WHERE s1.CourseNo=1 AND s1.StudentNo = s.StudentNo) AS "语文",
(SELECT s2.score FROM score s2 WHERE s2.CourseNo=2 AND s2.StudentNo = s.StudentNo) AS "数学",
(SELECT s3.score FROM score s3 WHERE s3.CourseNo=3 AND s3.StudentNo = s.StudentNo) AS "英语",
COUNT(s.CourseNo) AS "有效课程数",
AVG(s.score) AS "有效平均分"
FROM
score s
GROUP BY s.StudentNo
ORDER BY s.StudentNo
SELECT
s.CourseNo,
MAX(s.score),
MIN(s.score)
FROM
score s
GROUP BY
s.CourseNo
SELECT
s.CourseNo,
MAX(s.score),
MIN(s.score)
FROM
score s
GROUP BY
s.CourseNo
-- 看了下上次在Github上写的,不得不说,practice makes perfect
SELECT
s.CourseNo,
c.name,
AVG(s.score) AS '平均分',
SUM(CASE WHEN s.score > 60 THEN 1 ELSE 0 END) AS '及格数',
COUNT(*) AS '总数',
SUM(CASE WHEN s.score > 60 THEN 1 ELSE 0 END)/COUNT(*)*100 AS '及格率'
FROM
score s,
course c
WHERE
s.CourseNo = c.courseNo
GROUP BY s.CourseNo
ORDER BY AVG(s.score), SUM(CASE WHEN s.score > 60 THEN 1 ELSE 0 END)/COUNT(*) DESC
-- 看了下上次在Github上写的,不得不说,practice makes perfect
SELECT
s.CourseNo,
c.name,
AVG(s.score) AS '平均分',
SUM(CASE WHEN s.score > 60 THEN 1 ELSE 0 END) AS '及格数',
COUNT(*) AS '总数',
SUM(CASE WHEN s.score > 60 THEN 1 ELSE 0 END)/COUNT(*)*100 AS '及格率'
FROM
score s,
course c
WHERE
s.CourseNo = c.courseNo
GROUP BY s.CourseNo
ORDER BY AVG(s.score), SUM(CASE WHEN s.score > 60 THEN 1 ELSE 0 END)/COUNT(*) DESC
SELECT
c1.name,
t1.name,
AVG(s1.score)
FROM
score s1,
course c1,
teacher t1
WHERE
s1.CourseNo = c1.courseNo
AND
c1.teacherNo = t1.teacherNo
GROUP BY s1.CourseNo
ORDER BY AVG(s1.score) DESC
SELECT
c1.name,
t1.name,
AVG(s1.score)
FROM
score s1,
course c1,
teacher t1
WHERE
s1.CourseNo = c1.courseNo
AND
c1.teacherNo = t1.teacherNo
GROUP BY s1.CourseNo
ORDER BY AVG(s1.score) DESC
SELECT
c.courseNo AS '课程ID',
c.name AS '课程名称',
SUM(CASE WHEN s.score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS '(100-85)',
SUM(CASE WHEN s.score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS '(85-70)',
SUM(CASE WHEN s.score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS '(70-60)',
SUM(CASE WHEN s.score < 60 THEN 1 ELSE 0 END) AS '(低于60)'
FROM
course c,
score s
WHERE
c.courseNo = s.CourseNo
GROUP BY c.courseNo
SELECT
c.courseNo AS '课程ID',
c.name AS '课程名称',
SUM(CASE WHEN s.score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS '(100-85)',
SUM(CASE WHEN s.score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS '(85-70)',
SUM(CASE WHEN s.score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS '(70-60)',
SUM(CASE WHEN s.score < 60 THEN 1 ELSE 0 END) AS '(低于60)'
FROM
course c,
score s
WHERE
c.courseNo = s.CourseNo
GROUP BY c.courseNo
SELECT
*
FROM
score s
WHERE
(
SELECT
COUNT(*)
FROM
score s1
WHERE
s1.CourseNo = s.CourseNo
AND
s1.score > s.score
) < 3
ORDER BY s.CourseNo
SELECT
*
FROM
score s
WHERE
(
SELECT
COUNT(*)
FROM
score s1
WHERE
s1.CourseNo = s.CourseNo
AND
s1.score > s.score
) < 3
ORDER BY s.CourseNo
SELECT
c.name AS '课程',
COUNT(s.StudentNo) AS '选修学生数'
FROM
course c LEFT JOIN score s ON c.courseNo = s.CourseNo
GROUP BY c.courseNo
SELECT
c.name AS '课程',
COUNT(s.StudentNo) AS '选修学生数'
FROM
course c LEFT JOIN score s ON c.courseNo = s.CourseNo
GROUP BY c.courseNo
SELECT
stu1.StudentNo AS '学号',
stu1.name AS '姓名'
FROM
(SELECT StudentNo, COUNT(CourseNo) AS amount FROM score GROUP BY StudentNo) t1,
student stu1
WHERE
t1.StudentNo = stu1.studentNo
AND
t1.amount = 1
SELECT
stu1.StudentNo AS '学号',
stu1.name AS '姓名'
FROM
(SELECT StudentNo, COUNT(CourseNo) AS amount FROM score GROUP BY StudentNo) t1,
student stu1
WHERE
t1.StudentNo = stu1.studentNo
AND
t1.amount = 1
SELECT
stu1.studentNo AS '学号',
stu1.name AS '姓名'
FROM
score s,
student stu1
WHERE
s.StudentNo = stu1.studentNo
GROUP BY s.StudentNo
HAVING COUNT(s.CourseNo) = 1
SELECT
stu1.studentNo AS '学号',
stu1.name AS '姓名'
FROM
score s,
student stu1
WHERE
s.StudentNo = stu1.studentNo
GROUP BY s.StudentNo
HAVING COUNT(s.CourseNo) = 1
SELECT
s.sex AS '性别',
COUNT(*) AS '人数'
FROM
student s
GROUP BY s.sex
SELECT
s.sex AS '性别',
COUNT(*) AS '人数'
FROM
student s
GROUP BY s.sex
SELECT
s.name AS '姓名',
COUNT(*) AS '学生数'
FROM
student s
GROUP BY s.name
SELECT
s.name AS '姓名',
COUNT(*) AS '学生数'
FROM
student s
GROUP BY s.name
SELECT
s.name AS '姓名'
FROM
student s
WHERE
YEAR(CURDATE()) - s.age = 1991
SELECT
s.name AS '姓名'
FROM
student s
WHERE
YEAR(CURDATE()) - s.age = 1991
#未考虑到课程无人选修的情况
SELECT
c.name AS '课程名称',
AVG(s.score) AS '平均成绩'
FROM
score s,
course c
WHERE
s.CourseNo = c.courseNo
GROUP BY c.courseNo
ORDER BY AVG(s.score)
#如果某课程无人选修,其平均成绩显示为null
SELECT
c.name AS '课程名称',
AVG(s.score) AS '平均成绩'
FROM
course c LEFT JOIN score s ON c.courseNo = s.CourseNo
GROUP BY c.courseNo
ORDER BY AVG(s.score)
#未考虑到课程无人选修的情况
SELECT
c.name AS '课程名称',
AVG(s.score) AS '平均成绩'
FROM
score s,
course c
WHERE
s.CourseNo = c.courseNo
GROUP BY c.courseNo
ORDER BY AVG(s.score)
#如果某课程无人选修,其平均成绩显示为null
SELECT
c.name AS '课程名称',
AVG(s.score) AS '平均成绩'
FROM
course c LEFT JOIN score s ON c.courseNo = s.CourseNo
GROUP BY c.courseNo
ORDER BY AVG(s.score)
SELECT
stu.studentNo AS '学号',
stu.name AS '姓名',
AVG(s.score) AS '平均成绩'
FROM
score s,
student stu
WHERE
s.StudentNo = stu.studentNo
GROUP BY s.StudentNo
HAVING AVG(s.score) > 85
SELECT
stu.studentNo AS '学号',
stu.name AS '姓名',
AVG(s.score) AS '平均成绩'
FROM
score s,
student stu
WHERE
s.StudentNo = stu.studentNo
GROUP BY s.StudentNo
HAVING AVG(s.score) > 85
SELECT
stu.name AS '姓名',
s.score AS '数学成绩'
FROM
score s,
course c,
student stu
WHERE
s.CourseNo = c.courseNo
AND
s.StudentNo = stu.studentNo
AND
c.name = '数学'
AND
s.score < 60
SELECT
stu.name AS '姓名',
s.score AS '数学成绩'
FROM
score s,
course c,
student stu
WHERE
s.CourseNo = c.courseNo
AND
s.StudentNo = stu.studentNo
AND
c.name = '数学'
AND
s.score < 60
SELECT
stu.name AS '姓名',
c.name AS '选课'
FROM
score s,
course c,
student stu
WHERE
s.CourseNo = c.courseNo
AND
s.StudentNo = stu.studentNo
ORDER BY stu.name
SELECT
stu.name AS '姓名',
c.name AS '选课'
FROM
score s,
course c,
student stu
WHERE
s.CourseNo = c.courseNo
AND
s.StudentNo = stu.studentNo
ORDER BY stu.name
SELECT
stu.name AS '姓名',
c.name AS '课程名称',
s.score AS '分数'
FROM
score s,
student stu,
course c
WHERE
s.StudentNo = stu.studentNo
AND
s.CourseNo = c.courseNo
AND
s.score > 70
SELECT
stu.name AS '姓名',
c.name AS '课程名称',
s.score AS '分数'
FROM
score s,
student stu,
course c
WHERE
s.StudentNo = stu.studentNo
AND
s.CourseNo = c.courseNo
AND
s.score > 70
#包含不及格记录的课程
SELECT DISTINCT
c.courseNo AS '课程号',
c.name AS '课程名称'
FROM
score s,
course c
WHERE
s.CourseNo = c.courseNo
AND
s.score < 60
#不及格的课程的选修记录
SELECT
stu.name AS '姓名',
c.name AS '课程名称',
s.score AS '分数'
FROM
score s,
student stu,
course c
WHERE
s.StudentNo = stu.studentNo
AND
s.CourseNo = c.courseNo
AND
s.score < 60
ORDER BY c.courseNo DESC
#包含不及格记录的课程
SELECT DISTINCT
c.courseNo AS '课程号',
c.name AS '课程名称'
FROM
score s,
course c
WHERE
s.CourseNo = c.courseNo
AND
s.score < 60
#不及格的课程的选修记录
SELECT
stu.name AS '姓名',
c.name AS '课程名称',
s.score AS '分数'
FROM
score s,
student stu,
course c
WHERE
s.StudentNo = stu.studentNo
AND
s.CourseNo = c.courseNo
AND
s.score < 60
ORDER BY c.courseNo DESC
SELECT
stu.studentNo AS '学号',
stu.name AS '姓名'
FROM
score s,
student stu
WHERE
s.StudentNo = stu.studentNo
AND
s.CourseNo = 3
AND
s.score > 80
SELECT
stu.studentNo AS '学号',
stu.name AS '姓名'
FROM
score s,
student stu
WHERE
s.StudentNo = stu.studentNo
AND
s.CourseNo = 3
AND
s.score > 80
#method-1
SELECT
COUNT(DISTINCT s.StudentNo) AS '选了课程的学生人数'
FROM
score s
#method-2
SELECT
COUNT(*) AS '选了课程的学生人数'
FROM
(SELECT * FROM score s GROUP BY s.StudentNo) t
#method-1
SELECT
COUNT(DISTINCT s.StudentNo) AS '选了课程的学生人数'
FROM
score s
#method-2
SELECT
COUNT(*) AS '选了课程的学生人数'
FROM
(SELECT * FROM score s GROUP BY s.StudentNo) t
SELECT
stu.name AS '学生姓名',
s.score AS '成绩'
FROM
score s,
student stu,
course c,
teacher t
WHERE
s.StudentNo = stu.studentNo
AND
s.CourseNo = c.courseNo
AND
c.teacherNo = t.teacherNo
AND
t.name = '杨艳'
ORDER BY s.score DESC
LIMIT 0, 1
SELECT
stu.name AS '学生姓名',
s.score AS '成绩'
FROM
score s,
student stu,
course c,
teacher t
WHERE
s.StudentNo = stu.studentNo
AND
s.CourseNo = c.courseNo
AND
c.teacherNo = t.teacherNo
AND
t.name = '杨艳'
ORDER BY s.score DESC
LIMIT 0, 1
SELECT
c.name AS '课程名称',
COUNT(*) AS '选修人数'
FROM
score s,
course c
WHERE
s.CourseNo = c.courseNo
GROUP BY s.CourseNo
SELECT
c.name AS '课程名称',
COUNT(*) AS '选修人数'
FROM
score s,
course c
WHERE
s.CourseNo = c.courseNo
GROUP BY s.CourseNo
#method-1
SELECT
s.StudentNo AS '学号',
s.CourseNo AS '课程号',
s.score AS '成绩'
FROM
score s
WHERE
(SELECT COUNT(*) FROM score s1 WHERE s1.score = s.score AND s1.CourseNo <> s.COurseNo) > 0
ORDER BY s.score DESC, s.StudentNo, s.CourseNo
#method-2
SELECT DISTINCT
s1.StudentNo AS '学号',
s1.CourseNo AS '课程号',
s1.score AS '成绩'
FROM
score s1,
score s2
WHERE
s1.score = s2.score
AND
s1.CourseNo <> s2.CourseNo
ORDER BY s1.score DESC, s1.StudentNo, s1.CourseNo
#method-1
SELECT
s.StudentNo AS '学号',
s.CourseNo AS '课程号',
s.score AS '成绩'
FROM
score s
WHERE
(SELECT COUNT(*) FROM score s1 WHERE s1.score = s.score AND s1.CourseNo <> s.COurseNo) > 0
ORDER BY s.score DESC, s.StudentNo, s.CourseNo
#method-2
SELECT DISTINCT
s1.StudentNo AS '学号',
s1.CourseNo AS '课程号',
s1.score AS '成绩'
FROM
score s1,
score s2
WHERE
s1.score = s2.score
AND
s1.CourseNo <> s2.CourseNo
ORDER BY s1.score DESC, s1.StudentNo, s1.CourseNo
SELECT
s.CourseNo AS '课程号',
s.StudentNo AS '学号',
s.score AS '分数'
FROM
score s
WHERE
(SELECT COUNT(*) FROM score s1 WHERE s1.CourseNo = s.CourseNo AND s1.score > s.score) < 2
ORDER BY s.CourseNo
SELECT
s.CourseNo AS '课程号',
s.StudentNo AS '学号',
s.score AS '分数'
FROM
score s
WHERE
(SELECT COUNT(*) FROM score s1 WHERE s1.CourseNo = s.CourseNo AND s1.score > s.score) < 2
ORDER BY s.CourseNo
附件列表
你必知必会的SQL面试题的更多相关文章
- 读书笔记汇总 - SQL必知必会(第4版)
本系列记录并分享学习SQL的过程,主要内容为SQL的基础概念及练习过程. 书目信息 中文名:<SQL必知必会(第4版)> 英文名:<Sams Teach Yourself SQL i ...
- 读书笔记--SQL必知必会--建立练习环境
书目信息 中文名:<SQL必知必会(第4版)> 英文名:<Sams Teach Yourself SQL in 10 Minutes - Fourth Edition> MyS ...
- 读书笔记--SQL必知必会12--联结表
12.1 联结 联结(join),利用SQL的SELECT在数据查询的执行中联结表. 12.1.1 关系表 关系数据库中,关系表的设计是把信息分解成多个表,一类数据一个表,各表通过某些共同的值互相关联 ...
- 读书笔记--SQL必知必会18--视图
读书笔记--SQL必知必会18--视图 18.1 视图 视图是虚拟的表,只包含使用时动态检索数据的查询. 也就是说作为视图,它不包含任何列和数据,包含的是一个查询. 18.1.1 为什么使用视图 重用 ...
- 《SQL必知必会》学习笔记(一)
这两天看了<SQL必知必会>第四版这本书,并照着书上做了不少实验,也对以前的概念有得新的认识,也发现以前自己有得地方理解错了.我采用的数据库是SQL Server2012.数据库中有一张比 ...
- SQL 必知必会
本文介绍基本的 SQL 语句,包括查询.过滤.排序.分组.联结.视图.插入数据.创建操纵表等.入门系列,不足颇多,望诸君指点. 注意本文某些例子只能在特定的DBMS中实现(有的已标明,有的未标明),不 ...
- 0005 《SQL必知必会》笔记01-SELECT语句
1.SELECT基本语句: SELECT 字段名1,···,字段名n FROM 表名 2.检索所有字段,用"*"替换字段名,这会导致效率低下 SELECT * FROM 表名; 3 ...
- 《SQL必知必会》学习笔记二)
<SQL必知必会>学习笔记(二) 咱们接着上一篇的内容继续.这一篇主要回顾子查询,联合查询,复制表这三类内容. 上一部分基本上都是简单的Select查询,即从单个数据库表中检索数据的单条语 ...
- 图灵程序设计丛书(SQL必知必会)笔记
SQL必知必会 第二课:检索数据 1.分页 (1).SQL Server 栗子 : select top 2 columns from tableName (2).Oracle 栗子 :select ...
随机推荐
- 一篇文章学会Spring4.0
spring概述 spring 是一个开源框架. Spring 为简化企业级应用开发而生. 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能. Spring 是 ...
- Android笔记: ViewPager基本用法
ViewPager控件可以实现多个View(或fragment)滑动切换的效果,属于比较常用的控件.下面用一个简单的例子介绍一下该控件的基本用法. 首先是布局文件activity_view_pager ...
- ssh环境的搭建,基于注解和配置文件使用
搭建spring.Struts2.hibernate三大框架的环境 这里分两部分来讲:一.用myeclipse 2014 快速搭建环境,非常快捷, 大部分配置文件信息系统都帮我们写好,建议老手使用 二 ...
- Ubuntu14.04下搭建VPN服务 -pptp
在Ubantu下采用PPTP搭建VPN,优点是配置简单快捷.本教程亲自测试,熟练了在新机器上5分钟搞定VPN. - - - - - - - - - - - - - - - - - - - - - - ...
- JDK+Apache+Tomcat+MySQL配置 一起来学习吧
配置JDK1.8+Apache2.4+Tomcat8.0+mySQL5.1,网上的资料非常繁杂,花费几天时间配置成功,汇总记录. 操作系统:CentOS6.5 预先下载最新版软件: apache-to ...
- SVG视野
前面的话 SVG中坐标系统非常关键,但在介绍坐标系统之前,首先要了解视野.本文将详细介绍SVG视野 视野 下面来区分视窗.世界和视野 [视窗] SVG的属性width.height来控制视窗的大小,也 ...
- java内存管理(堆、栈、方法区)
java内存管理 简介 首先我们要了解我们为什么要学习java虚拟机的内存管理,不是java的gc垃圾回收机制都帮我们释放了内存了吗?但是在写程序的过程中却也往往因为不懂内存管理而造成了一些不容易察觉 ...
- 比较三个 CSS 预处理器:Sass、LESS 和 Stylus(上)
前沿 : 第一次写不够成熟,可能描述有所错误,还请大家修改指正,我会对已完成的文章进行修改. 一.什么是CSS预处理器 CSS预处理器定义了一种新的语言,基本的思想是用一种专门的编程语言,开发者只需要 ...
- tomcat 和 jboss access log 日志输出详解
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt179 工作中nginx+jboss/tomcat反向代理集成,想打开后端jb ...
- spring cloud认识
Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布式会话和集群状 ...