目录

1.表结构

2.创建表和插入数据

3.习题

1.表结构

2.建表和插入数据

  1. # 创建班级表
  2. create table class(
  3. cid int primary key auto_increment,
  4. caption varchar(32) not null
  5. );
  6.  
  7. # 创建学生表
  8. create table student(
  9. sid int primary key auto_increment,
  10. gender char(1) not null,
  11. class_id int not null,
  12. sname varchar(32) not null,
  13. foreign key(class_id) references class(cid) on delete cascade on update cascade
  14. );
  15.  
  16. # 创建老师表
  17. create table teacher(
  18. tid int primary key auto_increment,
  19. tname varchar(32) not null
  20. );
  21.  
  22. # 创建课程表
  23. create table course(
  24. cid int primary key auto_increment,
  25. cname varchar(32) not null,
  26. teacher_id int not null,
  27. foreign key(teacher_id) references teacher(tid) on delete cascade on update cascade
  28. );
  29.  
  30. # 创建成绩表
  31. create table score(
  32. sid int primary key auto_increment,
  33. student_id int not null,
  34. course_id int not null,
  35. num int not null,
  36. foreign key(student_id) references student(sid) on delete cascade on update cascade,
  37. foreign key(course_id) references course(cid) on delete cascade on update cascade
  38. );
  39.  
  40. # 班级表插入记录
  41. insert into class values
  42. ('', '三年二班'),
  43. ('', '三年三班'),
  44. ('', '一年二班'),
  45. ('', '二年一班');
  46.  
  47. # 学生表插入记录
  48. insert into student values
  49. ('', '男', '', '理解'),
  50. ('', '女', '', '钢蛋'),
  51. ('', '男', '', '张三'),
  52. ('', '男', '', '张一'),
  53. ('', '女', '', '张二'),
  54. ('', '男', '', '张四'),
  55. ('', '女', '', '铁锤'),
  56. ('', '男', '', '李三'),
  57. ('', '男', '', '李一'),
  58. ('', '女', '', '李二'),
  59. ('', '男', '', '李四'),
  60. ('', '女', '', '如花'),
  61. ('', '男', '', '刘三'),
  62. ('', '男', '', '刘一'),
  63. ('', '女', '', '刘二'),
  64. ('', '男', '', '刘四');
  65.  
  66. # 老师表插入记录
  67. insert into teacher values
  68. ('', '张磊'),
  69. ('', '李平'),
  70. ('', '刘海燕'),
  71. ('', '朱云海'),
  72. ('', '李春秋');
  73.  
  74. # 课程表插入记录
  75. insert into course values
  76. ('', '生物', ''),
  77. ('', '物理', ''),
  78. ('', '体育', ''),
  79. ('', '美术', '');
  80.  
  81. # 成绩表插入记录
  82. insert into score values
  83. ('', '', '', ''),
  84. ('', '', '', ''),
  85. ('', '', '', ''),
  86. ('', '', '', ''),
  87. ('', '', '', ''),
  88. ('', '', '', ''),
  89. ('', '', '', ''),
  90. ('', '', '', ''),
  91. ('', '', '', ''),
  92. ('', '', '', ''),
  93. ('', '', '', ''),
  94. ('', '', '', ''),
  95. ('', '', '', ''),
  96. ('', '', '', ''),
  97. ('', '', '', ''),
  98. ('', '', '', ''),
  99. ('', '', '', ''),
  100. ('', '', '', ''),
  101. ('', '', '', ''),
  102. ('', '', '', ''),
  103. ('', '', '', ''),
  104. ('', '', '', ''),
  105. ('', '', '', ''),
  106. ('', '', '', ''),
  107. ('', '', '', ''),
  108. ('', '', '', ''),
  109. ('', '', '', ''),
  110. ('', '', '', ''),
  111. ('', '', '', ''),
  112. ('', '', '', ''),
  113. ('', '', '', ''),
  114. ('', '', '', ''),
  115. ('', '', '', ''),
  116. ('', '', '', ''),
  117. ('', '', '', ''),
  118. ('', '', '', ''),
  119. ('', '', '', ''),
  120. ('', '', '', ''),
  121. ('', '', '', ''),
  122. ('', '', '', ''),
  123. ('', '', '', ''),
  124. ('', '', '', ''),
  125. ('', '', '', ''),
  126. ('', '', '', ''),
  127. ('', '', '', ''),
  128. ('', '', '', ''),
  129. ('', '', '', ''),
  130. ('', '', '', '');

3.习题

1、查询所有的课程的名称以及对应的任课老师姓名

  1. # 1、查询所有的课程的名称以及对应的任课老师姓名
  2. # where
  3. select
  4. course.cname,teacher.tname
  5. from
  6. teacher,course
  7. where
  8. teacher.tid = course.teacher_id
  9.  
  10. # inner join
  11. select
  12. course.cname,teacher.tname
  13. from
  14. teacher inner join course on teacher.tid = course.teacher_id

2、查询学生表中男女生各有多少人

  1. # 2、查询学生表中男女生各有多少人
  2. select
  3. gender,count(*)
  4. from
  5. student
  6. group by
  7. gender

3、查询物理成绩等于100的学生的姓名

  1. # 3、查询物理成绩等于100的学生的姓名
  2. # where
  3. select
  4. student.sname , score.num
  5. from
  6. score,student,course
  7. where
  8. score.student_id = student.sid
  9. and
  10. score.course_id = course.cid
  11. and
  12. course.cname = "物理"
  13. and
  14. score.num = 100
  15.  
  16. # inner join
  17. select
  18. student.sname , score.num
  19. from
  20. score inner join student on score.student_id = student.sid
  21. inner join course on score.course_id = course.cid
  22. where
  23. course.cname = "物理"
  24. and
  25. score.num = 100

4、查询平均成绩大于八十分的同学的姓名和平均成绩

  1. # where 写法
  2. select
  3. student_id,student.sname,avg(num)
  4. from
  5. score,student
  6. where
  7. score.student_id = student.sid
  8. group by
  9. student_id
  10. having
  11. avg(num) > 80
  12.  
  13. # inner join
  14. select
  15. student_id,student.sname,avg(num)
  16. from
  17. score inner join student on score.student_id = student.sid
  18. group by
  19. student_id
  20. having
  21. avg(num) > 80

5、查询所有学生的学号,姓名,选课数,总成绩3

  1. # 选课数
  2. select
  3. student_id,count(*)
  4. from
  5. score
  6. group by
  7. student_id
  8.  
  9. # 总成绩
  10. select
  11. student_id,sum(num)
  12. from
  13. score
  14. group by
  15. student_id
  16.  
  17. # where
  18. select
  19. student_id,sname,count(*),sum(num)
  20. from
  21. score,student
  22. where
  23. score.student_id = student.sid
  24. group by
  25. student_id
  26.  
  27. # inner join
  28. select
  29. student_id,sname,count(*),sum(num)
  30. from
  31. score inner join student on score.student_id = student.sid
  32. group by
  33. student_id
  34.  
  35. # ( 附加所有学生 )
  36. # right join
  37. select
  38. student.sid,sname,count(score.course_id),sum(num)
  39. from
  40. score right join student on score.student_id = student.sid
  41. group by
  42. student.sid
  43.  
  44. # left join
  45. select
  46. student.sid,sname,count(score.course_id),sum(num)
  47. from
  48. student left join score on score.student_id = student.sid
  49. group by
  50. student.sid

6、 查询姓李老师的个数

  1. # 6、 查询姓李老师的个数
  2. select
  3. count(*)
  4. from
  5. teacher
  6. where
  7. tname like '李%'

7、 查询没有报李平老师课的学生姓名

  1. # 7、 查询没有报李平老师课的学生姓名
  2. # 1.报了李平老师课程的学生id是?
  3. """distinct 去重
  4. distinct student_id ok
  5. distinct(student_id) ok
  6. """
  7. select
  8. distinct( student_id )
  9. from
  10. teacher,course,score
  11. where
  12. teacher.tid = course.teacher_id
  13. and
  14. course.cid = score.course_id
  15. and
  16. teacher.tname = "李平"
  17.  
  18. # 2.查询学生表,除了这个id的剩下的就是没有报李平老师课程的
  19. select
  20. student.sname
  21. from
  22. student
  23. where
  24. sid not in (1号数据)
  25.  
  26. #3.综合拼接
  27. select
  28. student.sname
  29. from
  30. student
  31. where
  32. sid not in (select
  33. distinct( student_id )
  34. from
  35. teacher,course,score
  36. where
  37. teacher.tid = course.teacher_id
  38. and
  39. course.cid = score.course_id
  40. and
  41. teacher.tname = "李平")

8、 查询物理课程的分数比生物课程的分数高的学生的学号

  1. # 8、 查询物理课程的分数比生物课程的分数高的学生的学号
  2.  
  3. # 1.物理课程学生分数
  4. select
  5. score.student_id as t1_id , score.num as num , course.cid ,course.cname
  6. from
  7. course inner join score on course.cid = score.course_id
  8. where
  9. course.cname = "物理"
  10.  
  11. # 2.生物课程学生分数
  12. select
  13. score.student_id as t2_id , score.num as num , course.cid ,course.cname
  14. from
  15. course inner join score on course.cid = score.course_id
  16. where
  17. course.cname = "生物"
  18.  
  19. # 综合拼接
  20. """
  21. # 格式
  22. select
  23. t1.t1_id
  24. from
  25. (1) inner join (2) on 1.student_id = 2.student_id
  26. where
  27. 1.num > 2.num
  28. """
  29. select
  30. t1.t1_id
  31. from
  32. (select
  33. score.student_id as t1_id , score.num as num , course.cid ,course.cname
  34. from
  35. course inner join score on course.cid = score.course_id
  36. where
  37. course.cname = "物理") as t1 inner join (select
  38. score.student_id as t2_id , score.num as num , course.cid ,course.cname
  39. from
  40. course inner join score on course.cid = score.course_id
  41. where
  42. course.cname = "生物") as t2 on t1.t1_id = t2.t2_id
  43. where
  44. t1.num > t2.num

9、 查询没有同时选修物理课程和体育课程的学生姓名

  1. # 1.找物理和体育的课程id
  2. select
  3. cid
  4. from
  5. course
  6. where
  7. cname = "物理" or cname = "体育"
  8.  
  9. # 2.找学习体育物理课程的学生id
  10. select
  11. student_id
  12. from
  13. score
  14. where
  15. course_id in (2,3)
  16.  
  17. # 拼装数据
  18. select
  19. student_id
  20. from
  21. score
  22. where
  23. course_id in (select
  24. cid
  25. from
  26. course
  27. where
  28. cname = "物理" or cname = "体育")
  29.  
  30. # 3.(同时)学习体育物理课程的学生id
  31. select
  32. student_id
  33. from
  34. score
  35. where
  36. course_id in (select
  37. cid
  38. from
  39. course
  40. where
  41. cname = "物理" or cname = "体育")
  42.  
  43. group by
  44. score.student_id
  45. having
  46. count(*) = 2
  47.  
  48. # 4.除了通过学习物理和体育的学生之外,剩下的都是没有同时学习的学生id
  49. select
  50. sid,sname
  51. from
  52. student
  53. where
  54. sid not in (3号)
  55.  
  56. # 综合拼装:
  57. select
  58. sid,sname
  59. from
  60. student
  61. where
  62. sid not in (select
  63. student_id
  64. from
  65. score
  66. where
  67. course_id in (select
  68. cid
  69. from
  70. course
  71. where
  72. cname = "物理" or cname = "体育")
  73.  
  74. group by
  75. score.student_id
  76. having
  77. count(*) = 2)

10、查询挂科超过两门(包括两门)的学生姓名和班级

  1. # 10、查询挂科超过两门(包括两门)的学生姓名和班级
  2. """挂科<60"""
  3. select
  4. student_id,student.sname,class.caption
  5. from
  6. score inner join student on score.student_id = student.sid
  7. inner join class on class.cid = student.class_id
  8. where
  9. num < 60
  10. group by
  11. student_id
  12. having
  13. count(*) >= 2

11、查询选修了所有课程的学生姓名

  1. # 11、查询选修了所有课程的学生姓名
  2. # 1.统计所有课程总数
  3. select count(*) from course
  4.  
  5. # 2.按照学生分类,总数量是1号查询出来的数据,等价于学了所有课程
  6. select
  7. student.sid, student.sname
  8. from
  9. score inner join student on score.student_id = student.sid
  10. group by
  11. score.student_id
  12. having
  13. count(*) = (1号)
  14.  
  15. # 综合拼接
  16. select
  17. student.sid, student.sname
  18. from
  19. score inner join student on score.student_id = student.sid
  20. group by
  21. score.student_id
  22. having
  23. count(*) = (select count(*) from course)

12、查询李平老师教的课程的所有成绩记录

  1. # 12、查询李平老师教的课程的所有成绩记录
  2. # 内联
  3. select
  4. score.student_id , course.cname , score.num
  5. from
  6. teacher , course , score
  7. where
  8. teacher.tid = course.teacher_id
  9. and
  10. score.course_id = course.cid
  11. and
  12. teacher.tname = "李平"
  13.  
  14. # 子查询
  15. # 1.找李平老师的课程id
  16. select
  17. course.cid
  18. from
  19. teacher,course
  20. where
  21. teacher.tid = course.teacher_id
  22. and
  23. teacher.tname = "李平"
  24. # 2.通过课程id号 找score里面的数据
  25. select *
  26. from
  27. score
  28. where
  29. course_id in (1号)
  30.  
  31. # 综合拼接
  32. select
  33. score.student_id ,score.num
  34. from
  35. score
  36. where
  37. course_id in (select
  38. course.cid
  39. from
  40. teacher,course
  41. where
  42. teacher.tid = course.teacher_id
  43. and
  44. teacher.tname = "李平")

13、查询全部学生都选修了的课程号和课程名

  1. # 13、查询全部学生都选修了的课程号和课程名
  2. # 1.通过score表,找有成绩的学生个数
  3. select
  4. count(distinct student_id)
  5. from
  6. score
  7.  
  8. # 2.按照课程分类,筛选学生个数等于13的课程id
  9. select
  10. course_id
  11. from
  12. score
  13. group by
  14. course_id
  15. having
  16. count(*) = 13
  17.  
  18. # 综合拼接
  19. select
  20. course_id,course.cname
  21. from
  22. score,course
  23. where
  24. score.course_id = course.cid
  25. group by
  26. course_id
  27. having
  28. count(*) = (select
  29. count(distinct student_id)
  30. from
  31. score)

14、查询每门课程被选修的次数

  1. # 14、查询每门课程被选修的次数;
  2. select
  3. course_id,count(*)
  4. from
  5. score
  6. group by
  7. course_id

15、查询只选修了一门课程的学生学号和姓名

  1. # 15、查询只选修了一门课程的学生学号和姓名
  2. # 1.按照学生分类,统计课程个数为1
  3. select
  4. student_id
  5. from
  6. score
  7. group by
  8. student_id
  9. having
  10. count(*) = 1
  11.  
  12. # 2.顺手连带一个学生表student , 通过id拿姓名
  13.  
  14. select
  15. student_id,student.sname
  16. from
  17. score inner join student on score.student_id = student.sid
  18. group by
  19. student_id
  20. having
  21. count(*) = 1

16、查询所有学生考出的成绩并按从高到低排序(成绩去重)

  1. # 16、查询所有学生考出的成绩并按从高到低排序(成绩去重)
  2. select
  3. distinct num,group_concat(student_id)
  4. from
  5. score
  6. group by
  7. num
  8. order by
  9. num desc
  10.  
  11. # 其他同学想法
  12. select
  13. avg(num),sum(num)
  14. from
  15. score
  16. group by
  17. student_id
  18. order by
  19. avg(num) desc

17、查询平均成绩大于85的学生姓名和平均成绩

  1. # 17、查询平均成绩大于85的学生姓名和平均成绩
  2. # 子查询
  3.  
  4. # 1.找学生id
  5. select
  6. student_id
  7. from
  8. score
  9. group by
  10. student_id
  11. having
  12. avg(num) > 85
  13.  
  14. # 2.找学生表对应数据
  15. select sname
  16. from
  17. student
  18. where
  19. id = (1号)
  20.  
  21. # 综合拼接
  22. select
  23. sid,sname
  24. from
  25. student
  26. where
  27. sid = (select
  28. student_id
  29. from
  30. score
  31. group by
  32. student_id
  33. having
  34. avg(num) > 85)

18、查询生物成绩不及格的学生姓名和对应生物分数

  1. # 18、查询生物成绩不及格的学生姓名和对应生物分数
  2. select
  3. student.sname,score.num,course.cname
  4. from
  5. course inner join score on course.cid = score.course_id
  6. inner join student on student.sid = score.student_id
  7. where
  8. score.num < 60
  9. and
  10. course.cname = "生物"

19、查询在所有选修了李平老师课程的学生中,这些课程(李平老师的课程,不是所有课程)平均成绩最高的学生姓名

  1. # 19、查询在所有选修了李平老师课程的学生中,这些课程(李平老师的课程,不是所有课程)平均成绩最高的学生姓名
  2. # 1.找李平老师教的课程id
  3. select
  4. course.cid
  5. from
  6. teacher , course
  7. where
  8. teacher.tid = course.teacher_id
  9. and
  10. teacher.tname = "李平"
  11. # 2 4
  12.  
  13. # 2.学习李平老师课程的学生中,按照学生分类,找平均分最高的id
  14. select
  15. score.student_id , avg(num)
  16. from
  17. score
  18. where
  19. score.course_id in (2,4)
  20. group by
  21. score.student_id
  22. order by
  23. avg(num) desc limit 1
  24.  
  25. # 3.通过学生id 顺带着连一张student学生表,找出姓名
  26. select
  27. score.student_id , student.sname , avg(num)
  28. from
  29. score,student
  30. where
  31. score.student_id = student.sid
  32. and
  33. score.course_id in (1号)
  34. group by
  35. score.student_id
  36. order by
  37. avg(num) desc limit 1
  38.  
  39. # 4.综合拼接
  40. select
  41. score.student_id , student.sname , avg(num)
  42. from
  43. score,student
  44. where
  45. score.student_id = student.sid
  46. and
  47. score.course_id in (select
  48. course.cid
  49. from
  50. teacher , course
  51. where
  52. teacher.tid = course.teacher_id
  53. and
  54. teacher.tname = "李平" )
  55. group by
  56. score.student_id
  57. order by
  58. avg(num) desc limit 1

20、查询每门课程成绩最好的课程id、学生姓名和分数

  1. # 20、查询每门课程成绩最好的课程id、学生姓名和分数
  2. # 1.找分数最大值.
  3. select
  4. course_id,max(num) as max_num
  5. from
  6. score
  7. group by
  8. score.course_id
  9.  
  10. # 2.找出该分数对应的那批学生
  11.  
  12. select
  13. *
  14. from
  15. score as t1 inner join student as t2 on t1.student_id = t2.sid
  16. inner join (1号) as t3 on t1.course_id = t3.course_id
  17.  
  18. # 综合拼接
  19. select
  20. t1.course_id , t2.sname , t3.max_num
  21. from
  22. score as t1 inner join student as t2 on t1.student_id = t2.sid
  23. inner join (select
  24. course_id,max(num) as max_num
  25. from
  26. score
  27. group by
  28. score.course_id) as t3 on t1.course_id = t3.course_id
  29.  
  30. where
  31. t1.num = t3.max_num

21、查询不同课程但成绩相同的课程号、学生号、成绩

  1. # 21、查询不同课程但成绩相同的课程号、学生号、成绩
  2. """不同的课程 如果使用!= 相同的数据返回来又查询了一遍,翻倍,为了防止翻倍重复查询使用>或者< """
  3. select
  4. s1.student_id as s1_sid,
  5. s2.student_id as s2_sid,
  6. s1.course_id as s1_cid,
  7. s2.course_id as s2_cid,
  8. s1.num as s1_num,
  9. s2.num as s2_num
  10. from
  11. score as s1,
  12. score as s2
  13. where
  14. s1.course_id > s2.course_id
  15. and
  16. s1.num = s2.num

22、查询没学过“李平”老师课程的学生姓名以及选修的课程名称

和第7题重复了!!

23、查询所有选修了学号为2的同学选修过的一门或者多门课程的同学学号和姓名

  1. # 23、查询所有选修了学号为2的同学选修过的一门或者多门课程的同学学号和姓名
  2. # 1.学号为2的学生,选了什么学科
  3. select
  4. course_id
  5. from
  6. score
  7. where
  8. student_id = 2 # 1 3 4
  9.  
  10. # 2.学过1 3 4 学科的学生都有谁
  11. select
  12. distinct student_id,student.sname
  13. from
  14. score inner join student on score.student_id= student.sid
  15. where
  16. course_id in (1,3,4)
  17.  
  18. # 综合拼接
  19. select
  20. distinct student_id,student.sname
  21. from
  22. score inner join student on score.student_id= student.sid
  23. where
  24. course_id in (select
  25. course_id
  26. from
  27. score
  28. where
  29. student_id = 2)

24、任课最多的老师中学生单科成绩最高的课程id、学生姓名和分数

  1. # 24、任课最多的老师中学生单科成绩最高的课程id、学生姓名和分数
  2.  
  3. # 1.老师任何的最大数量是多少
  4. """任课数量为2的老师可能不止一个"""
  5. select
  6. count(*)
  7. from
  8. course
  9. group by
  10. teacher_id
  11. order by
  12. count(*) desc limit 1
  13.  
  14. # 2.找最大任课数量为2的老师id
  15. select
  16. teacher_id
  17. from
  18. course
  19. group by
  20. teacher_id
  21. having
  22. count(*) = (1号)
  23.  
  24. # 综合拼接
  25. select
  26. teacher_id
  27. from
  28. course
  29. group by
  30. teacher_id
  31. having
  32. count(*) = (select
  33. count(*)
  34. from
  35. course
  36. group by
  37. teacher_id
  38. order by
  39. count(*) desc limit 1)
  40.  
  41. # 3.通过老师id 找课程
  42. select
  43. cid
  44. from
  45. course
  46. where
  47. teacher_id in (2号)
  48.  
  49. # 综合拼接
  50. # 2 4
  51. select
  52. cid
  53. from
  54. course
  55. where
  56. teacher_id in (select
  57. teacher_id
  58. from
  59. course
  60. group by
  61. teacher_id
  62. having
  63. count(*) = (select
  64. count(*)
  65. from
  66. course
  67. group by
  68. teacher_id
  69. order by
  70. count(*) desc limit 1))
  71.  
  72. # 4.通过该课程号,找其中的最大值(最大分数)
  73. select
  74. course_id,
  75. max(num) as max_num
  76. from
  77. score
  78. where
  79. course_id in (3号)
  80. group by
  81. course_id
  82.  
  83. # 综合拼接
  84. select
  85. course_id,
  86. max(num) as max_num
  87. from
  88. score
  89. where
  90. course_id in (select
  91. cid
  92. from
  93. course
  94. where
  95. teacher_id in (select
  96. teacher_id
  97. from
  98. course
  99. group by
  100. teacher_id
  101. having
  102. count(*) = (select
  103. count(*)
  104. from
  105. course
  106. group by
  107. teacher_id
  108. order by
  109. count(*) desc limit 1)))
  110. group by
  111. course_id
  112.  
  113. # 5.把对应的学生姓名,最大分数拼在一起,做一次单表查询
  114.  
  115. select
  116. *
  117. from
  118. score as t1 inner join student as t2 on t1.student_id = t2.sid
  119. inner join (4号) as t3 on t3.course_id = t1.course_id
  120.  
  121. # 综合拼接
  122. select
  123. *
  124. from
  125. score as t1 inner join student as t2 on t1.student_id = t2.sid
  126. inner join (select
  127. course_id,
  128. max(num) as max_num
  129. from
  130. score
  131. where
  132. course_id in (select
  133. cid
  134. from
  135. course
  136. where
  137. teacher_id in (select
  138. teacher_id
  139. from
  140. course
  141. group by
  142. teacher_id
  143. having
  144. count(*) = (select
  145. count(*)
  146. from
  147. course
  148. group by
  149. teacher_id
  150. order by
  151. count(*) desc limit 1)))
  152. group by
  153. course_id) as t3 on t3.course_id = t1.course_id
  154.  
  155. # 6.把分数是100分的最大值的学员查出来
  156. select
  157. *
  158. from
  159. score as t1 inner join student as t2 on t1.student_id = t2.sid
  160. inner join (4号) as t3 on t3.course_id = t1.course_id
  161. where
  162. t1.num = t3.max_num
  163.  
  164. # 综合拼接
  165. select
  166. t1.course_id,t2.sname,t3.max_num
  167. from
  168. score as t1 inner join student as t2 on t1.student_id = t2.sid
  169. inner join (select
  170. course_id,
  171. max(num) as max_num
  172. from
  173. score
  174. where
  175. course_id in (select
  176. cid
  177. from
  178. course
  179. where
  180. teacher_id in (select
  181. teacher_id
  182. from
  183. course
  184. group by
  185. teacher_id
  186. having
  187. count(*) = (select
  188. count(*)
  189. from
  190. course
  191. group by
  192. teacher_id
  193. order by
  194. count(*) desc limit 1)))
  195. group by
  196. course_id) as t3 on t3.course_id = t1.course_id
  197. where
  198. t1.num = t3.max_num

day41:MYSQL:select查询练习题的更多相关文章

  1. MYSQL select查询练习题

    10. 查询Score表中的最高分的学生学号和课程号.(子查询或者排序) select sno,cno from score where degree=(select max(degree) from ...

  2. MySQL Select查询

    1. 基本语法: SELECT {* | <字段列名>} [ FROM <表 1>, <表 2>… [WHERE <表达式> [GROUP BY < ...

  3. mysql常见查询练习题

    #建学生信息表student create table student ( sno varchar(20) not null primary key, sname varchar(20) not nu ...

  4. MySQL select 查询之分组和过滤

    SELECT 语法 SELECT [ALL | DISTINCT] {* | table.* | [table.field1[as alias1][,table.field2[as alias2]][ ...

  5. MySQL select 查询的分页和排序

    SELECT 语法 SELECT [ALL | DISTINCT] {* | table.* | [table.field1[as alias1][,table.field2[as alias2]][ ...

  6. mysql select语句查询流程是怎么样的

    select查询流程是怎么样的 mysql select查询的数据是查询内存里面,如果没有查询的数据没有在内存,就需要mysql的innodb引擎读取磁盘,将数据加载的内存后在读取.这就体现了,mys ...

  7. mysql DML select查询

    windows上的操作 1.从官网下载mysql 下载navicat,用来连接mysql的 2.打开运行启动mysql 3.在navicat上的连接打开新建连接 然后输入链接名,连接名就是用户名,自己 ...

  8. MySQL连表查询练习题

    1.建库 库名:linux50 字符集:utf8 校验规则:utf8_general_ci  create database linux4 charset utf8 default collate ...

  9. MySQL 表查询语句练习题

    MySQL 表查询语句练习题: 一.  设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表 ...

随机推荐

  1. luogu P4095 [HEOI2013]Eden 的新背包问题 多重背包 背包的合并

    LINK:Eden 的新背包问题 就是一个多重背包 每次去掉一个物品 询问钱数为w所能买到的最大值. 可以对于每次Q暴力dp 利用单调队列优化多重背包 这样复杂度是Qnm的. 发现过不了n==10的点 ...

  2. x86架构:分页机制和原理

    分页是现在CPU核心的管理内存方式,网上介绍材料很多,这里不赘述,简单介绍一下分页的背景和原理 1.先说说为什么要分段 实模式下程序之间不隔离,互相能直接读写对方内存,或跳转到其他进程的代码运行,导致 ...

  3. Idea 提交配置说明

    Idea 提交配置说明# Auto-update after commit :自动升级后提交 keep files locked :把文件锁上,我想这应该就只能你修改其他开发人不能修改不了的功能 在你 ...

  4. ios wkwebview didReceiveAuthenticationChallenge crash解决

    //需要响应身份验证时调用 同样在block中需要传入用户身份凭证 //现在就是不进行https验证了 然后就闪退不了了 - (void)webView:(WKWebView *)webView di ...

  5. 提交项目到码云上git的使用

    git clone .. cd 到项目目录 git branch   查看当前的所有分支 git branch shanshan    创建一个属于自己的分支 git checkout shansha ...

  6. 强大的输入框-应用快速启动uTools

    uTools uTools是一个 极简.插件化.跨平台 的现代桌面软件.通过自由选配丰富的插件,打造你得心应手的工具集合. 当你熟悉它后,能够为你节约大量时间,让你可以更加专注地改变世界. uTool ...

  7. python8.3多进程

    from multiprocessing import Processimport time def run1 (name,sex): print(name,sex,"执行进程1" ...

  8. jquery 事件对象笔记

    jQuery元素操作 设置或获取元素固有属性   获取               prop(属性名)    修改               prop(属性名,值) 获取自定义属性          ...

  9. 2020-04-05:谈一下spring事务传播

  10. C#LeetCode刷题-图

    图篇 # 题名 刷题 通过率 难度 133 克隆图   18.7% 中等 207 课程表   40.0% 中等 210 课程表 II   40.0% 中等 310 最小高度树   29.5% 中等 3 ...