题目入口:http://db.itoi.sd.cn/problem/set

成绩管理相关题目

Student表

image.png

Course表

image.png

SC表

image.png

初始化SQL语句

  1. SET NAMES utf8mb4;
  2. SET FOREIGN_KEY_CHECKS = 0;
  3. /*学生表*/
  4. CREATE TABLE Student
  5. (
  6. Sno INT(9) PRIMARY KEY,
  7. Sname VARCHAR(10),
  8. Ssex CHAR(2),
  9. Sage TINYINT(3),
  10. Sdept VARCHAR(20)
  11. );
  12. INSERT INTO Student
  13. VALUES (201215121, '李勇', '男', 20, 'CS'),
  14. (201215122, '刘晨', '女', 19, 'CS'),
  15. (201215123, '王敏', '女', 18, 'MA'),
  16. (201215125, '张立', '男', 19, 'IS'),
  17. (201215133, '张三', '男', 21, 'TE'),
  18. (201215137, '赵四', '男', 23, 'TE'),
  19. (201215139, '田二', '女', 24, 'CS'),
  20. (201215140, '李四', '男', 21, 'CS'),
  21. (201215141, '郑五', '女', 22, 'IS');
  22. /*课程表*/
  23. CREATE TABLE Course
  24. (
  25. Cno INT(4) PRIMARY KEY,
  26. Cname VARCHAR(40),
  27. Cpno INT(4),
  28. Ccredit TINYINT(3),
  29. FOREIGN KEY (Cpno) REFERENCES Course (Cno)
  30. );
  31. INSERT INTO Course
  32. VALUES (1, '数据库', 5, 4),
  33. (2, '数学', NULL, 2),
  34. (3, '信息系统', 1, 4),
  35. (4, '操作系统', 6, 3),
  36. (5, '数据结构', 7, 4),
  37. (6, '数据处理', NULL, 2),
  38. (7, 'PASCAL语言', 6, 4);
  39. /*选课表*/
  40. CREATE TABLE SC
  41. (
  42. Sno INT(9),
  43. Cno INT(4),
  44. Grade SMALLINT(3),
  45. PRIMARY KEY (Sno, Cno),
  46. /* 主码由两个属性构成,必须作为表级完整性进行定义*/
  47. FOREIGN KEY (Sno) REFERENCES Student (Sno),
  48. /* 表级完整性约束条件,Sno是外码,被参照表是Student */
  49. FOREIGN KEY (Cno) REFERENCES Course (Cno)
  50. /* 表级完整性约束条件, Cno是外码,被参照表是Course*/
  51. );
  52. INSERT INTO SC
  53. VALUES (201215121, 1, 92),
  54. (201215121, 2, 85),
  55. (201215121, 3, 88),
  56. (201215122, 2, 90),
  57. (201215122, 3, 80),
  58. (201215122, 6, 59),
  59. (201215123, 1, 84),
  60. (201215125, 1, 60),
  61. (201215125, 3, 90),
  62. (201215133, 4, 87),
  63. (201215137, 2, 79),
  64. (201215139, 2, 80),
  65. (201215140, 2, 81);
  66. SET FOREIGN_KEY_CHECKS = 1;

问题代码

  1. 0
  2. select Sno, Sname
  3. from Student
  4. 1
  5. select Sname, Sno, Sdept
  6. from Student
  7. 2
  8. select Sname, Sno, Sdept
  9. from Student
  10. 3
  11. select distinct Sno
  12. from SC
  13. 4
  14. select distinct Sname
  15. from Student
  16. where Sdept='CS'
  17. 5
  18. select Sname, Sage
  19. from Student
  20. where Sage<20
  21. 6
  22. select Sno
  23. from SC
  24. where Grade<60
  25. 7
  26. select Sname, Sdept, Sage
  27. from Student
  28. where Sage>=20 and Sage<=23
  29. 8
  30. select Sname, Sdept, Sage
  31. from Student
  32. where Sage<20 or Sage>23
  33. 9
  34. select Sname, Ssex
  35. from Student
  36. where Sdept='CS' or Sdept='MA' or Sdept='IS'
  37. 10
  38. select Sname, Ssex
  39. from Student
  40. where Sdept not in ('CS', 'MA', 'IS')
  41. 11
  42. select Sname, Sno, Ssex
  43. from Student
  44. where Sname like '刘_'
  45. 12
  46. select Sname, Sno
  47. from Student
  48. where Sname like '_立'
  49. 13
  50. select Sname, Sno, Ssex
  51. from Student
  52. where Sname not like '刘_'
  53. 14
  54. select Sno, Cno
  55. from SC
  56. where Grade is not null
  57. 15
  58. select Sname
  59. from Student
  60. where Sdept='CS' and Sage<20
  61. 16
  62. select Sno, Grade
  63. from SC
  64. where Cno='3'
  65. order by Grade desc
  66. 17
  67. select *
  68. from Student
  69. order by Sdept asc, Sage desc
  70. 18
  71. select count(Sno) as 'COUNT'
  72. from Student
  73. 19
  74. select count(Sno) as COUNT
  75. from (
  76. select Sno
  77. from SC
  78. group by sno) as test
  79. 20
  80. select avg(Grade) as AVG
  81. from SC
  82. where Cno='1'
  83. 21
  84. select max(Grade) as MAX
  85. from SC
  86. where Cno='1'
  87. 22
  88. select SUM(Ccredit) as SUM
  89. from Course, SC
  90. where Course.Cno=SC.Cno
  91. and Sno='201215121'
  92. 23
  93. select Cno, count(Grade) as COUNT
  94. from SC
  95. group by Cno
  96. 24
  97. select Sno
  98. from(
  99. select Sno, count(Grade) as COUNT
  100. from SC
  101. group by Sno
  102. )as test
  103. where COUNT>2
  104. 25
  105. select Sno, AVG(Grade) as AVG
  106. from SC
  107. group by Sno
  108. having AVG(Grade)>=88
  109. 26
  110. select Student.Sno, Sname, Ssex, Sage, Sdept, Cno, Grade
  111. from Student inner join SC
  112. on Student.Sno=SC.Sno
  113. 27
  114. SELECT x.Cno, y.Cpno
  115. FROM Course x, Course y
  116. WHERE x.Cpno = y.Cno
  117. 28
  118. select Student.Sno, Student.Sname
  119. from Student, SC
  120. where Student.Sno=SC.Sno
  121. and SC.Cno='2'
  122. and SC.Grade>80
  123. 29
  124. select test.Sno, test.Sname, Course.Cname, test.Grade
  125. from Course join
  126. (select SC.Sno, SC.Cno, Student.Sname, SC.Grade
  127. from Student join SC
  128. on Student.Sno=SC.Sno)as test
  129. on Course.Cno=test.Cno

Movie相关题目

  1. 30
  2. select title
  3. from movie
  4. where director='Steven Spielberg'
  5. 31
  6. select year
  7. from movie
  8. where mID in (
  9. select mID
  10. from rating
  11. where stars='4' or stars='5'
  12. )
  13. order by year asc
  14. 32
  15. select title
  16. from movie
  17. where mID not in (
  18. select mID
  19. from rating)
  20. 33
  21. select name
  22. from reviewer
  23. where rID in (
  24. select rID
  25. from rating
  26. where ratingDate is null)
  27. 34
  28. select reviewer.name, test.title, test.stars, test.ratingDate
  29. from reviewer join (
  30. select movie.title, rating.rID, rating.stars, rating.ratingDate
  31. from movie join rating
  32. on movie.mID=rating.mID
  33. ) as test
  34. on reviewer.rID=test.rID
  35. order by reviewer.name, test.title, test.stars
  36. 35
  37. 37
  38. select movie.title, rating_2.stars_avg as stars
  39. from movie join (
  40. select mID, AVG(stars) as stars_avg
  41. from rating
  42. group by mID) as rating_2
  43. on movie.mID=rating_2.mID
  44. order by stars desc, title
  45. 38
  46. select name
  47. from reviewer
  48. where rID in(
  49. select rID
  50. from rating
  51. group by rID
  52. having COUNT(rID)>=3)
  53. 39
  54. select name
  55. from reviewer
  56. where rID in (
  57. select distinct rID
  58. from rating
  59. where mID in (
  60. select mID
  61. from movie
  62. where title='Gone with the Wind'))
  63. 40
  64. select temp.name as name, movie.title as title, temp.stars as stars
  65. from movie join (
  66. select rating.stars, reviewer.name, rating.mID
  67. from rating join reviewer
  68. on rating.rID=reviewer.rID) as temp
  69. on movie.mID=temp.mID
  70. where movie.director=temp.name