练习题和参考解

(1)查询“001”课程比“002”课程成绩低的所有学生的学号、001学科成绩、002学科成绩
 

 
 
 
 
 
 
 
1
SELECT 
2
  s1.StudentNo,
3
  s1.score AS '001',
4
  s2.score AS '002'
5
FROM
6
  score s1,
7
  (
8
  SELECT
9
    *
10
  FROM
11
    score s
12
  WHERE
13
    s.CourseNo = 2
14
  ) s2
15
WHERE
16
  s1.CourseNo = 1
17
  AND
18
  s1.StudentNo = s2.StudentNo
19
  AND
20
  s1.score < s2.score
21
  ORDER BY s1.StudentNo
 
 
 
 

 
 
 
 
 
 
 
1
SELECT
2
  s1.StudentNo,
3
  s1.score AS '001',
4
  s2.score AS '002'
5
FROM 
6
  score s1, 
7
  score s2
8
WHERE 
9
  s1.CourseNo = 001 
10
  AND 
11
  s2.CourseNo = 002 
12
  AND 
13
  s1.StudentNo = s2.StudentNo
14
  AND
15
  s1.score < s2.score
16
  ORDER BY s1.StudentNo
 
 
 
 
(2)查询平均成绩大于60分的同学的学号和平均成绩
 

 
 
 
 
 
 
 
1
SELECT
2
  s1.StudentNo,
3
  AVG(s1.score)
4
FROM
5
  score s1
6
GROUP BY s1.StudentNo
7
HAVING AVG(s1.score)>60
 
 
 
 
(3)查询所有同学的学号、姓名、选课数、总成绩
 

 
 
 
 
 
 
 
1
SELECT
2
  s1.StudentNo,
3
  stu1.name,
4
  COUNT(*),
5
  SUM(s1.score)
6
FROM
7
  score s1,
8
  student stu1
9
WHERE
10
  s1.StudentNo = stu1.StudentNo
11
GROUP BY s1.StudentNo
 
 
 
 
(4)查询姓“李”的老师的个数
 

 
 
 
 
 
 
 
1
SELECT
2
  COUNT(*)
3
FROM
4
  teacher t1
5
WHERE
6
  t1.name like '李%'
 
 
 
 
(5)查询没学过“叶平”老师课的同学的学号、姓名
 

 
 
 
 
 
 
 
1
SELECT
2
  stu1.StudentNo,
3
  stu1.name
4
FROM
5
  student stu1
6
WHERE
7
  stu1.StudentNo NOT IN
8
  (
9
  SELECT DISTINCT
10
    s1.StudentNo
11
  FROM
12
    score s1,
13
    course c1,
14
    teacher t1
15
  WHERE
16
    s1.courseNo = c1.CourseNo
17
    AND
18
    c1.teacherNo = t1.teacherNo
19
    AND
20
    t1.name = '叶平'
21
  )
 
 
 
(6)查询学过“001”并且也学过编号“002”课程的同学的学号、姓名
 

 
 
 
 
 
 
 
1
-- 这个算法比普通的要有想法
2
SELECT
3
  s1.StudentNo,
4
  stu1.name
5
FROM
6
  score s1,
7
  student stu1
8
WHERE
9
  s1.StudentNo = stu1.StudentNo
10
  AND
11
  s1.CourseNo IN (1, 2)
12
GROUP BY s1.StudentNo
13
HAVING COUNT(*) = 2
 
 
 
 

 
 
 
 
 
 
 
1
SELECT
2
  s1.StudentNo,
3
  stu1.name
4
FROM
5
  score s1,
6
  student stu1
7
WHERE
8
  s1.StudentNo = stu1.StudentNo
9
  AND
10
  s1.CourseNo = 1
11
  AND
12
  s1.StudentNo IN
13
  (
14
  SELECT
15
    s2.StudentNo
16
  FROM
17
    score s2
18
  WHERE
19
    s2.CourseNo = 2
20
  )
 
 
 
(7)查询学过“叶平”老师所教的所有课的同学的学号、姓名
 

 
 
 
 
 
 
 
1
-- 利用主键值不相同,它们的和一定各不相同,叶平老师的课程的主键值和如果与学生所学的叶平老师的课程的主键值和相等,那么说明学了叶平老师所有课程
2
SELECT
3
  stu1.StudentNo,
4
  stu1.name
5
FROM
6
  score s1,
7
  student stu1,
8
  course c1,
9
  teacher t1
10
WHERE
11
  s1.StudentNo = stu1.StudentNo
12
  AND
13
  s1.CourseNo = c1.CourseNo
14
  AND
15
  c1.teacherNo = t1.teacherNo
16
  AND
17
  t1.name = '叶平'
18
GROUP BY s1.StudentNo
19
HAVING SUM(s1.CourseNo)=
20
(
21
SELECT
22
  SUM(c2.CourseNo)
23
FROM
24
  course c2,
25
  teacher t2
26
WHERE
27
  c2.teacherNo = t2.teacherNo
28
  AND
29
  t2.name = '叶平'
30
)
 
 
 
 

 
 
 
 
 
 
 
1
-- 如果学生学习叶平老师的课程数量,与叶平老师所教学课程的数量相同,那么说明该同学学了叶平老师的所有课程
2
SELECT
3
  stu1.StudentNo,
4
  stu1.name
5
FROM
6
  score s1,
7
  student stu1,
8
  course c1,
9
  teacher t1
10
WHERE
11
  s1.StudentNo = stu1.StudentNo
12
  AND
13
  s1.CourseNo = c1.CourseNo
14
  AND
15
  c1.teacherNo = t1.teacherNo
16
  AND
17
  t1.name = '叶平'
18
GROUP BY s1.StudentNo
19
HAVING COUNT(*) =
20
(
21
SELECT
22
  COUNT(*)
23
FROM
24
  course c2,
25
  teacher t2
26
WHERE
27
  c2.teacherNo = t2.teacherNo
28
  AND
29
  t2.name = '叶平' 
30
)
 
 
 
(8)查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名
 

 
 
 
 
 
 
 
1
SELECT
2
  stu1.studentNo,
3
  stu1.name
4
FROM
5
  score s1,
6
  (
7
  SELECT
8
    s2.StudentNo,
9
    s2.score
10
  FROM
11
    score s2
12
  WHERE
13
    s2.CourseNo = 1
14
  ) t2,
15
  student stu1
16
WHERE
17
  s1.CourseNo = 2
18
  AND 
19
  s1.StudentNo = t2.StudentNo
20
  AND 
21
  s1.score < t2.score
22
  AND 
23
  s1.StudentNo = stu1.studentNo
 
 
 
(9)查询有课程成绩小于60分的同学的学号、姓名
 

 
 
 
 
 
 
 
1
SELECT DISTINCT
2
  s1.StudentNo,
3
  stu1.name
4
FROM
5
  score s1,
6
  student stu1
7
WHERE
8
  s1.StudentNo = stu1.studentNo
9
  AND
10
  s1.score < 60
 
 
 
(10)查询没有学全所有课的同学的学号、姓名
 

 
 
 
 
 
 
 
1
SELECT
2
  stu1.StudentNo,
3
  stu1.name
4
FROM
5
  score s1,
6
  student stu1
7
WHERE
8
  s1.StudentNo = stu1.StudentNo
9
GROUP BY s1.StudentNo
10
HAVING COUNT(*) < 
11
(
12
SELECT
13
  COUNT(*)
14
FROM
15
  course c1
16
)
 
 
 
(11)查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名
 

 
 
 
 
 
 
 
1
SELECT DISTINCT
2
  stu1.StudentNo,
3
  stu1.name
4
FROM
5
  score s1,
6
  student stu1
7
WHERE
8
  s1.StudentNo = stu1.StudentNo
9
  AND
10
  s1.CourseNo IN
11
  (
12
  SELECT
13
    s2.CourseNo
14
  FROM
15
    score s2
16
  WHERE
17
    s2.StudentNo = 1
18
  )
 
 
 
(12)查询至少学过学号为“001”同学所有一门课的其他同学学号和姓名(和11题撞脸,排除1号同学就可以了)
 

 
 
 
 
 
 
 
1
SELECT DISTINCT
2
  stu1.StudentNo,
3
  stu1.name
4
FROM
5
  score s1,
6
  student stu1
7
WHERE
8
  s1.StudentNo = stu1.StudentNo
9
  AND
10
  s1.StudentNo != 1
11
  AND
12
  s1.CourseNo IN
13
  (
14
  SELECT
15
    s2.CourseNo
16
  FROM
17
    score s2
18
  WHERE
19
    s2.StudentNo = 1
20
  )
 
 
 
(13)把“score”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩
 

 
 
 
 
 
 
 
1
-- 涉及将两表联合,将本表某字段的值按条件设置为另个表的某个字段的值 (参考链接:MySQL:把一个表中的数据按键值更新(update)到另一个表)
2
UPDATE
3
  score s,
4
  (
5
  SELECT
6
    s1.CourseNo as courseNo,
7
    AVG(s1.score) as avgScore 
8
  FROM
9
    score s1,
10
    course c1,
11
    teacher t1
12
  WHERE
13
    s1.CourseNo = c1.CourseNo
14
    AND
15
    c1.teacherNo = t1.teacherNo
16
    AND
17
    t1.name = '叶平'
18
  GROUP BY s1.CourseNo
19
  ) as t
20
SET
21
  s.score = t.avgScore
22
WHERE
23
  s.CourseNo = t.courseNo
 
 
 
(14)查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名
 

 
 
 
 
 
 
 
1
SELECT
2
  stu.studentNo,
3
  stu.name
4
FROM
5
  score s,
6
  student stu
7
WHERE
8
  s.StudentNo != 2
9
  AND
10
  s.StudentNo = stu.studentNo
11
GROUP BY s.StudentNo
12
HAVING SUM(s.CourseNo)=
13
(
14
SELECT
15
  SUM(s1.CourseNo)
16
FROM
17
  score s1
18
WHERE
19
  s1.StudentNo = 2
20
)
 
 
 
(15)删除学习“叶平”老师课的SC表记录
 

 
 
 
 
 
 
 
1
DELETE FROM
2
  score s
3
WHERE
4
  s.CourseNo IN
5
  (
6
  SELECT
7
    c.CourseNo
8
  FROM
9
    course c,
10
    teacher t
11
  WHERE
12
    c.teacherNo = t.teacherNo
13
    AND
14
    t.name = '叶平'
15
  )
 
 
 
(16)向SC表中插入一些记录,这些记录要求符合以下条件:1、没有上过编号“002”课程的同学学号;2、插入“002”号课程的平均成绩
 

 
 
 
 
 
 
 
1
-- 本题采用插入子查询的方式,三个字段中后两个字段为常量(基本格式:INSERT INTO R(A1, A2 ... ,An) 子查询)
2
INSERT INTO
3
  score(StudentNo, CourseNo, score)
4
(
5
SELECT 
6
  stu.studentNo,
7
  2,
8
  (SELECT AVG(s3.score) FROM score s3 WHERE s3.CourseNo = 2)
9
FROM 
10
  student stu 
11
WHERE 
12
  stu.studentNo 
13
NOT IN 
14
(
15
SELECT 
16
  s2.StudentNo 
17
FROM 
18
  score s2 
19
WHERE 
20
  s2.CourseNo = 2
21
)
22
)
 
 
 
(17)按学号由低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分
 

 
 
 
 
 
 
 
1
-- 用了个极蠢的办法,虽然很瓜很绕但是也算是温习了下相关子查询、CASE WHEN、EXISTS了,另外对自己也有所启发,就留下了
2
-- 然后做到这里的时候感慨,随着练习总是越来越熟练的,尽管自己写得很绕,但以往是根本想不到用什么CASE WHEN、EXISTS之类的,也算是成长吧
3
SELECT
4
  stu.studentNo,
5
  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 "语文",
6
  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 "数学",
7
  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 "英语",
8
  t1.validateCount AS '有效科目数',
9
  t2.validateAVG AS '有效平均分'
10
FROM
11
  student stu,
12
  (SELECT s.studentNo, COUNT(*) AS validateCount FROM score s WHERE s.CourseNo IN (1, 2, 3) GROUP BY s.StudentNo) as t1,
13
  (SELECT s.studentNo, AVG(s.score) AS validateAVG FROM score s WHERE s.CourseNo IN (1, 2, 3) GROUP BY s.StudentNo) as t2
14
WHERE
15
  stu.studentNo = t1.studentNo
16
  AND
17
  stu.studentNo = t2.studentNo
18
ORDER BY stu.StudentNo
 
 
 
 

 
 
 
 
 
 
 
1
-- 另外,参考博客中博主给出的答案如下,比我的就简洁多了,
2
-- 其次,他在这里的有效课程数和有效平均分是针对学生所有的成绩,而并非此处的仅仅三科
3
-- 因为题意也不是很清楚,也就作罢,正好算是两种形式吧
4
SELECT
5
  s.StudentNo,
6
  (SELECT s1.score FROM score s1 WHERE s1.CourseNo=1 AND s1.StudentNo = s.StudentNo) AS "语文",
7
  (SELECT s2.score FROM score s2 WHERE s2.CourseNo=2 AND s2.StudentNo = s.StudentNo) AS "数学",
8
  (SELECT s3.score FROM score s3 WHERE s3.CourseNo=3 AND s3.StudentNo = s.StudentNo) AS "英语",
9
  COUNT(s.CourseNo) AS "有效课程数",
10
  AVG(s.score) AS "有效平均分"
11
FROM
12
  score s
13
GROUP BY s.StudentNo
14
ORDER BY s.StudentNo
 
 
 
(18)查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
 

 
 
 
 
 
 
 
1
SELECT
2
  s.CourseNo,
3
  MAX(s.score),
4
  MIN(s.score)
5
FROM
6
  score s
7
GROUP BY
8
  s.CourseNo
 
 
 
(19)按各科平均成绩从低到高和及格率的百分数从高到低顺序;
 

 
 
 
 
 
 
 
1
-- 看了下上次在Github上写的,不得不说,practice makes perfect
2
SELECT
3
  s.CourseNo,
4
  c.name,
5
  AVG(s.score) AS '平均分',
6
  SUM(CASE WHEN s.score > 60 THEN 1 ELSE 0 END) AS '及格数',
7
  COUNT(*) AS '总数',
8
  SUM(CASE WHEN s.score > 60 THEN 1 ELSE 0 END)/COUNT(*)*100 AS '及格率'
9
FROM
10
  score s,
11
  course c
12
WHERE
13
  s.CourseNo = c.courseNo
14
GROUP BY s.CourseNo
15
ORDER BY AVG(s.score), SUM(CASE WHEN s.score > 60 THEN 1 ELSE 0 END)/COUNT(*) DESC
 
 
 
还不完全,参考原博主,加isnull
 
(20)查询不同老师所教不同课程平均分从高到低显示
 

 
 
 
 
 
 
 
1
SELECT
2
  c1.name,
3
  t1.name,
4
  AVG(s1.score)
5
FROM
6
  score s1,
7
  course c1,
8
  teacher t1
9
WHERE
10
  s1.CourseNo = c1.courseNo
11
  AND
12
  c1.teacherNo = t1.teacherNo
13
GROUP BY s1.CourseNo
14
ORDER BY AVG(s1.score) DESC
 
 
 
(21)统计列印各科成绩,各分数段人数:课程ID,课程名称,(100-85),(85-70,(70-60),( 低于60)
 

 
 
 
 
 
 
 
1
SELECT
2
  c.courseNo AS '课程ID',
3
  c.name AS '课程名称',
4
  SUM(CASE WHEN s.score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS '(100-85)',
5
  SUM(CASE WHEN s.score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS '(85-70)',
6
  SUM(CASE WHEN s.score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS '(70-60)',
7
  SUM(CASE WHEN s.score < 60 THEN 1 ELSE 0 END) AS '(低于60)'
8
FROM
9
  course c,
10
  score s
11
WHERE
12
  c.courseNo = s.CourseNo
13
GROUP BY c.courseNo
 
 
 
(22)查询各科成绩前三名的记录(不考虑成绩并列情况)
 

 
 
 
 
 
 
 
1
SELECT
2
    *
3
FROM
4
  score s
5
WHERE
6
  (
7
    SELECT
8
        COUNT(*)
9
    FROM
10
        score s1
11
    WHERE
12
        s1.CourseNo = s.CourseNo
13
        AND
14
        s1.score > s.score
15
    ) < 3
16
ORDER BY s.CourseNo
 
 
 
(23)查询每门课程被选修的学生数
 

 
 
 
 
 
 
 
1
SELECT
2
  c.name AS '课程',
3
  COUNT(s.StudentNo) AS '选修学生数'
4
FROM
5
  course c LEFT JOIN score s ON c.courseNo = s.CourseNo
6
GROUP BY c.courseNo
 
 
 
(24)查询出只选修了一门课程的全部学生的学号和姓名
 

 
 
 
 
 
 
 
1
SELECT
2
  stu1.StudentNo AS '学号',
3
    stu1.name AS '姓名'
4
FROM
5
  (SELECT StudentNo, COUNT(CourseNo) AS amount FROM score GROUP BY StudentNo) t1,
6
  student stu1
7
WHERE
8
  t1.StudentNo = stu1.studentNo
9
  AND
10
  t1.amount = 1
 
 
 
 

 
 
 
 
 
 
 
1
SELECT
2
  stu1.studentNo AS '学号',
3
  stu1.name AS '姓名'
4
FROM
5
  score s,
6
  student stu1
7
WHERE
8
  s.StudentNo = stu1.studentNo
9
GROUP BY s.StudentNo
10
HAVING COUNT(s.CourseNo) = 1
 
 
 
(25)查询男生、女生的人数
 

 
 
 
 
 
 
 
1
SELECT
2
  s.sex AS '性别',
3
  COUNT(*) AS '人数'
4
FROM
5
  student s
6
GROUP BY s.sex
 
 
 
(26)查询同名同姓学生名单,并统计同名人数
 

 
 
 
 
 
 
 
1
SELECT
2
  s.name AS '姓名',
3
  COUNT(*) AS '学生数'
4
FROM
5
  student s
6
GROUP BY s.name
 
 
 
(27)查询1991年出生的学生名单
 

 
 
 
 
 
 
 
1
SELECT
2
  s.name AS '姓名'
3
FROM
4
  student s
5
WHERE
6
  YEAR(CURDATE()) - s.age = 1991
 
 
 
(28)查询每门课程的平均成绩,结果按平均成绩升序排列
 

 
 
 
 
 
 
 
1
#未考虑到课程无人选修的情况
2
SELECT
3
  c.name AS '课程名称',
4
  AVG(s.score) AS '平均成绩'
5
FROM
6
  score s,
7
  course c
8
WHERE
9
  s.CourseNo = c.courseNo
10
GROUP BY c.courseNo
11
ORDER BY AVG(s.score)
12

13

14

15
#如果某课程无人选修,其平均成绩显示为null
16
SELECT
17
  c.name AS '课程名称',
18
  AVG(s.score) AS '平均成绩'
19
FROM
20
  course c LEFT JOIN score s ON c.courseNo = s.CourseNo
21
GROUP BY c.courseNo  
22
ORDER BY AVG(s.score)
 
 
 
(29)查询平均成绩大于85的所有学生的学号、姓名和平均成绩
 

 
 
 
 
 
 
 
1
SELECT
2
  stu.studentNo AS '学号',
3
  stu.name AS '姓名',
4
  AVG(s.score) AS '平均成绩'
5
FROM
6
  score s,
7
  student stu
8
WHERE
9
  s.StudentNo = stu.studentNo
10
GROUP BY s.StudentNo
11
HAVING AVG(s.score) > 85
 
 
 
(30)查询课程名称为“数学”,且分数低于60的学生姓名和分数
 

 
 
 
 
 
 
 
1
SELECT
2
  stu.name AS '姓名',
3
  s.score AS '数学成绩'
4
FROM
5
  score s,
6
  course c,
7
  student stu
8
WHERE
9
  s.CourseNo = c.courseNo
10
  AND
11
  s.StudentNo = stu.studentNo
12
  AND
13
  c.name = '数学'
14
  AND
15
  s.score < 60
 
 
 
(31)查询所有学生的选课情况
 

 
 
 
 
 
 
 
1
SELECT
2
  stu.name AS '姓名',
3
  c.name AS '选课'
4
FROM
5
  score s,
6
  course c,
7
  student stu
8
WHERE
9
  s.CourseNo = c.courseNo
10
  AND
11
  s.StudentNo = stu.studentNo
12
ORDER BY stu.name
 
 
 
(32)查询任何一门课程成绩在70分以上的姓名、课程名称和分数
 

 
 
 
 
 
 
 
1
SELECT
2
  stu.name AS '姓名',
3
  c.name AS '课程名称',
4
  s.score AS '分数'
5
FROM
6
  score s,
7
  student stu,
8
  course c
9
WHERE
10
  s.StudentNo = stu.studentNo
11
  AND
12
  s.CourseNo = c.courseNo
13
  AND
14
  s.score > 70
 
 
 
(33)查询不及格的课程,并按课程号从大到小排列
 

 
 
 
 
 
 
 
1
#包含不及格记录的课程
2
SELECT DISTINCT
3
  c.courseNo AS '课程号',
4
  c.name AS '课程名称' 
5
FROM
6
  score s,
7
  course c
8
WHERE
9
  s.CourseNo = c.courseNo
10
  AND
11
  s.score < 60
12

13
#不及格的课程的选修记录
14
SELECT
15
  stu.name AS '姓名',
16
  c.name AS '课程名称',
17
  s.score AS '分数'
18
FROM
19
  score s,
20
  student stu,
21
  course c
22
WHERE
23
  s.StudentNo = stu.studentNo
24
  AND
25
  s.CourseNo = c.courseNo
26
  AND
27
  s.score < 60
28
ORDER BY c.courseNo DESC
 
 
 
(34)查询课程编号为003且课程成绩在80分以上的学生的学号和姓名
 

 
 
 
 
 
 
 
1
SELECT
2
  stu.studentNo AS '学号',
3
  stu.name AS '姓名'
4
FROM
5
  score s,
6
  student stu
7
WHERE
8
  s.StudentNo = stu.studentNo
9
  AND
10
  s.CourseNo = 3
11
  AND
12
  s.score > 80
 
 
 
(35)求选了课程的学生人数
 

 
 
 
 
 
 
 
1
#method-1
2
SELECT
3
  COUNT(DISTINCT s.StudentNo) AS '选了课程的学生人数'
4
FROM
5
  score s
6

7
#method-2
8
SELECT
9
  COUNT(*) AS '选了课程的学生人数'
10
FROM
11
  (SELECT * FROM score s GROUP BY s.StudentNo) t
 
 
 
(36)查询选修“杨艳”老师所授课程的学生中,成绩最高的学生姓名及其成绩
 

 
 
 
 
 
 
 
1
SELECT
2
  stu.name AS '学生姓名',
3
  s.score AS '成绩'
4
FROM
5
  score s,
6
  student stu,
7
  course c,
8
  teacher t
9
WHERE
10
  s.StudentNo = stu.studentNo
11
  AND
12
  s.CourseNo = c.courseNo
13
  AND
14
  c.teacherNo = t.teacherNo
15
  AND
16
  t.name = '杨艳'
17
ORDER BY s.score DESC
18
LIMIT 0, 1
 
 
 
(37)查询各个课程及相应的选修人数
 

 
 
 
 
 
 
 
1
SELECT
2
  c.name AS '课程名称',
3
  COUNT(*) AS '选修人数'
4
FROM
5
  score s,
6
  course c
7
WHERE
8
  s.CourseNo = c.courseNo
9
GROUP BY s.CourseNo
 
 
 
(38)查询不同课程但成绩相同的学生的学号、课程号、学生成绩
 

 
 
 
 
 
 
 
1
#method-1
2
SELECT
3
  s.StudentNo AS '学号',
4
  s.CourseNo AS '课程号',
5
  s.score AS '成绩'
6
FROM
7
  score s
8
WHERE
9
  (SELECT COUNT(*) FROM score s1 WHERE s1.score = s.score AND s1.CourseNo <> s.COurseNo) > 0
10
ORDER BY s.score DESC, s.StudentNo, s.CourseNo
11

12
#method-2
13
SELECT DISTINCT
14
  s1.StudentNo AS '学号',
15
  s1.CourseNo AS '课程号',
16
  s1.score AS '成绩'
17
FROM
18
  score s1,
19
  score s2
20
WHERE
21
  s1.score = s2.score 
22
  AND
23
  s1.CourseNo <> s2.CourseNo
24
  ORDER BY s1.score DESC, s1.StudentNo, s1.CourseNo
 
 
 
(39)查询每门课程成绩最好的前两名
 

 
 
 
 
 
 
 
1
SELECT
2
  s.CourseNo AS '课程号',
3
  s.StudentNo AS '学号',
4
  s.score AS '分数'
5

练习题和参考解

(1)查询“001”课程比“002”课程成绩低的所有学生的学号、001学科成绩、002学科成绩
 

 
 
 
 
 
 
 
1
SELECT 
2
  s1.StudentNo,
3
  s1.score AS '001',
4
  s2.score AS '002'
5
FROM
6
  score s1,
7
  (
8
  SELECT
9
    *
10
  FROM
11
    score s
12
  WHERE
13
    s.CourseNo = 2
14
  ) s2
15
WHERE
16
  s1.CourseNo = 1
17
  AND
18
  s1.StudentNo = s2.StudentNo
19
  AND
20
  s1.score < s2.score
21
  ORDER BY s1.StudentNo
 
 
 
 

 
 
 
 
 
 
 
1
SELECT
2
  s1.StudentNo,
3
  s1.score AS '001',
4
  s2.score AS '002'
5
FROM 
6
  score s1, 
7
  score s2
8
WHERE 
9
  s1.CourseNo = 001 
10
  AND 
11
  s2.CourseNo = 002 
12
  AND 
13
  s1.StudentNo = s2.StudentNo
14
  AND
15
  s1.score < s2.score
16
  ORDER BY s1.StudentNo
 
 
 
 
(2)查询平均成绩大于60分的同学的学号和平均成绩
 

 
 
 
 
 
 
 
1
SELECT
2
  s1.StudentNo,
3
  AVG(s1.score)
4
FROM
5
  score s1
6
GROUP BY s1.StudentNo
7
HAVING AVG(s1.score)>60
 
 
 
 
(3)查询所有同学的学号、姓名、选课数、总成绩
 

 
 
 
 
 
 
 
1
SELECT
2
  s1.StudentNo,
3
  stu1.name,
4
  COUNT(*),
5
  SUM(s1.score)
6
FROM
7
  score s1,
8
  student stu1
9
WHERE
10
  s1.StudentNo = stu1.StudentNo
11
GROUP BY s1.StudentNo
 
 
 
 
(4)查询姓“李”的老师的个数
 

 
 
 
 
 
 
 
1
SELECT
2
  COUNT(*)
3
FROM
4
  teacher t1
5
WHERE
6
  t1.name like '李%'
 
 
 
 
(5)查询没学过“叶平”老师课的同学的学号、姓名
 

 
 
 
 
 
 
 
1
SELECT
2
  stu1.StudentNo,
3
  stu1.name
4
FROM
5
  student stu1
6
WHERE
7
  stu1.StudentNo NOT IN
8
  (
9
  SELECT DISTINCT
10
    s1.StudentNo
11
  FROM
12
    score s1,
13
    course c1,
14
    teacher t1
15
  WHERE
16
    s1.courseNo = c1.CourseNo
17
    AND
18
    c1.teacherNo = t1.teacherNo
19
    AND
20
    t1.name = '叶平'
21
  )
 
 
 
(6)查询学过“001”并且也学过编号“002”课程的同学的学号、姓名
 

 
 
 
 
 
 
 
1
-- 这个算法比普通的要有想法
2
SELECT
3
  s1.StudentNo,
4
  stu1.name
5
FROM
6
  score s1,
7
  student stu1
8
WHERE
9
  s1.StudentNo = stu1.StudentNo
10
  AND
11
  s1.CourseNo IN (1, 2)
12
GROUP BY s1.StudentNo
13
HAVING COUNT(*) = 2
 
 
 
 

 
 
 
 
 
 
 
1
SELECT
2
  s1.StudentNo,
3
  stu1.name
4
FROM
5
  score s1,
6
  student stu1
7
WHERE
8
  s1.StudentNo = stu1.StudentNo
9
  AND
10
  s1.CourseNo = 1
11
  AND
12
  s1.StudentNo IN
13
  (
14
  SELECT
15
    s2.StudentNo
16
  FROM
17
    score s2
18
  WHERE
19
    s2.CourseNo = 2
20
  )
 
 
 
(7)查询学过“叶平”老师所教的所有课的同学的学号、姓名
 

 
 
 
 
 
 
 
1
-- 利用主键值不相同,它们的和一定各不相同,叶平老师的课程的主键值和如果与学生所学的叶平老师的课程的主键值和相等,那么说明学了叶平老师所有课程
2
SELECT
3
  stu1.StudentNo,
4
  stu1.name
5
FROM
6
  score s1,
7
  student stu1,
8
  course c1,
9
  teacher t1
10
WHERE
11
  s1.StudentNo = stu1.StudentNo
12
  AND
13
  s1.CourseNo = c1.CourseNo
14
  AND
15
  c1.teacherNo = t1.teacherNo
16
  AND
17
  t1.name = '叶平'
18
GROUP BY s1.StudentNo
19
HAVING SUM(s1.CourseNo)=
20
(
21
SELECT
22
  SUM(c2.CourseNo)
23
FROM
24
  course c2,
25
  teacher t2
26
WHERE
27
  c2.teacherNo = t2.teacherNo
28
  AND
29
  t2.name = '叶平'
30
)
 
 
 
 

 
 
 
 
 
 
 
1
-- 如果学生学习叶平老师的课程数量,与叶平老师所教学课程的数量相同,那么说明该同学学了叶平老师的所有课程
2
SELECT
3
  stu1.StudentNo,
4
  stu1.name
5
FROM
6
  score s1,
7
  student stu1,
8
  course c1,
9
  teacher t1
10
WHERE
11
  s1.StudentNo = stu1.StudentNo
12
  AND
13
  s1.CourseNo = c1.CourseNo
14
  AND
15
  c1.teacherNo = t1.teacherNo
16
  AND
17
  t1.name = '叶平'
18
GROUP BY s1.StudentNo
19
HAVING COUNT(*) =
20
(
21
SELECT
22
  COUNT(*)
23
FROM
24
  course c2,
25
  teacher t2
26
WHERE
27
  c2.teacherNo = t2.teacherNo
28
  AND
29
  t2.name = '叶平' 
30
)
 
 
 
(8)查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名
 

 
 
 
 
 
 
 
1
SELECT
2
  stu1.studentNo,
3
  stu1.name
4
FROM
5
  score s1,
6
  (
7
  SELECT
8
    s2.StudentNo,
9
    s2.score
10
  FROM
11
    score s2
12
  WHERE
13
    s2.CourseNo = 1
14
  ) t2,
15
  student stu1
16
WHERE
17
  s1.CourseNo = 2
18
  AND 
19
  s1.StudentNo = t2.StudentNo
20
  AND 
21
  s1.score < t2.score
22
  AND 
23
  s1.StudentNo = stu1.studentNo
 
 
 
(9)查询有课程成绩小于60分的同学的学号、姓名
 

 
 
 
 
 
 
 
1
SELECT DISTINCT
2
  s1.StudentNo,
3
  stu1.name
4
FROM
5
  score s1,
6
  student stu1
7
WHERE
8
  s1.StudentNo = stu1.studentNo
9
  AND
10
  s1.score < 60
 
 
 
(10)查询没有学全所有课的同学的学号、姓名
 

 
 
 
 
 
 
 
1
SELECT
2
  stu1.StudentNo,
3
  stu1.name
4
FROM
5
  score s1,
6
  student stu1
7
WHERE
8
  s1.StudentNo = stu1.StudentNo
9
GROUP BY s1.StudentNo
10
HAVING COUNT(*) < 
11
(
12
SELECT
13
  COUNT(*)
14
FROM
15
  course c1
16
)
 
 
 
(11)查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名
 

 
 
 
 
 
 
 
1
SELECT DISTINCT
2
  stu1.StudentNo,
3
  stu1.name
4
FROM
5
  score s1,
6
  student stu1
7
WHERE
8
  s1.StudentNo = stu1.StudentNo
9
  AND
10
  s1.CourseNo IN
11
  (
12
  SELECT
13
    s2.CourseNo
14
  FROM
15
    score s2
16
  WHERE
17
    s2.StudentNo = 1
18
  )
 
 
 
(12)查询至少学过学号为“001”同学所有一门课的其他同学学号和姓名(和11题撞脸,排除1号同学就可以了)
 

 
 
 
 
 
 
 
1
SELECT DISTINCT
2
  stu1.StudentNo,
3
  stu1.name
4
FROM
5
  score s1,
6
  student stu1
7
WHERE
8
  s1.StudentNo = stu1.StudentNo
9
  AND
10
  s1.StudentNo != 1
11
  AND
12
  s1.CourseNo IN
13
  (
14
  SELECT
15
    s2.CourseNo
16
  FROM
17
    score s2
18
  WHERE
19
    s2.StudentNo = 1
20
  )
 
 
 
(13)把“score”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩
 

 
 
 
 
 
 
 
1
-- 涉及将两表联合,将本表某字段的值按条件设置为另个表的某个字段的值 (参考链接:MySQL:把一个表中的数据按键值更新(update)到另一个表)
2
UPDATE
3
  score s,
4
  (
5
  SELECT
6
    s1.CourseNo as courseNo,
7
    AVG(s1.score) as avgScore 
8
  FROM
9
    score s1,
10
    course c1,
11
    teacher t1
12
  WHERE
13
    s1.CourseNo = c1.CourseNo
14
    AND
15
    c1.teacherNo = t1.teacherNo
16
    AND
17
    t1.name = '叶平'
18
  GROUP BY s1.CourseNo
19
  ) as t
20
SET
21
  s.score = t.avgScore
22
WHERE
23
  s.CourseNo = t.courseNo
 
 
 
(14)查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名
 

 
 
 
 
 
 
 
1
SELECT
2
  stu.studentNo,
3
  stu.name
4
FROM
5
  score s,
6
  student stu
7
WHERE
8
  s.StudentNo != 2
9
  AND
10
  s.StudentNo = stu.studentNo
11
GROUP BY s.StudentNo
12
HAVING SUM(s.CourseNo)=
13
(
14
SELECT
15
  SUM(s1.CourseNo)
16
FROM
17
  score s1
18
WHERE
19
  s1.StudentNo = 2
20
)
 
 
 
(15)删除学习“叶平”老师课的SC表记录
 

 
 
 
 
 
 
 
1
DELETE FROM
2
  score s
3
WHERE
4
  s.CourseNo IN
5
  (
6
  SELECT
7
    c.CourseNo
8
  FROM
9
    course c,
10
    teacher t
11
  WHERE
12
    c.teacherNo = t.teacherNo
13
    AND
14
    t.name = '叶平'
15
  )
 
 
 
(16)向SC表中插入一些记录,这些记录要求符合以下条件:1、没有上过编号“002”课程的同学学号;2、插入“002”号课程的平均成绩
 

 
 
 
 
 
 
 
1
-- 本题采用插入子查询的方式,三个字段中后两个字段为常量(基本格式:INSERT INTO R(A1, A2 ... ,An) 子查询)
2
INSERT INTO
3
  score(StudentNo, CourseNo, score)
4
(
5
SELECT 
6
  stu.studentNo,
7
  2,
8
  (SELECT AVG(s3.score) FROM score s3 WHERE s3.CourseNo = 2)
9
FROM 
10
  student stu 
11
WHERE 
12
  stu.studentNo 
13
NOT IN 
14
(
15
SELECT 
16
  s2.StudentNo 
17
FROM 
18
  score s2 
19
WHERE 
20
  s2.CourseNo = 2
21
)
22
)
 
 
 
(17)按学号由低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分
 

 
 
 
 
 
 
 
1
-- 用了个极蠢的办法,虽然很瓜很绕但是也算是温习了下相关子查询、CASE WHEN、EXISTS了,另外对自己也有所启发,就留下了
2
-- 然后做到这里的时候感慨,随着练习总是越来越熟练的,尽管自己写得很绕,但以往是根本想不到用什么CASE WHEN、EXISTS之类的,也算是成长吧
3
SELECT
4
  stu.studentNo,
5
  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 "语文",
6
  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 "数学",
7
  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 "英语",
8
  t1.validateCount AS '有效科目数',
9
  t2.validateAVG AS '有效平均分'
10
FROM
11
  student stu,
12
  (SELECT s.studentNo, COUNT(*) AS validateCount FROM score s WHERE s.CourseNo IN (1, 2, 3) GROUP BY s.StudentNo) as t1,
13
  (SELECT s.studentNo, AVG(s.score) AS validateAVG FROM score s WHERE s.CourseNo IN (1, 2, 3) GROUP BY s.StudentNo) as t2
14
WHERE
15
  stu.studentNo = t1.studentNo
16
  AND
17
  stu.studentNo = t2.studentNo
18
ORDER BY stu.StudentNo
 
 
 
 

 
 
 
 
 
 
 
1
-- 另外,参考博客中博主给出的答案如下,比我的就简洁多了,
2
-- 其次,他在这里的有效课程数和有效平均分是针对学生所有的成绩,而并非此处的仅仅三科
3
-- 因为题意也不是很清楚,也就作罢,正好算是两种形式吧
4
SELECT
5
  s.StudentNo,
6
  (SELECT s1.score FROM score s1 WHERE s1.CourseNo=1 AND s1.StudentNo = s.StudentNo) AS "语文",
7
  (SELECT s2.score FROM score s2 WHERE s2.CourseNo=2 AND s2.StudentNo = s.StudentNo) AS "数学",
8
  (SELECT s3.score FROM score s3 WHERE s3.CourseNo=3 AND s3.StudentNo = s.StudentNo) AS "英语",
9
  COUNT(s.CourseNo) AS "有效课程数",
10
  AVG(s.score) AS "有效平均分"
11
FROM
12
  score s
13
GROUP BY s.StudentNo
14
ORDER BY s.StudentNo
 
 
 
(18)查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
 

 
 
 
 
 
 
 
1
SELECT
2
  s.CourseNo,
3
  MAX(s.score),
4
  MIN(s.score)
5
FROM
6
  score s
7
GROUP BY
8
  s.CourseNo
 
 
 
(19)按各科平均成绩从低到高和及格率的百分数从高到低顺序;
 

 
 
 
 
 
 
 
1
-- 看了下上次在Github上写的,不得不说,practice makes perfect
2
SELECT
3
  s.CourseNo,
4
  c.name,
5
  AVG(s.score) AS '平均分',
6
  SUM(CASE WHEN s.score > 60 THEN 1 ELSE 0 END) AS '及格数',
7
  COUNT(*) AS '总数',
8
  SUM(CASE WHEN s.score > 60 THEN 1 ELSE 0 END)/COUNT(*)*100 AS '及格率'
9
FROM
10
  score s,
11
  course c
12
WHERE
13
  s.CourseNo = c.courseNo
14
GROUP BY s.CourseNo
15
ORDER BY AVG(s.score), SUM(CASE WHEN s.score > 60 THEN 1 ELSE 0 END)/COUNT(*) DESC
 
 
 
还不完全,参考原博主,加isnull
 
(20)查询不同老师所教不同课程平均分从高到低显示
 

 
 
 
 
 
 
 
1
SELECT
2
  c1.name,
3
  t1.name,
4
  AVG(s1.score)
5
FROM
6
  score s1,
7
  course c1,
8
  teacher t1
9
WHERE
10
  s1.CourseNo = c1.courseNo
11
  AND
12
  c1.teacherNo = t1.teacherNo
13
GROUP BY s1.CourseNo
14
ORDER BY AVG(s1.score) DESC
 
 
 
(21)统计列印各科成绩,各分数段人数:课程ID,课程名称,(100-85),(85-70,(70-60),( 低于60)
 

 
 
 
 
 
 
 
1
SELECT
2
  c.courseNo AS '课程ID',
3
  c.name AS '课程名称',
4
  SUM(CASE WHEN s.score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS '(100-85)',
5
  SUM(CASE WHEN s.score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS '(85-70)',
6
  SUM(CASE WHEN s.score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS '(70-60)',
7
  SUM(CASE WHEN s.score < 60 THEN 1 ELSE 0 END) AS '(低于60)'
8
FROM
9
  course c,
10
  score s
11
WHERE
12
  c.courseNo = s.CourseNo
13
GROUP BY c.courseNo
 
 
 
(22)查询各科成绩前三名的记录(不考虑成绩并列情况)
 

 
 
 
 
 
 
 
1
SELECT
2
    *
3
FROM
4
  score s
5
WHERE
6
  (
7
    SELECT
8
        COUNT(*)
9
    FROM
10
        score s1
11
    WHERE
12
        s1.CourseNo = s.CourseNo
13
        AND
14
        s1.score > s.score
15
    ) < 3
16
ORDER BY s.CourseNo
 
 
 
(23)查询每门课程被选修的学生数
 

 
 
 
 
 
 
 
1
SELECT
2
  c.name AS '课程',
3
  COUNT(s.StudentNo) AS '选修学生数'
4
FROM
5
  course c LEFT JOIN score s ON c.courseNo = s.CourseNo
6
GROUP BY c.courseNo
 
 
 
(24)查询出只选修了一门课程的全部学生的学号和姓名
 

 
 
 
 
 
 
 
1
SELECT
2
  stu1.StudentNo AS '学号',
3
    stu1.name AS '姓名'
4
FROM
5
  (SELECT StudentNo, COUNT(CourseNo) AS amount FROM score GROUP BY StudentNo) t1,
6
  student stu1
7
WHERE
8
  t1.StudentNo = stu1.studentNo
9
  AND
10
  t1.amount = 1
 
 
 
 

 
 
 
 
 
 
 
1
SELECT
2
  stu1.studentNo AS '学号',
3
  stu1.name AS '姓名'
4
FROM
5
  score s,
6
  student stu1
7
WHERE
8
  s.StudentNo = stu1.studentNo
9
GROUP BY s.StudentNo
10
HAVING COUNT(s.CourseNo) = 1
 
 
 
(25)查询男生、女生的人数
 

 
 
 
 
 
 
 
1
SELECT
2
  s.sex AS '性别',
3
  COUNT(*) AS '人数'
4
FROM
5
  student s
6
GROUP BY s.sex
 
 
 
(26)查询同名同姓学生名单,并统计同名人数
 

 
 
 
 
 
 
 
1
SELECT
2
  s.name AS '姓名',
3
  COUNT(*) AS '学生数'
4
FROM
5
  student s
6
GROUP BY s.name
 
 
 
(27)查询1991年出生的学生名单
 

 
 
 
 
 
 
 
1
SELECT
2
  s.name AS '姓名'
3
FROM
4
  student s
5
WHERE
6
  YEAR(CURDATE()) - s.age = 1991
 
 
 
(28)查询每门课程的平均成绩,结果按平均成绩升序排列
 

 
 
 
 
 
 
 
1
#未考虑到课程无人选修的情况
2
SELECT
3
  c.name AS '课程名称',
4
  AVG(s.score) AS '平均成绩'
5
FROM
6
  score s,
7
  course c
8
WHERE
9
  s.CourseNo = c.courseNo
10
GROUP BY c.courseNo
11
ORDER BY AVG(s.score)
12

13

14

15
#如果某课程无人选修,其平均成绩显示为null
16
SELECT
17
  c.name AS '课程名称',
18
  AVG(s.score) AS '平均成绩'
19
FROM
20
  course c LEFT JOIN score s ON c.courseNo = s.CourseNo
21
GROUP BY c.courseNo  
22
ORDER BY AVG(s.score)
 
 
 
(29)查询平均成绩大于85的所有学生的学号、姓名和平均成绩
 

 
 
 
 
 
 
 
1
SELECT
2
  stu.studentNo AS '学号',
3
  stu.name AS '姓名',
4
  AVG(s.score) AS '平均成绩'
5
FROM
6
  score s,
7
  student stu
8
WHERE
9
  s.StudentNo = stu.studentNo
10
GROUP BY s.StudentNo
11
HAVING AVG(s.score) > 85
 
 
 
(30)查询课程名称为“数学”,且分数低于60的学生姓名和分数
 

 
 
 
 
 
 
 
1
SELECT
2
  stu.name AS '姓名',
3
  s.score AS '数学成绩'
4
FROM
5
  score s,
6
  course c,
7
  student stu
8
WHERE
9
  s.CourseNo = c.courseNo
10
  AND
11
  s.StudentNo = stu.studentNo
12
  AND
13
  c.name = '数学'
14
  AND
15
  s.score < 60
 
 
 
(31)查询所有学生的选课情况
 

 
 
 
 
 
 
 
1
SELECT
2
  stu.name AS '姓名',
3
  c.name AS '选课'
4
FROM
5
  score s,
6
  course c,
7
  student stu
8
WHERE
9
  s.CourseNo = c.courseNo
10
  AND
11
  s.StudentNo = stu.studentNo
12
ORDER BY stu.name
 
 
 
(32)查询任何一门课程成绩在70分以上的姓名、课程名称和分数
 

 
 
 
 
 
 
 
1
SELECT
2
  stu.name AS '姓名',
3
  c.name AS '课程名称',
4
  s.score AS '分数'
5
FROM
6
  score s,
7
  student stu,
8
  course c
9
WHERE
10
  s.StudentNo = stu.studentNo
11
  AND
12
  s.CourseNo = c.courseNo
13
  AND
14
  s.score > 70
 
 
 
(33)查询不及格的课程,并按课程号从大到小排列
 

 
 
 
 
 
 
 
1
#包含不及格记录的课程
2
SELECT DISTINCT
3
  c.courseNo AS '课程号',
4
  c.name AS '课程名称' 
5
FROM
6
  score s,
7
  course c
8
WHERE
9
  s.CourseNo = c.courseNo
10
  AND
11
  s.score < 60
12

13
#不及格的课程的选修记录
14
SELECT
15
  stu.name AS '姓名',
16
  c.name AS '课程名称',
17
  s.score AS '分数'
18
FROM
19
  score s,
20
  student stu,
21
  course c
22
WHERE
23
  s.StudentNo = stu.studentNo
24
  AND
25
  s.CourseNo = c.courseNo
26
  AND
27
  s.score < 60
28
ORDER BY c.courseNo DESC
 
 
 
(34)查询课程编号为003且课程成绩在80分以上的学生的学号和姓名
 

 
 
 
 
 
 
 
1
SELECT
2
  stu.studentNo AS '学号',
3
  stu.name AS '姓名'
4
FROM
5
  score s,
6
  student stu
7
WHERE
8
  s.StudentNo = stu.studentNo
9
  AND
10
  s.CourseNo = 3
11
  AND
12
  s.score > 80
 
 
 
(35)求选了课程的学生人数
 

 
 
 
 
 
 
 
1
#method-1
2
SELECT
3
  COUNT(DISTINCT s.StudentNo) AS '选了课程的学生人数'
4
FROM
5
  score s
6

7
#method-2
8
SELECT
9
  COUNT(*) AS '选了课程的学生人数'
10
FROM
11
  (SELECT * FROM score s GROUP BY s.StudentNo) t
 
 
 
(36)查询选修“杨艳”老师所授课程的学生中,成绩最高的学生姓名及其成绩
 

 
 
 
 
 
 
 
1
SELECT
2
  stu.name AS '学生姓名',
3
  s.score AS '成绩'
4
FROM
5
  score s,
6
  student stu,
7
  course c,
8
  teacher t
9
WHERE
10
  s.StudentNo = stu.studentNo
11
  AND
12
  s.CourseNo = c.courseNo
13
  AND
14
  c.teacherNo = t.teacherNo
15
  AND
16
  t.name = '杨艳'
17
ORDER BY s.score DESC
18
LIMIT 0, 1
 
 
 
(37)查询各个课程及相应的选修人数
 

 
 
 
 
 
 
 
1
SELECT
2
  c.name AS '课程名称',
3
  COUNT(*) AS '选修人数'
4
FROM
5
  score s,
6
  course c
7
WHERE
8
  s.CourseNo = c.courseNo
9
GROUP BY s.CourseNo
 
 
 
(38)查询不同课程但成绩相同的学生的学号、课程号、学生成绩
 

 
 
 
 
 
 
 
1
#method-1
2
SELECT
3
  s.StudentNo AS '学号',
4
  s.CourseNo AS '课程号',
5
  s.score AS '成绩'
6
FROM
7
  score s
8
WHERE
9
  (SELECT COUNT(*) FROM score s1 WHERE s1.score = s.score AND s1.CourseNo <> s.COurseNo) > 0
10
ORDER BY s.score DESC, s.StudentNo, s.CourseNo
11

12
#method-2
13
SELECT DISTINCT
14
  s1.StudentNo AS '学号',
15
  s1.CourseNo AS '课程号',
16
  s1.score AS '成绩'
17
FROM
18
  score s1,
19
  score s2
20
WHERE
21
  s1.score = s2.score 
22
  AND
23
  s1.CourseNo <> s2.CourseNo
24
  ORDER BY s1.score DESC, s1.StudentNo, s1.CourseNo
 
 
 
(39)查询每门课程成绩最好的前两名
 

 
 
 
 
 
 
 
1
SELECT
2
  s.CourseNo AS '课程号',
3
  s.StudentNo AS '学号',
4
  s.score AS '分数'
5
FROM
6
  score s
7
WHERE
8
  (SELECT COUNT(*) FROM score s1 WHERE s1.CourseNo = s.CourseNo AND s1.score > s.score) < 2
9
ORDER BY s.CourseNo
FROM
6
  score s
7
WHERE
8
  (SELECT COUNT(*) FROM score s1 WHERE s1.CourseNo = s.CourseNo AND s1.score > s.score) < 2
9
ORDER BY s.CourseNo

sql 摘抄的更多相关文章

  1. oracle sql 语句 示例

    --oracle 用户对象的导入导出 exp devimage/oracle@172.xx.x.xx/TESTDB owner='devimage' file=d:/devimage.dmp log= ...

  2. 解开发者之痛:中国移动MySQL数据库优化最佳实践(转)

    开源数据库MySQL比较容易碰到性能瓶颈,为此经常需要对MySQL数据库进行优化,而MySQL数据库优化需要运维DBA与相关开发共同参与,其中MySQL参数及服务器配置优化主要由运维DBA完成,开发则 ...

  3. 中国移动MySQL数据库优化最佳实践

    原创 2016-08-12 章颖 DBAplus社群 本文根据DBAplus社群第69期线上分享整理而成,文末还有书送哦~ 讲师介绍章颖 数据研发工程师 现任中国移动杭州研发中心数据研发工程师,擅长M ...

  4. mysql数据库优化原则

    一.一个例子 数据库需要处理的行数: 189444*1877*13482~~~479亿 如果在关联字段上加上合适的索引: 数据库需要处理的行数:368006*1*3*1~~~110万 MySQL通常是 ...

  5. 防止 SQL 注入的方法(摘抄)

    ——选自<深入Ajax : 架构与最佳实践 = Advanced Ajax : architecture and best practices/ (美)Shawn M.Lauriat著:张过,宋 ...

  6. [摘抄] 为什么 Linq 可以高效率查询 SQL ?

    From C# in Depth(3rd) - Jon Skeet using (LinqDemoDataContext db = new LinqDemoDataContext()) { var f ...

  7. SQL server 查询语句优先级-摘抄

    SQL 不同于与其他编程语言的最明显特征是处理代码的顺序.在大数编程语言中,代码按编码顺序被处理,但是在SQL语言中,第一个被处理的子句是FROM子句,尽管SELECT语句第一个出现,但是几乎总是最后 ...

  8. SQL 全角半角转换-(摘抄)

    /****** SQL转换全角/半角函数 开始******/ CREATE FUNCTION ConvertWordAngle ( @str NVARCHAR(4000), --要转换的字符串 @fl ...

  9. sql分页汇总-摘抄自网络

    文章:几种常见SQL分页方式效率比较 个人倾向于:(2005以上版本支持 row_number()) select * from ( select row_number()over(order by ...

随机推荐

  1. 如何改变string中的字符值?

    string本身是不可变的,因此要改变string中字符,需要如下操作: str := “hello world” s := []byte(str) s[] = ‘o’ str = string(s) ...

  2. struts2中的Action实现的三种方式

    Action类创建方式有哪些? 方式一:直接创建一个类,可以是POJO,即原生Java类,没有继承任何类,也没有实现任何接口 这种方式使得strust2框架的代码侵入性更低,但是这种方式是理想状态,开 ...

  3. 分组函数 partition by 的详解,与order by 区别

    partition  by关键字是分析性函数的一部分,它和聚合函数(如group by)不同的地方在于它能返回一个分组中的多条记录,而聚合函数一般只有一条反映统计值的记录, partition  by ...

  4. day07作业猜年龄游戏

    # 给定年龄,用户可以猜三次年龄 # # 年龄猜对,让用户选择两次奖励 # # 用户选择两次奖励后退出 get_prize_dict = {} # 获取的奖品信息 age = 18 inp_count ...

  5. controler--application配置

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  6. 从React Native到微服务,落地一个全栈解决方案

    Poplar是一个社交主题的内容社区,但自身并不做社区,旨在提供可快速二次开发的开源基础套件.前端基于React Native与Redux构建,后端由Spring Boot.Dubbo.Zookeep ...

  7. 15 个最佳 jQuery 翻书效果插件

    本文为你带来15个非常实用的.实现类似翻书效果的jQuery插件,你可以很容易地整合到你的web应用中,提升用户体验. 1.  BookBlock BookBlock可以将任何内容(如图像.文本)创建 ...

  8. HTTP协议之-URL

    一.URL url统一资源定位符 1.URL格式: 方案.就是指所使用的协议,类似http:// 服务器的地址,类似i.cnblogs.com/ 制定web服务器的某个资源路径 方案://服务器位置/ ...

  9. python中递归函数

    python中的 递归函数,是指的是函数在函数内部调用自己的函数 需要满足两个条件,一,需要有一个明确的终止条件 二,需要函数自己在内部调用自己

  10. laravel框架手动删除迁移文件后再次创建报错

    手动删除laravel框架数据表迁移文件后再次创建报错 如下图: 执行创建操作之后会在autoload_static.php及autoload_classmap.php这两个文件中添加迁移文件的目录. ...