sql 摘抄
练习题和参考解
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,
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
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
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
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,
(
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
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.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
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 = '叶平'
)
-- 本题采用插入子查询的方式,三个字段中后两个字段为常量(基本格式: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
-- 另外,参考博客中博主给出的答案如下,比我的就简洁多了,
-- 其次,他在这里的有效课程数和有效平均分是针对学生所有的成绩,而并非此处的仅仅三科
-- 因为题意也不是很清楚,也就作罢,正好算是两种形式吧
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
-- 看了下上次在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
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
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
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.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
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.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 '课程名称',
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
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
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
#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 '分数'
练习题和参考解
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,
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
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
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
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,
(
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
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.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
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 = '叶平'
)
-- 本题采用插入子查询的方式,三个字段中后两个字段为常量(基本格式: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
-- 另外,参考博客中博主给出的答案如下,比我的就简洁多了,
-- 其次,他在这里的有效课程数和有效平均分是针对学生所有的成绩,而并非此处的仅仅三科
-- 因为题意也不是很清楚,也就作罢,正好算是两种形式吧
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
-- 看了下上次在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
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
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
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.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
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.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 '课程名称',
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
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
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
#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
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 摘抄的更多相关文章
- oracle sql 语句 示例
--oracle 用户对象的导入导出 exp devimage/oracle@172.xx.x.xx/TESTDB owner='devimage' file=d:/devimage.dmp log= ...
- 解开发者之痛:中国移动MySQL数据库优化最佳实践(转)
开源数据库MySQL比较容易碰到性能瓶颈,为此经常需要对MySQL数据库进行优化,而MySQL数据库优化需要运维DBA与相关开发共同参与,其中MySQL参数及服务器配置优化主要由运维DBA完成,开发则 ...
- 中国移动MySQL数据库优化最佳实践
原创 2016-08-12 章颖 DBAplus社群 本文根据DBAplus社群第69期线上分享整理而成,文末还有书送哦~ 讲师介绍章颖 数据研发工程师 现任中国移动杭州研发中心数据研发工程师,擅长M ...
- mysql数据库优化原则
一.一个例子 数据库需要处理的行数: 189444*1877*13482~~~479亿 如果在关联字段上加上合适的索引: 数据库需要处理的行数:368006*1*3*1~~~110万 MySQL通常是 ...
- 防止 SQL 注入的方法(摘抄)
——选自<深入Ajax : 架构与最佳实践 = Advanced Ajax : architecture and best practices/ (美)Shawn M.Lauriat著:张过,宋 ...
- [摘抄] 为什么 Linq 可以高效率查询 SQL ?
From C# in Depth(3rd) - Jon Skeet using (LinqDemoDataContext db = new LinqDemoDataContext()) { var f ...
- SQL server 查询语句优先级-摘抄
SQL 不同于与其他编程语言的最明显特征是处理代码的顺序.在大数编程语言中,代码按编码顺序被处理,但是在SQL语言中,第一个被处理的子句是FROM子句,尽管SELECT语句第一个出现,但是几乎总是最后 ...
- SQL 全角半角转换-(摘抄)
/****** SQL转换全角/半角函数 开始******/ CREATE FUNCTION ConvertWordAngle ( @str NVARCHAR(4000), --要转换的字符串 @fl ...
- sql分页汇总-摘抄自网络
文章:几种常见SQL分页方式效率比较 个人倾向于:(2005以上版本支持 row_number()) select * from ( select row_number()over(order by ...
随机推荐
- 微信小程序中显示html富文本的方法
微信小程序中显示html富文本的方法 使用方法:git地址:https://github.com/icindy/wxParse 一.下载wxParse文件 二.在要引入的页面的js文件中,引入文件 j ...
- springboot 加载jsp 刷新jsp ,刷新Controller (亲自尝试)
解决jsp加载成功.<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId&g ...
- 第一周作业—N42-虚怀若谷
一.Linux发行版描述. Linux发行版主要有三个分支:Slackware.Debian.Redhat: (1) Slackware: SUSE:基于Slackware二次开发的一款Linux,主 ...
- 系统信息&&硬件信息查看
系统信息&&硬件信息查看 一系统信息查看 (一)查看系统版本信息 [root@centos7 ~]# cat /etc/redhat-release CentOS Linux rele ...
- CTF | bugku | 速度要快
检查源码时发现有 <!-- OK ,now you have to post the margin what you find --> 检查响应头发现有 flag: 6LeR55qE6L+ ...
- [CSP-S模拟测试]:公园(BFS+剪枝)
题目传送门(内部题31) 输入格式 第一行,五个整数$V,M,N,E,L$.接下来$M$行,每行两个正整数$s_i,a_i$.保证$s_i$互不相等.接下来$N$行,每行两个正整数$t_j,b_j$. ...
- 简记 jQuery 插件模板
/** * @lisence jquery plugin demo v1.0.0 * * author: Jeremy Yu * * description: * this is a jquery p ...
- Cent OS (二)常用的命令介绍
1. 常用的Linux命令 序号 命令 对应英文 作用 01 ls list 查看当前文件夹下的内容 02 pwd print work directory 查看当前所在的文件夹 03 cd [目 ...
- QTP学习笔记--Excel数据源
直接读取Excel表格的function摘自此处http://www.51testing.com/html/40/307440-827863.html 特此感谢! Excel作为QTP自动化测试的数 ...
- Linux sudo 详解
简单的说,sudo 是一种权限管理机制,管理员可以授权于一些普通用户去执行一些 root 执行的操作,而不需要知道 root 的密码.严谨些说,sudo 允许一个已授权用户以超级用户或者其它用户的角色 ...