一、表关系

  请创建如下表,并创建相关约束

               
班级表:class       学生表:student      
cid caption grade_id   sid sname gender class_id
1 一年一班 1   1 乔丹 1
2 二年一班 2   2 艾弗森 1
3 三年二班 3   3 科比 2
               
老师表:teacher       课程表:course      
tid tname     cid cname teacher_id  
1 张三     1 生物 1  
2 李四     2 体育 1  
3 王五     3 物理 2  
               
成绩表:score         年级表:class_grade    
sid student_id course_id score   gid gname  
1 1 1 60   1 一年级  
2 1 2 59   2 二年级  
3 2 2 99   3 三年级  
               
班级任职表:teach2cls              
tcid tid cid          
1 1 1          
2 1 2          
3 2 1          
4 3 2        

二、操作表

  1. -- 1、自行创建测试数据;
  2. create database db5;
  3. use db5;
  4.  
  5. create table teacher (
  6. tid int primary key auto_increment,
  7. tname char(20) not null
  8. );
  9.  
  10. insert into teacher(tname) values
  11. ('张三'),
  12. ('李四'),
  13. ('王五'),
  14. ('赵六'),
  15. ('马七');
  16.  
  17. create table class_grade (
  18. gid int primary key auto_increment,
  19. gname char(20) not null
  20. );
  21.  
  22. insert into class_grade(gname) values
  23. ('一年级'),
  24. ('二年级'),
  25. ('三年级'),
  26. ('四年级'),
  27. ('五年级');
  28.  
  29. create table course (
  30. cid int primary key auto_increment,
  31. cname char(20) not null,
  32. teacher_id int,
  33. foreign key(teacher_id) references teacher(tid)
  34. on delete cascade
  35. on update cascade
  36. );
  37.  
  38. insert into course(cname, teacher_id) values
  39. ('生物',1),
  40. ('体育',1),
  41. ('物理',2),
  42. ('语文',3),
  43. ('数学',4),
  44. ('外语',5);
  45.  
  46. create table class (
  47. cid int primary key auto_increment,
  48. caption char(20),
  49. grade_id int,
  50. foreign key(grade_id) references class_grade(gid)
  51. on delete cascade
  52. on update cascade
  53. );
  54.  
  55. insert into class(caption, grade_id) values
  56. ('一年一班',1),
  57. ('一年二班',1),
  58. ('二年一班',2),
  59. ('二年二班',2),
  60. ('三年一班',3),
  61. ('四年一班',4),
  62. ('五年一班',5);
  63.  
  64. create table student (
  65. sid int primary key auto_increment,
  66. sname char(20),
  67. gender enum('男', '女') not null,
  68. class_id int,
  69. foreign key(class_id) references class(cid)
  70. on delete cascade
  71. on update cascade
  72. );
  73.  
  74. insert into student(sname,gender,class_id) values
  75. ('乔丹','女',1),
  76. ('艾弗森','女',1),
  77. ('科比','男',2),
  78. ('curry','男',2),
  79. ('james','男',3),
  80. ('李瑞','女',4),
  81. ('白雪','女',5),
  82. ('无敌','男',5),
  83. ('天剑','男',6),
  84. ('egon','女',7),
  85. ('alex','男',7);
  86.  
  87. create table score (
  88. sid int primary key auto_increment,
  89. student_id int,
  90. course_id int,
  91. score int,
  92. foreign key(student_id) references student(sid)
  93. on delete cascade
  94. on update cascade,
  95. foreign key(course_id) references course(cid)
  96. on delete cascade
  97. on update cascade
  98. );
  99.  
  100. insert into score(student_id, course_id, score) values
  101. (1,1,60),
  102. (1,2,59),
  103. (1,3,58),
  104. (1,4,71),
  105. (1,5,68),
  106. (1,6,100),
  107. (2,1,90),
  108. (2,2,99),
  109. (2,3,71),
  110. (2,4,68),
  111. (2,5,92),
  112. (2,6,88),
  113. (3,1,23),
  114. (3,2,55),
  115. (3,3,72),
  116. (3,4,88),
  117. (3,5,92),
  118. (3,6,12),
  119. (4,2,65),
  120. (4,4,78),
  121. (4,5,34),
  122. (5,2,75),
  123. (5,4,38),
  124. (5,5,44),
  125. (6,2,23),
  126. (6,4,32),
  127. (6,5,0),
  128. (7,1,78),
  129. (7,3,60),
  130. (7,6,45),
  131. (8,1,43),
  132. (8,3,65),
  133. (8,6,99),
  134. (9,2,56),
  135. (9,3,69),
  136. (9,5,78),
  137. (10,2,43),
  138. (10,3,69),
  139. (10,5,90),
  140. (11,2,90),
  141. (11,3,89),
  142. (11,5,100);
  143.  
  144. create table teach2cls (
  145. tcid int primary key auto_increment,
  146. tid int,
  147. cid int,
  148. foreign key(tid) references teacher(tid)
  149. on delete cascade
  150. on update cascade,
  151. foreign key(cid) references class(cid)
  152. on delete cascade
  153. on update cascade
  154. );
  155.  
  156. insert into teach2cls(tid, cid) values # 五个老师、七个班级
  157. (1,1),
  158. (1,2),
  159. (1,5),
  160. (1,7),
  161. (2,2),
  162. (2,3),
  163. (2,4),
  164. (2,6),
  165. (3,1),
  166. (3,2),
  167. (3,4),
  168. (3,5),
  169. (3,6),
  170. (4,2),
  171. (4,4),
  172. (4,7),
  173. (5,5),
  174. (5,6),
  175. (5,7);
  176.  
  177. -- 2、查询学生总人数;
  178. select count(sid) as total_num from student;
  179. +-----------+
  180. | total_num |
  181. +-----------+
  182. | 11 |
  183. +-----------+
  184.  
  185. -- 3、查询“生物”课程和“物理”课程成绩都及格的学生id和姓名;
  186. select sid, sname from student where sid in (
  187. select
  188. score.student_id # 先找到学习了这两们课且都及格学生的id
  189. from
  190. score
  191. inner join course
  192. on score.course_id=course.cid
  193. where
  194. course.cname in (
  195. '生物',
  196. '物理'
  197. )
  198. and score.score >=60
  199. group by
  200. score.student_id
  201. having
  202. count(course_id) =2
  203. );
  204. +-----+-----------+
  205. | sid | sname |
  206. +-----+-----------+
  207. | 2 | 艾弗森 |
  208. | 7 | 白雪 |
  209. +-----+-----------+
  210.  
  211. -- 4、查询每个年级的班级数,取出班级数最多的前三个年级;
  212. select
  213. gname,count(class.cid)
  214. from
  215. class_grade
  216. inner join
  217. class
  218. on class.grade_id=class_grade.gid
  219. group by gid
  220. order by count(cid) DESC
  221. limit 3;
  222. +-----------+------------------+
  223. | gname | count(class.cid) |
  224. +-----------+------------------+
  225. | 一年级 | 2 |
  226. | 二年级 | 2 |
  227. | 四年级 | 1 |
  228. +-----------+------------------+
  229.  
  230. -- 5、查询平均成绩最高和最低的学生的id和姓名以及平均成绩;
  231. select * from (
  232. select
  233. avg(score) as avg_score
  234. from
  235. score
  236. group by
  237. student_id
  238. order by avg_score ASC
  239. limit 1 ) t1 union
  240. select * from
  241. (select
  242. avg(score) as avg_score
  243. from
  244. score
  245. group by
  246. student_id
  247. order by avg_score DESC
  248. limit 1) t2;
  249. +-----------+
  250. | avg_score |
  251. +-----------+
  252. | 18.3333 |
  253. | 93.0000 |
  254. +-----------+
  255. select
  256. student_id,
  257. concat(student.sname) as student_name,
  258. avg(score)
  259. from
  260. student
  261. inner join
  262. score
  263. on score.student_id=student.sid
  264. group by
  265. student_id
  266. having
  267. avg(score) in (
  268. select * from (
  269. select
  270. avg(score) as avg_score
  271. from
  272. score
  273. group by
  274. student_id
  275. order by avg_score ASC
  276. limit 1 ) t1
  277. union
  278. select * from(
  279. select
  280. avg(score) as avg_score
  281. from
  282. score
  283. group by
  284. student_id
  285. order by avg_score DESC
  286. limit 1) t2
  287. );
  288. # 由于最小成绩是无限小数,无法显示
  289. +------------+--------------+------------+
  290. | student_id | student_name | avg(score) |
  291. +------------+--------------+------------+
  292. | 11 | alex | 93.0000 |
  293. +------------+--------------+------------+
  294.  
  295. -- 6、查询每个年级的学生人数;
  296. select
  297. grade_id, count(sid)
  298. from
  299. student
  300. left join
  301. class
  302. on student.class_id=class.cid
  303. group by
  304. grade_id;
  305. +----------+------------+
  306. | grade_id | count(sid) |
  307. +----------+------------+
  308. | 1 | 4 |
  309. | 2 | 2 |
  310. | 3 | 2 |
  311. | 4 | 1 |
  312. | 5 | 2 |
  313. +----------+------------+
  314. select
  315. gname, t1.stu_num as stu_num
  316. from
  317. class_grade
  318. inner join
  319. (select
  320. grade_id, count(sid) as stu_num
  321. from
  322. student
  323. left join
  324. class
  325. on student.class_id=class.cid
  326. group by
  327. grade_id) as t1
  328. on class_grade.gid=t1.grade_id;
  329. +-----------+---------+
  330. | gname | stu_num |
  331. +-----------+---------+
  332. | 一年级 | 4 |
  333. | 二年级 | 2 |
  334. | 三年级 | 2 |
  335. | 四年级 | 1 |
  336. | 五年级 | 2 |
  337. +-----------+---------+
  338.  
  339. -- 7、查询每位学生的学号,姓名,选课数,平均成绩;
  340. select
  341. student.sid,
  342. concat(student.sname),
  343. count(course_id) as course_num,
  344. avg(score) as avg_score
  345. from
  346. student
  347. inner join
  348. score
  349. on student.sid=score.student_id
  350. group by student.sid;
  351. +-----+-----------------------+------------+-----------+
  352. | sid | concat(student.sname) | course_num | avg_score |
  353. +-----+-----------------------+------------+-----------+
  354. | 1 | 乔丹 | 6 | 69.3333 |
  355. | 2 | 艾弗森 | 6 | 84.6667 |
  356. | 3 | 科比 | 6 | 57.0000 |
  357. | 4 | curry | 3 | 59.0000 |
  358. | 5 | james | 3 | 52.3333 |
  359. | 6 | 李瑞 | 3 | 18.3333 |
  360. | 7 | 白雪 | 3 | 61.0000 |
  361. | 8 | 无敌 | 3 | 69.0000 |
  362. | 9 | 天剑 | 3 | 67.6667 |
  363. | 10 | egon | 3 | 67.3333 |
  364. | 11 | alex | 3 | 93.0000 |
  365. +-----+-----------------------+------------+-----------+
  366.  
  367. -- 8、查询学生编号为“2”的学生的姓名、该学生成绩最高的课程名、成绩最低的课程名及分数;
  368. # score——》student——》course
  369. select
  370. student.sname,
  371. course.cname,
  372. score.score
  373. from
  374. score
  375. left join student on score.student_id=student.sid
  376. left join course on score.course_id=course.cid
  377. where student.sid=2
  378. and score in (
  379. select * from (
  380. select
  381. max(score)
  382. from
  383. score
  384. where
  385. student_id=2) t1
  386. union
  387. select * from (
  388. select
  389. min(score)
  390. from
  391. score
  392. where
  393. student_id=2) t2
  394. );
  395. +-----------+--------+-------+
  396. | sname | cname | score |
  397. +-----------+--------+-------+
  398. | 艾弗森 | 体育 | 99 |
  399. | 艾弗森 | 语文 | 68 |
  400. +-----------+--------+-------+
  401.  
  402. -- 9、查询姓“李”的老师的个数和所带班级数;
  403. select
  404. concat(teacher.tname),
  405. count(distinct teacher.tid) as teacher_num,
  406. count(teach2cls.cid) as class_num
  407. from
  408. teacher
  409. inner join
  410. teach2cls
  411. on teacher.tid=teach2cls.tid
  412. where
  413. tname like '李%'
  414. group by teacher.tid;
  415. +-----------------------+-------------+-----------+
  416. | concat(teacher.tname) | teacher_num | class_num |
  417. +-----------------------+-------------+-----------+
  418. | 李四 | 1 | 4 |
  419. +-----------------------+-------------+-----------+
  420.  
  421. -- 10、查询班级数小于5的年级id和年级名;
  422. select
  423. gid,
  424. gname
  425. from
  426. class_grade
  427. where gid in (
  428. select
  429. grade_id
  430. from
  431. class
  432. group by
  433. grade_id
  434. having
  435. count(cid) < 5 )
  436. ;
  437. +-----+-----------+
  438. | gid | gname |
  439. +-----+-----------+
  440. | 1 | 一年级 |
  441. | 2 | 二年级 |
  442. | 3 | 三年级 |
  443. | 4 | 四年级 |
  444. | 5 | 五年级 |
  445. +-----+-----------+
  446.  
  447. -- 11、查询班级信息,包括班级id、班级名称、年级、年级级别(12为低年级,34为中年级,56为高年级),示例结果如下;
  448. # mysql中case when then else end的用法
  449. select
  450. class.cid,
  451. class.caption,
  452. class_grade.gname,
  453. case # 如果
  454. when class_grade.gid between 1 and 2 then '低年级' # when后接条件,then后接返回值
  455. when class_grade.gid between 3 and 4 then '中年级'
  456. when class_grade.gid between 5 and 6 then '高年级'
  457. else 0 # 其他的返回值
  458. end as '年级级别' # end代表结束,自定义为'年级级别'
  459. from
  460. class
  461. inner join
  462. class_grade
  463. on class.grade_id=class_grade.gid;
  464. +-----+--------------+-----------+--------------+
  465. | cid | caption | gname | 年级级别 |
  466. +-----+--------------+-----------+--------------+
  467. | 1 | 一年一班 | 一年级 | 低年级 |
  468. | 2 | 一年二班 | 一年级 | 低年级 |
  469. | 3 | 二年一班 | 二年级 | 低年级 |
  470. | 4 | 二年二班 | 二年级 | 低年级 |
  471. | 5 | 三年一班 | 三年级 | 中年级 |
  472. | 6 | 四年一班 | 四年级 | 中年级 |
  473. | 7 | 五年一班 | 五年级 | 高年级 |
  474. +-----+--------------+-----------+--------------+
  475.  
  476. -- 12、查询学过“张三”老师2门课以上的同学的学号、姓名;
  477. select
  478. sid,
  479. sname
  480. from
  481. student
  482. where
  483. sid in (
  484. select
  485. student_id
  486. from
  487. score
  488. where course_id in (
  489. select
  490. cid
  491. from
  492. course
  493. where
  494. teacher_id=(
  495. select
  496. tid
  497. from
  498. teacher
  499. where
  500. tname='张三'
  501. )
  502. )
  503. group by student_id
  504. having count(course_id)>=2
  505. );
  506. +-----+-----------+
  507. | sid | sname |
  508. +-----+-----------+
  509. | 1 | 乔丹 |
  510. | 2 | 艾弗森 |
  511. | 3 | 科比 |
  512. +-----+-----------+
  513.  
  514. -- 13、查询教授课程超过2门的老师的id和姓名;
  515. select
  516. tid,
  517. tname
  518. from
  519. teacher
  520. where tid in (
  521. select
  522. teacher_id
  523. from
  524. course
  525. group by
  526. teacher_id
  527. having
  528. count(cid) >= 2
  529. );
  530. +-----+--------+
  531. | tid | tname |
  532. +-----+--------+
  533. | 1 | 张三 |
  534. +-----+--------+
  535.  
  536. -- 14、查询学过编号“1”课程和编号“2”课程的同学的学号、姓名;
  537. select
  538. sid,
  539. sname
  540. from
  541. student
  542. where sid in (
  543. select
  544. student_id
  545. from
  546. score
  547. where course_id in (1,2)
  548. group by
  549. student_id
  550. );
  551. +-----+-----------+
  552. | sid | sname |
  553. +-----+-----------+
  554. | 1 | 乔丹 |
  555. | 2 | 艾弗森 |
  556. | 3 | 科比 |
  557. | 4 | curry |
  558. | 5 | james |
  559. | 6 | 李瑞 |
  560. | 7 | 白雪 |
  561. | 8 | 无敌 |
  562. | 9 | 天剑 |
  563. | 10 | egon |
  564. | 11 | alex |
  565. +-----+-----------+
  566.  
  567. -- 15、查询没有带过高年级的老师id和姓名;
  568. select
  569. *
  570. from
  571. teacher
  572. where tid not in (
  573. select
  574. tid
  575. from
  576. teach2cls
  577. inner join
  578. class
  579. on teach2cls.cid=class.cid
  580. where class.grade_id in (5,6)
  581. );
  582. +-----+--------+
  583. | tid | tname |
  584. +-----+--------+
  585. | 2 | 李四 |
  586. | 3 | 王五 |
  587. +-----+--------+
  588.  
  589. -- 16、查询学过“张三”老师所教的所有课的同学的学号、姓名;
  590. select
  591. sid,
  592. sname
  593. from
  594. student
  595. where class_id in (
  596. select
  597. cid # 张三教过的班级id
  598. from
  599. teach2cls
  600. inner join
  601. teacher
  602. on teach2cls.tid=teacher.tid
  603. where teacher.tname='张三'
  604. );
  605. +-----+-----------+
  606. | sid | sname |
  607. +-----+-----------+
  608. | 1 | 乔丹 |
  609. | 2 | 艾弗森 |
  610. | 3 | 科比 |
  611. | 4 | curry |
  612. | 7 | 白雪 |
  613. | 8 | 无敌 |
  614. | 10 | egon |
  615. | 11 | alex |
  616. +-----+-----------+
  617.  
  618. -- 17、查询带过超过2个班级的老师的id和姓名;
  619. select
  620. *
  621. from
  622. teacher
  623. where tid in (
  624. select
  625. tid
  626. from
  627. teach2cls
  628. group by
  629. tid
  630. having
  631. count(cid) >= 2
  632. );
  633. +-----+--------+
  634. | tid | tname |
  635. +-----+--------+
  636. | 1 | 张三 |
  637. | 2 | 李四 |
  638. | 3 | 王五 |
  639. | 4 | 赵六 |
  640. | 5 | 马七 |
  641. +-----+--------+
  642.  
  643. -- 18、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;
  644. select
  645. sid,
  646. sname
  647. from
  648. student
  649. where sid in(
  650. select
  651. t1.student_id
  652. from
  653. (select
  654. student_id,
  655. score as score_1
  656. from
  657. score
  658. where
  659. course_id=1) as t1
  660. inner join
  661. (select
  662. student_id,
  663. score as score_2
  664. from
  665. score
  666. where
  667. course_id=2) as t2
  668. on t1.student_id=t2.student_id
  669. where
  670. t2.score_2 > t1.score_1
  671. );
  672. +-----+-----------+
  673. | sid | sname |
  674. +-----+-----------+
  675. | 2 | 艾弗森 |
  676. | 3 | 科比 |
  677. +-----+-----------+
  678.  
  679. -- 19、查询所带班级数最多的老师id和姓名;
  680. select
  681. *
  682. from
  683. teacher
  684. where tid in (
  685. select
  686. tid # 考虑带最多班级的是多个老师,老师的tid
  687. from
  688. teach2cls
  689. group by
  690. tid
  691. having
  692. count(cid)=(
  693. select # 得到最多班级数量
  694. count(cid)
  695. from
  696. teach2cls
  697. group by
  698. tid
  699. order by
  700. count(cid) desc
  701. limit 1
  702. )
  703. );
  704. +-----+--------+
  705. | tid | tname |
  706. +-----+--------+
  707. | 3 | 王五 |
  708. +-----+--------+
  709.  
  710. -- 20、查询有课程成绩小于60分的同学的学号、姓名;
  711. select
  712. sid,
  713. sname
  714. from
  715. student
  716. where
  717. sid in (
  718. select
  719. distinct student_id
  720. from
  721. score
  722. where
  723. score.score < 60
  724. );
  725. +-----+--------+
  726. | sid | sname |
  727. +-----+--------+
  728. | 1 | 乔丹 |
  729. | 3 | 科比 |
  730. | 4 | curry |
  731. | 5 | james |
  732. | 6 | 李瑞 |
  733. | 7 | 白雪 |
  734. | 8 | 无敌 |
  735. | 9 | 天剑 |
  736. | 10 | egon |
  737. +-----+--------+
  738.  
  739. -- 21、查询没有学全所有课的同学的学号、姓名;
  740. select
  741. sid,
  742. sname
  743. from
  744. student
  745. where sid in (
  746. select
  747. student_id
  748. from
  749. score
  750. group by student_id
  751. having count(course_id) < (
  752. select
  753. count(cid)
  754. from
  755. course
  756. )
  757. );
  758. +-----+--------+
  759. | sid | sname |
  760. +-----+--------+
  761. | 4 | curry |
  762. | 5 | james |
  763. | 6 | 李瑞 |
  764. | 7 | 白雪 |
  765. | 8 | 无敌 |
  766. | 9 | 天剑 |
  767. | 10 | egon |
  768. | 11 | alex |
  769. +-----+--------+
  770.  
  771. -- 22、查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名;
  772. select
  773. sid,
  774. sname
  775. from
  776. student
  777. where sid in (
  778. select
  779. distinct student_id
  780. from
  781. score
  782. where course_id in (
  783. select
  784. course_id # 找出学号1锁学得所有课程
  785. from
  786. score
  787. where
  788. student_id=1
  789. ) and student_id!=1 # 排除掉学号为1的学生
  790. );
  791. +-----+-----------+
  792. | sid | sname |
  793. +-----+-----------+
  794. | 2 | 艾弗森 |
  795. | 3 | 科比 |
  796. | 4 | curry |
  797. | 5 | james |
  798. | 6 | 李瑞 |
  799. | 7 | 白雪 |
  800. | 8 | 无敌 |
  801. | 9 | 天剑 |
  802. | 10 | egon |
  803. | 11 | alex |
  804. +-----+-----------+
  805.  
  806. -- 23、查询至少学过学号为“1”同学所选课程中任意一门课的其他同学学号和姓名;
  807. select
  808. sid,
  809. sname
  810. from
  811. student
  812. where sid in (
  813. select
  814. distinct student_id
  815. from
  816. score
  817. where course_id in (
  818. select
  819. course_id # 找出学号1锁学得所有课程
  820. from
  821. score
  822. where
  823. student_id=1
  824. ) and student_id!=1 # 排除掉学号为1的学生
  825. );
  826. +-----+-----------+
  827. | sid | sname |
  828. +-----+-----------+
  829. | 2 | 艾弗森 |
  830. | 3 | 科比 |
  831. | 4 | curry |
  832. | 5 | james |
  833. | 6 | 李瑞 |
  834. | 7 | 白雪 |
  835. | 8 | 无敌 |
  836. | 9 | 天剑 |
  837. | 10 | egon |
  838. | 11 | alex |
  839. +-----+-----------+
  840.  
  841. -- 24、查询和“2”号同学学习的课程完全相同的其他同学的学号和姓名;
  842. select
  843. sid,
  844. sname
  845. from
  846. student
  847. where
  848. sid in (
  849. select
  850. score.student_id
  851. from
  852. score
  853. inner join
  854. (select
  855. course_id # 课程id
  856. from
  857. score
  858. where
  859. student_id = 2) as t1
  860. where
  861. score.course_id = t1.course_id
  862. and
  863. score.student_id != 2
  864. group by
  865. score.student_id
  866. having
  867. count(score.course_id) = (
  868. select # 学号2所学课程总数
  869. count(course_id)
  870. from
  871. score
  872. where
  873. student_id =2
  874. )
  875. );
  876. +-----+--------+
  877. | sid | sname |
  878. +-----+--------+
  879. | 1 | 乔丹 |
  880. | 3 | 科比 |
  881. +-----+--------+
  882.  
  883. -- 25、删除学习“张三”老师课的score表记录;
  884. select
  885. course.cid
  886. from
  887. course
  888. left join
  889. teacher
  890. on course.teacher_id=teacher.tid
  891. where
  892. teacher.tname='张三';
  893. +-----+
  894. | cid |
  895. +-----+
  896. | 1 |
  897. | 2 |
  898. +-----+
  899. # 删除course_id为1,2的记录
  900. delete from
  901. score
  902. where
  903. course_id in (
  904. select
  905. course.cid
  906. from
  907. course
  908. left join
  909. teacher
  910. on course.teacher_id=teacher.tid
  911. where
  912. teacher.tname='张三'
  913. );
  914. +-----+------------+-----------+-------+
  915. | sid | student_id | course_id | score |
  916. +-----+------------+-----------+-------+
  917. | 3 | 1 | 3 | 58 |
  918. | 4 | 1 | 4 | 71 |
  919. | 5 | 1 | 5 | 68 |
  920. | 6 | 1 | 6 | 100 |
  921. | 9 | 2 | 3 | 71 |
  922. | 10 | 2 | 4 | 68 |
  923. | 11 | 2 | 5 | 92 |
  924. | 12 | 2 | 6 | 88 |
  925. | 15 | 3 | 3 | 72 |
  926. | 16 | 3 | 4 | 88 |
  927. | 17 | 3 | 5 | 92 |
  928. | 18 | 3 | 6 | 12 |
  929. | 20 | 4 | 4 | 78 |
  930. | 21 | 4 | 5 | 34 |
  931. | 23 | 5 | 4 | 38 |
  932. | 24 | 5 | 5 | 44 |
  933. | 26 | 6 | 4 | 32 |
  934. | 27 | 6 | 5 | 0 |
  935. | 29 | 7 | 3 | 60 |
  936. | 30 | 7 | 6 | 45 |
  937. | 32 | 8 | 3 | 65 |
  938. | 33 | 8 | 6 | 99 |
  939. | 35 | 9 | 3 | 69 |
  940. | 36 | 9 | 5 | 78 |
  941. | 38 | 10 | 3 | 69 |
  942. | 39 | 10 | 5 | 90 |
  943. | 41 | 11 | 3 | 89 |
  944. | 42 | 11 | 5 | 100 |
  945. +-----+------------+-----------+-------+
  946.  
  947. -- 26、向score表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“2”课程的同学学号;②插入“2”号课程的平均成绩;
  948. select
  949. sid
  950. from
  951. student
  952. where
  953. sid not in (
  954. select
  955. student_id
  956. from
  957. score
  958. where
  959. course_id =2
  960. );
  961. +-----+
  962. | sid |
  963. +-----+
  964. | 1 |
  965. | 2 |
  966. | 3 |
  967. | 4 |
  968. | 5 |
  969. | 6 |
  970. | 7 |
  971. | 8 |
  972. | 9 |
  973. | 10 |
  974. | 11 |
  975. +-----+
  976. select
  977. avg(score) as avg_score
  978. from
  979. score
  980. where
  981. course_id = 2;
  982. +-----------+
  983. | avg_score |
  984. +-----------+
  985. | NULL |
  986. +-----------+
  987.  
  988. insert into score(student_id, course_id, score)
  989. select
  990. t1.sid,
  991. 2,
  992. t2.avg_score
  993. from
  994. (select
  995. sid
  996. from
  997. student
  998. where
  999. sid not in (
  1000. select
  1001. student_id
  1002. from
  1003. score
  1004. where
  1005. course_id =2
  1006. )
  1007. ) as t1,
  1008. (select
  1009. avg(score) as avg_score
  1010. from
  1011. score
  1012. where
  1013. course_id = 2
  1014. ) as t2;
  1015. # 增加以下记录
  1016. +-----+------------+-----------+-------+
  1017. | sid | student_id | course_id | score |
  1018. +-----+------------+-----------+-------+
  1019. | 3 | 1 | 3 | 58 |
  1020. .......
  1021. |
  1022. | 42 | 11 | 5 | 100 |
  1023. | 43 | 1 | 2 | NULL |
  1024. | 44 | 2 | 2 | NULL |
  1025. | 45 | 3 | 2 | NULL |
  1026. | 46 | 4 | 2 | NULL |
  1027. | 47 | 5 | 2 | NULL |
  1028. | 48 | 6 | 2 | NULL |
  1029. | 49 | 7 | 2 | NULL |
  1030. | 50 | 8 | 2 | NULL |
  1031. | 51 | 9 | 2 | NULL |
  1032. | 52 | 10 | 2 | NULL |
  1033. | 53 | 11 | 2 | NULL |
  1034. +-----+------------+-----------+-------+
  1035. # 将这些空值都修改为73
  1036.  
  1037. -- 27、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;
  1038. # 这个题难度很大
  1039. select
  1040. main_score.student_id,
  1041. (select
  1042. score.score
  1043. from
  1044. score
  1045. left join
  1046. course
  1047. on score.course_id=course.cid
  1048. where
  1049. course.cname='语文'
  1050. and score.student_id=main_score.student_id) as chinese,
  1051. (select
  1052. score.score
  1053. from
  1054. score
  1055. left join
  1056. course
  1057. on score.course_id=course.cid
  1058. where
  1059. course.cname='数学'
  1060. and score.student_id=main_score.student_id) as math,
  1061. (select
  1062. score.score
  1063. from
  1064. score
  1065. left join
  1066. course
  1067. on score.course_id=course.cid
  1068. where
  1069. course.cname='英语'
  1070. and score.student_id=main_score.student_id) as english,
  1071. count(main_score.course_id),
  1072. avg(main_score.score)
  1073. from
  1074. score as main_score
  1075. group by
  1076. main_score.student_id
  1077. order by # 注意order的拼写
  1078. avg(main_score.score) ASC;
  1079. +------------+---------+------+---------+-----------------------------+-----------------------+
  1080. | student_id | chinese | math | english | count(main_score.course_id) | avg(main_score.score) |
  1081. +------------+---------+------+---------+-----------------------------+-----------------------+
  1082. | 6 | 32 | 0 | NULL | 3 | 35.0000 |
  1083. | 5 | 38 | 44 | NULL | 3 | 51.6667 |
  1084. | 7 | NULL | NULL | NULL | 3 | 59.3333 |
  1085. | 4 | 78 | 34 | NULL | 3 | 61.6667 |
  1086. | 3 | 88 | 92 | NULL | 5 | 67.4000 |
  1087. | 9 | NULL | 78 | NULL | 3 | 73.3333 |
  1088. | 1 | 71 | 68 | NULL | 5 | 74.0000 |
  1089. | 10 | NULL | 90 | NULL | 3 | 77.3333 |
  1090. | 2 | 68 | 92 | NULL | 5 | 78.4000 |
  1091. | 8 | NULL | NULL | NULL | 3 | 79.0000 |
  1092. | 11 | NULL | 100 | NULL | 3 | 87.3333 |
  1093. +------------+---------+------+---------+-----------------------------+-----------------------+
  1094.  
  1095. -- 28、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
  1096. select
  1097. course_id,
  1098. max(score.score) as max_score,
  1099. min(score.score) as min_score
  1100. from
  1101. course
  1102. left join
  1103. score
  1104. on course.cid=score.course_id
  1105. group by
  1106. score.course_id;
  1107. +-----------+-----------+-----------+
  1108. | course_id | max_score | min_score |
  1109. +-----------+-----------+-----------+
  1110. | NULL | NULL | NULL |
  1111. | 2 | 73 | 73 |
  1112. | 3 | 89 | 58 |
  1113. | 4 | 88 | 32 |
  1114. | 5 | 100 | 0 |
  1115. | 6 | 100 | 12 |
  1116. +-----------+-----------+-----------+
  1117.  
  1118. -- 29、按各科平均成绩从低到高和及格率的百分数从高到低顺序;
  1119. select
  1120. course_id,
  1121. avg(score) as avg_score
  1122. from
  1123. score
  1124. group by
  1125. course_id
  1126. order by
  1127. avg(score) desc;
  1128. +-----------+-----------+
  1129. | course_id | avg_score |
  1130. +-----------+-----------+
  1131. | 2 | 73.0000 |
  1132. | 3 | 69.1250 |
  1133. | 6 | 68.8000 |
  1134. | 5 | 66.4444 |
  1135. | 4 | 62.5000 |
  1136. +-----------+-----------+
  1137.  
  1138. -- 30、课程平均分从高到低显示(显示任课老师);
  1139. select
  1140. t2.cid,
  1141. t2.cname,
  1142. t2.avg_score,
  1143. teacher.tname
  1144. from
  1145. teacher
  1146. right join (
  1147. select
  1148. course.cid,
  1149. course.cname,
  1150. t1.avg_score,
  1151. course.teacher_id
  1152. from
  1153. course
  1154. inner join (
  1155. select
  1156. course_id,
  1157. avg(score) avg_score
  1158. from
  1159. score
  1160. group by
  1161. course_id
  1162. ) as t1
  1163. on course.cid=t1.course_id
  1164. ) as t2
  1165. on teacher.tid=t2.teacher_id
  1166. order by
  1167. t2.avg_score desc;
  1168. +-----+--------+-----------+--------+
  1169. | cid | cname | avg_score | tname |
  1170. +-----+--------+-----------+--------+
  1171. | 2 | 体育 | 73.0000 | 张三 |
  1172. | 3 | 物理 | 69.1250 | 李四 |
  1173. | 6 | 外语 | 68.8000 | 马七 |
  1174. | 5 | 数学 | 66.4444 | 赵六 |
  1175. | 4 | 语文 | 62.5000 | 王五 |
  1176. +-----+--------+-----------+--------+
  1177.  
  1178. -- 31、查询各科成绩前三名的记录(不考虑成绩并列情况)
  1179. select
  1180. score.sid,score.course_id,score.score,T.first_num,T.second_num
  1181. from
  1182. score
  1183. left join (
  1184. select
  1185. sid,
  1186. (select
  1187. score
  1188. from
  1189. score as s2
  1190. where s2.course_id = s1.course_id
  1191. order by score desc
  1192. limit 0,1) as first_num,
  1193. (select
  1194. score
  1195. from
  1196. score as s2
  1197. where s2.course_id = s1.course_id
  1198. order by score desc
  1199. limit 3,1) as second_num
  1200. from
  1201. score as s1
  1202. ) as T
  1203. on score.sid =T.sid
  1204. where score.score <= T.first_num and score.score >= T.second_num;
  1205. +-----+-----------+-------+-----------+------------+
  1206. | sid | course_id | score | first_num | second_num |
  1207. +-----+-----------+-------+-----------+------------+
  1208. | 43 | 2 | 73 | 73 | 73 |
  1209. | 44 | 2 | 73 | 73 | 73 |
  1210. | 45 | 2 | 73 | 73 | 73 |
  1211. | 46 | 2 | 73 | 73 | 73 |
  1212. | 47 | 2 | 73 | 73 | 73 |
  1213. | 48 | 2 | 73 | 73 | 73 |
  1214. | 49 | 2 | 73 | 73 | 73 |
  1215. | 50 | 2 | 73 | 73 | 73 |
  1216. | 51 | 2 | 73 | 73 | 73 |
  1217. | 52 | 2 | 73 | 73 | 73 |
  1218. | 53 | 2 | 73 | 73 | 73 |
  1219. | 9 | 3 | 71 | 89 | 69 |
  1220. | 15 | 3 | 72 | 89 | 69 |
  1221. | 35 | 3 | 69 | 89 | 69 |
  1222. | 38 | 3 | 69 | 89 | 69 |
  1223. | 41 | 3 | 89 | 89 | 69 |
  1224. | 4 | 4 | 71 | 88 | 68 |
  1225. | 10 | 4 | 68 | 88 | 68 |
  1226. | 16 | 4 | 88 | 88 | 68 |
  1227. | 20 | 4 | 78 | 88 | 68 |
  1228. | 11 | 5 | 92 | 100 | 90 |
  1229. | 17 | 5 | 92 | 100 | 90 |
  1230. | 39 | 5 | 90 | 100 | 90 |
  1231. | 42 | 5 | 100 | 100 | 90 |
  1232. | 6 | 6 | 100 | 100 | 45 |
  1233. | 12 | 6 | 88 | 100 | 45 |
  1234. | 30 | 6 | 45 | 100 | 45 |
  1235. | 33 | 6 | 99 | 100 | 45 |
  1236. +-----+-----------+-------+-----------+------------+
  1237. # 需要进一步改进
  1238. select
  1239. course_id,
  1240. (select
  1241. score
  1242. from
  1243. score as s2
  1244. where s2.course_id = s1.course_id
  1245. order by score desc
  1246. limit 1) as first_num,
  1247. (select
  1248. score
  1249. from
  1250. score as s2
  1251. where s2.course_id = s1.course_id
  1252. order by score desc
  1253. limit 1,1) as secend_num,
  1254. (select
  1255. score
  1256. from
  1257. score as s2
  1258. where s2.course_id = s1.course_id
  1259. order by score desc
  1260. limit 2,1) as third_num
  1261. from
  1262. score as s1;
  1263. +-----------+-----------+------------+-----------+
  1264. | course_id | first_num | secend_num | third_num |
  1265. +-----------+-----------+------------+-----------+
  1266. | 2 | 73 | 73 | 73 |
  1267. | 2 | 73 | 73 | 73 |
  1268. | 2 | 73 | 73 | 73 |
  1269. | 2 | 73 | 73 | 73 |
  1270. | 2 | 73 | 73 | 73 |
  1271. | 2 | 73 | 73 | 73 |
  1272. | 2 | 73 | 73 | 73 |
  1273. | 2 | 73 | 73 | 73 |
  1274. | 2 | 73 | 73 | 73 |
  1275. | 2 | 73 | 73 | 73 |
  1276. | 2 | 73 | 73 | 73 |
  1277. | 3 | 89 | 72 | 71 |
  1278. | 3 | 89 | 72 | 71 |
  1279. | 3 | 89 | 72 | 71 |
  1280. | 3 | 89 | 72 | 71 |
  1281. | 3 | 89 | 72 | 71 |
  1282. | 3 | 89 | 72 | 71 |
  1283. | 3 | 89 | 72 | 71 |
  1284. | 3 | 89 | 72 | 71 |
  1285. | 4 | 88 | 78 | 71 |
  1286. | 4 | 88 | 78 | 71 |
  1287. | 4 | 88 | 78 | 71 |
  1288. | 4 | 88 | 78 | 71 |
  1289. | 4 | 88 | 78 | 71 |
  1290. | 4 | 88 | 78 | 71 |
  1291. | 5 | 100 | 92 | 92 |
  1292. | 5 | 100 | 92 | 92 |
  1293. | 5 | 100 | 92 | 92 |
  1294. | 5 | 100 | 92 | 92 |
  1295. | 5 | 100 | 92 | 92 |
  1296. | 5 | 100 | 92 | 92 |
  1297. | 5 | 100 | 92 | 92 |
  1298. | 5 | 100 | 92 | 92 |
  1299. | 5 | 100 | 92 | 92 |
  1300. | 6 | 100 | 99 | 88 |
  1301. | 6 | 100 | 99 | 88 |
  1302. | 6 | 100 | 99 | 88 |
  1303. | 6 | 100 | 99 | 88 |
  1304. | 6 | 100 | 99 | 88 |
  1305. +-----------+-----------+------------+-----------+
  1306.  
  1307. select
  1308. course_id
  1309.  
  1310. from
  1311. (select
  1312. course_id,
  1313. (select
  1314. score
  1315. from
  1316. score as s2
  1317. where s2.course_id = s1.course_id
  1318. order by score desc
  1319. limit 1) as first_num,
  1320. (select
  1321. score
  1322. from
  1323. score as s2
  1324. where s2.course_id = s1.course_id
  1325. order by score desc
  1326. limit 1,1) as secend_num,
  1327. (select
  1328. score
  1329. from
  1330. score as s2
  1331. where s2.course_id = s1.course_id
  1332. order by score desc
  1333. limit 2,1) as third_num
  1334. from
  1335. score as s1
  1336. ) as T
  1337. group by course_id;
  1338.  
  1339. -- 32、查询每门课程被选修的学生数;
  1340. select
  1341. course_id,
  1342. count(student_id)
  1343. from
  1344. score
  1345. group by
  1346. course_id;
  1347. +-----------+-------------------+
  1348. | course_id | count(student_id) |
  1349. +-----------+-------------------+
  1350. | 2 | 11 |
  1351. | 3 | 8 |
  1352. | 4 | 6 |
  1353. | 5 | 9 |
  1354. | 6 | 5 |
  1355. +-----------+-------------------+
  1356.  
  1357. -- 33、查询选修了2门以上课程的全部学生的学号和姓名;
  1358. select
  1359. sid,
  1360. sname
  1361. from
  1362. student
  1363. where sid in (
  1364. select
  1365. student_id
  1366. from
  1367. score
  1368. group by
  1369. student_id
  1370. having count(course_id) > 2
  1371. );
  1372. +-----+-----------+
  1373. | sid | sname |
  1374. +-----+-----------+
  1375. | 1 | 乔丹 |
  1376. | 2 | 艾弗森 |
  1377. | 3 | 科比 |
  1378. | 4 | curry |
  1379. | 5 | james |
  1380. | 6 | 李瑞 |
  1381. | 7 | 白雪 |
  1382. | 8 | 无敌 |
  1383. | 9 | 天剑 |
  1384. | 10 | egon |
  1385. | 11 | alex |
  1386. +-----+-----------+
  1387.  
  1388. -- 34、查询男生、女生的人数,按倒序排列;
  1389. select
  1390. gender,
  1391. count(sid)
  1392. from
  1393. student
  1394. group by gender
  1395. order by count(sid) desc;
  1396. +--------+------------+
  1397. | gender | count(sid) |
  1398. +--------+------------+
  1399. | | 6 |
  1400. | | 5 |
  1401. +--------+------------+
  1402.  
  1403. -- 35、查询姓“张”的学生名单;
  1404. select
  1405. *
  1406. from
  1407. student
  1408. where
  1409. sname='张%';
  1410. Empty set (0.00 sec)
  1411.  
  1412. -- 36、查询同名同姓学生名单,并统计同名人数;
  1413. select
  1414. sname,
  1415. count(sid)
  1416. from
  1417. student
  1418. group by
  1419. sname
  1420. having
  1421. count(sid) > 1;
  1422. Empty set (0.00 sec)
  1423.  
  1424. -- 37、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
  1425. select
  1426. course_id,
  1427. avg(score)
  1428. from
  1429. score
  1430. group by
  1431. course_id
  1432. order by
  1433. avg(score), # 两种排序规则需要用逗号分隔
  1434. course_id DESC;
  1435. +-----------+------------+
  1436. | course_id | avg(score) |
  1437. +-----------+------------+
  1438. | 4 | 62.5000 |
  1439. | 5 | 66.4444 |
  1440. | 6 | 68.8000 |
  1441. | 3 | 69.1250 |
  1442. | 2 | 73.0000 |
  1443. +-----------+------------+
  1444.  
  1445. -- 38、查询课程名称为“数学”,且分数低于60的学生姓名和分数;
  1446. select
  1447. student.sname,
  1448. score.score
  1449. from
  1450. score
  1451. inner join
  1452. student
  1453. on score.student_id=student.sid
  1454. where score.course_id in (
  1455. select
  1456. cid
  1457. from
  1458. course
  1459. where
  1460. cname='数学'
  1461. );
  1462. +-----------+-------+
  1463. | sname | score |
  1464. +-----------+-------+
  1465. | 乔丹 | 68 |
  1466. | 艾弗森 | 92 |
  1467. | 科比 | 92 |
  1468. | curry | 34 |
  1469. | james | 44 |
  1470. | 李瑞 | 0 |
  1471. | 天剑 | 78 |
  1472. | egon | 90 |
  1473. | alex | 100 |
  1474. +-----------+-------+
  1475.  
  1476. -- 39、查询课程编号为“3”且课程成绩在80分以上的学生的学号和姓名;
  1477. select
  1478. sid,
  1479. sname
  1480. from
  1481. student
  1482. where sid in (
  1483. select
  1484. student_id
  1485. from
  1486. score
  1487. where
  1488. course_id=3
  1489. and score>=80
  1490. );
  1491. +-----+-------+
  1492. | sid | sname |
  1493. +-----+-------+
  1494. | 11 | alex |
  1495. +-----+-------+
  1496.  
  1497. -- 40、求选修了课程的学生人数
  1498. select
  1499. course_id,
  1500. count(student_id)
  1501. from
  1502. score
  1503. group by
  1504. course_id;
  1505. +-----------+-------------------+
  1506. | course_id | count(student_id) |
  1507. +-----------+-------------------+
  1508. | 2 | 11 |
  1509. | 3 | 8 |
  1510. | 4 | 6 |
  1511. | 5 | 9 |
  1512. | 6 | 5 |
  1513. +-----------+-------------------+
  1514.  
  1515. -- 41、查询选修“王五”老师所授课程的学生中,成绩最高和最低的学生姓名及其成绩;
  1516. select
  1517. *
  1518. from
  1519. (select
  1520. *
  1521. from
  1522. student
  1523. inner join
  1524. (select
  1525. student_id,
  1526. score
  1527. from
  1528. score
  1529. where
  1530. course_id in (
  1531. select
  1532. cid
  1533. from
  1534. teacher
  1535. inner join
  1536. course
  1537. on teacher.tid=course.teacher_id
  1538. where
  1539. tname='王五'
  1540. )
  1541. ) as t1
  1542. on student.sid=t1.student_id
  1543. order by
  1544. score
  1545. limit 1
  1546. ) as t2
  1547. union
  1548. select
  1549. *
  1550. from
  1551. (select
  1552. *
  1553. from
  1554. student
  1555. inner join
  1556. (select
  1557. student_id,
  1558. score
  1559. from
  1560. score
  1561. where
  1562. course_id in (
  1563. select
  1564. cid
  1565. from
  1566. teacher
  1567. inner join
  1568. course
  1569. on teacher.tid=course.teacher_id
  1570. where
  1571. tname='王五'
  1572. )
  1573. ) as t1
  1574. on student.sid=t1.student_id
  1575. order by
  1576. score DESC
  1577. limit 1
  1578. ) as t3;
  1579. +-----+--------+--------+----------+------------+-------+
  1580. | sid | sname | gender | class_id | student_id | score |
  1581. +-----+--------+--------+----------+------------+-------+
  1582. | 6 | 李瑞 | | 4 | 6 | 32 |
  1583. | 3 | 科比 | | 2 | 3 | 88 |
  1584. +-----+--------+--------+----------+------------+-------+
  1585.  
  1586. -- 42、查询各个课程及相应的选修人数;
  1587. select
  1588. cid,
  1589. cname,
  1590. t1.stu_num
  1591. from
  1592. course
  1593. right join
  1594. (select
  1595. course_id,
  1596. count(student_id) as stu_num
  1597. from
  1598. score
  1599. group by
  1600. course_id) as t1
  1601. on course.cid=t1.course_id;
  1602. +------+--------+---------+
  1603. | cid | cname | stu_num |
  1604. +------+--------+---------+
  1605. | 2 | 体育 | 11 |
  1606. | 3 | 物理 | 8 |
  1607. | 4 | 语文 | 6 |
  1608. | 5 | 数学 | 9 |
  1609. | 6 | 外语 | 5 |
  1610. +------+--------+---------+
  1611.  
  1612. -- 43、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
  1613. select
  1614. *
  1615. from
  1616. score
  1617. where
  1618. score in (
  1619. select
  1620. score # 找到不同课程但是有相同分数的成绩数值
  1621. from
  1622. score
  1623. group by
  1624. score
  1625. having
  1626. count(course_id)>1
  1627. )
  1628. order by score;
  1629. +-----+------------+-----------+-------+
  1630. | sid | student_id | course_id | score |
  1631. +-----+------------+-----------+-------+
  1632. | 5 | 1 | 5 | 68 |
  1633. | 10 | 2 | 4 | 68 |
  1634. | 35 | 9 | 3 | 69 |
  1635. | 38 | 10 | 3 | 69 |
  1636. | 4 | 1 | 4 | 71 |
  1637. | 9 | 2 | 3 | 71 |
  1638. | 43 | 1 | 2 | 73 |
  1639. | 44 | 2 | 2 | 73 |
  1640. | 45 | 3 | 2 | 73 |
  1641. | 46 | 4 | 2 | 73 |
  1642. | 47 | 5 | 2 | 73 |
  1643. | 48 | 6 | 2 | 73 |
  1644. | 49 | 7 | 2 | 73 |
  1645. | 50 | 8 | 2 | 73 |
  1646. | 51 | 9 | 2 | 73 |
  1647. | 52 | 10 | 2 | 73 |
  1648. | 53 | 11 | 2 | 73 |
  1649. | 20 | 4 | 4 | 78 |
  1650. | 36 | 9 | 5 | 78 |
  1651. | 12 | 2 | 6 | 88 |
  1652. | 16 | 3 | 4 | 88 |
  1653. | 11 | 2 | 5 | 92 |
  1654. | 17 | 3 | 5 | 92 |
  1655. | 6 | 1 | 6 | 100 |
  1656. | 42 | 11 | 5 | 100 |
  1657. +-----+------------+-----------+-------+
  1658.  
  1659. -- 44、查询每门课程成绩最好的前两名学生id和姓名;
  1660. select
  1661. student.sid,
  1662. student.sname,
  1663. t2.course_id,
  1664. t2.score,
  1665. t2.first_score,
  1666. t2.second_score
  1667. from
  1668. student
  1669. inner join ( # 这里会出现成绩相同的情况,录入了超过了两名的学生!!
  1670. select
  1671. score.student_id,
  1672. score.course_id,
  1673. score.score,
  1674. t1.first_score,
  1675. t1.second_score
  1676. from
  1677. score
  1678. inner join (
  1679. select
  1680. s1.sid,
  1681. (select s2.score from score as s2 where s1.course_id = s2.course_id order by s2.score desc limit 0,1) as first_score,
  1682. (select s3.score from score as s3 where s1.course_id = s3.course_id order by s3.score desc limit 1,1) as second_score
  1683. from
  1684. score as s1
  1685. ) as t1 on score.sid = t1.sid
  1686. where
  1687. score.score in ( # ??在这,会超过两个!!
  1688. t1.first_score,
  1689. t1.second_score
  1690. )
  1691. ) as t2 on student.sid = t2.student_id;
  1692. +-----+-----------+-----------+-------+-------------+--------------+
  1693. | sid | sname | course_id | score | first_score | second_score |
  1694. +-----+-----------+-----------+-------+-------------+--------------+
  1695. | 1 | 乔丹 | 2 | 73 | 73 | 73 |
  1696. | 2 | 艾弗森 | 2 | 73 | 73 | 73 |
  1697. | 3 | 科比 | 2 | 73 | 73 | 73 |
  1698. | 4 | curry | 2 | 73 | 73 | 73 |
  1699. | 5 | james | 2 | 73 | 73 | 73 |
  1700. | 6 | 李瑞 | 2 | 73 | 73 | 73 |
  1701. | 7 | 白雪 | 2 | 73 | 73 | 73 |
  1702. | 8 | 无敌 | 2 | 73 | 73 | 73 |
  1703. | 9 | 天剑 | 2 | 73 | 73 | 73 |
  1704. | 10 | egon | 2 | 73 | 73 | 73 |
  1705. | 11 | alex | 2 | 73 | 73 | 73 |
  1706. | 3 | 科比 | 3 | 72 | 89 | 72 |
  1707. | 11 | alex | 3 | 89 | 89 | 72 |
  1708. | 3 | 科比 | 4 | 88 | 88 | 78 |
  1709. | 4 | curry | 4 | 78 | 88 | 78 |
  1710. | 2 | 艾弗森 | 5 | 92 | 100 | 92 |
  1711. | 3 | 科比 | 5 | 92 | 100 | 92 |
  1712. | 11 | alex | 5 | 100 | 100 | 92 |
  1713. | 1 | 乔丹 | 6 | 100 | 100 | 99 |
  1714. | 8 | 无敌 | 6 | 99 | 100 | 99 |
  1715. +-----+-----------+-----------+-------+-------------+--------------+
  1716.  
  1717. -- 45、检索至少选修两门课程的学生学号;
  1718. select
  1719. student_id
  1720. from
  1721. score
  1722. group by
  1723. student_id
  1724. having
  1725. count(course_id)>=2;
  1726. +------------+
  1727. | student_id |
  1728. +------------+
  1729. | 1 |
  1730. | 2 |
  1731. | 3 |
  1732. | 4 |
  1733. | 5 |
  1734. | 6 |
  1735. | 7 |
  1736. | 8 |
  1737. | 9 |
  1738. | 10 |
  1739. | 11 |
  1740. +------------+
  1741.  
  1742. -- 46、查询没有学生选修的课程的课程号和课程名;
  1743. select
  1744. cid,
  1745. cname
  1746. from
  1747. course
  1748. where
  1749. cid not in (
  1750. select
  1751. course_id
  1752. from
  1753. score
  1754. group by
  1755. course_id
  1756. );
  1757. +-----+--------+
  1758. | cid | cname |
  1759. +-----+--------+
  1760. | 1 | 生物 |
  1761. +-----+--------+
  1762.  
  1763. -- 47、查询没带过任何班级的老师id和姓名;
  1764. select
  1765. tid,
  1766. tname
  1767. from
  1768. teacher
  1769. where
  1770. tid not in(
  1771. select
  1772. tid
  1773. from
  1774. teach2cls
  1775. group by
  1776. tid
  1777. );
  1778. Empty set (0.00 sec)
  1779.  
  1780. -- 48、查询有两门以上课程超过80分的学生id及其平均成绩;
  1781. select
  1782. student_id,
  1783. avg(score) as avg_score
  1784. from
  1785. score
  1786. where score.student_id in
  1787. (select
  1788. student_id # 找到有两门课大于80的学号
  1789. from
  1790. (select
  1791. *
  1792. from
  1793. score
  1794. where
  1795. score>80) as t1
  1796. group by
  1797. student_id
  1798. having
  1799. count(course_id) >= 2
  1800. )
  1801. group by student_id;
  1802. +------------+-----------+
  1803. | student_id | avg_score |
  1804. +------------+-----------+
  1805. | 2 | 78.4000 |
  1806. | 3 | 67.4000 |
  1807. | 11 | 87.3333 |
  1808. +------------+-----------+
  1809.  
  1810. -- 49、检索“3”课程分数小于60,按分数降序排列的同学学号;
  1811. select
  1812. student_id
  1813. from
  1814. score
  1815. where
  1816. course_id=3
  1817. and score<60
  1818. order by score DESC;
  1819. +------------+
  1820. | student_id |
  1821. +------------+
  1822. | 1 |
  1823. +------------+
  1824.  
  1825. -- 50、删除编号为“2”的同学的“1”课程的成绩;
  1826. select
  1827. sid
  1828. from
  1829. score
  1830. where
  1831. student_id=2
  1832. and course_id=1;
  1833.  
  1834. delete from
  1835. score
  1836. where
  1837. sid in (
  1838. select
  1839. sid
  1840. from
  1841. score
  1842. where
  1843. student_id=2
  1844. and course_id=1
  1845. );
  1846. ERROR 1093 (HY000): You can't specify target table 'score' for update in FROM clause'
  1847. delete from
  1848. score
  1849. where
  1850. sid in (
  1851. select
  1852. t1.sid
  1853. from (
  1854. select
  1855. sid
  1856. from
  1857. score
  1858. where
  1859. student_id=2
  1860. and course_id=1
  1861. ) as t1
  1862. );
  1863. Query OK, 0 rows affected (0.00 sec)
  1864.  
  1865. -- 51、查询同时选修了物理课和生物课的学生id和姓名;
  1866. select
  1867. cid
  1868. from
  1869. course
  1870. where
  1871. cname='物理'
  1872. or cname='生物';
  1873. +-----+--------+------------+
  1874. | cid | cname | teacher_id |
  1875. +-----+--------+------------+
  1876. | 1 | 生物 | 1 |
  1877. | 3 | 物理 | 2 |
  1878. +-----+--------+------------+
  1879.  
  1880. select
  1881. sid,
  1882. sname
  1883. from
  1884. student
  1885. where
  1886. sid in (
  1887. select
  1888. student_id
  1889. FROM
  1890. score
  1891. where
  1892. course_id in (
  1893. select
  1894. cid
  1895. from
  1896. course
  1897. where
  1898. cname='物理'
  1899. or cname='生物'
  1900. )
  1901. group by
  1902. student_id
  1903. having
  1904. count(course_id)=2
  1905. );
  1906. Empty set (0.00 sec)

SQL作业题及答案

mysql-作业的更多相关文章

  1. Mysql 作业(Scheduler)

    200 ? "200px" : this.width)!important;} --> 介绍 作业也叫做事件调度,其实它也就是一个时间触发器:它可以定义某个时间点执行指定的数 ...

  2. mysql定时任务/mysql作业

    转自:https://www.jb51.net/article/138569.htm 详细参考:https://www.cnblogs.com/qlqwjy/p/7954175.html(事件& ...

  3. MySQL作业

    创建作业事件 MONTH STARTS '2015-01-01 05:30:01' ON COMPLETION NOT PRESERVE ENABLE DO CALL sp_moveLoginReco ...

  4. MySQL(作业练习)

    day59 参考:http://www.cnblogs.com/wupeiqi/p/5748496.html 现有数据库 /* Navicat Premium Data Transfer Source ...

  5. mysql安装及基本操作(mysql作业)

    1 官网下载,链接  https://www.mysql.com/downloads/ Download MySQL Community Server 默认为你选好了Mac OS X 平台 选择的是. ...

  6. 【MySQL作业】MySQL函数——美和易思系统信息函数和加密函数应用习题

    点击打开所使用到的数据库>>> 1.显示当前 MySQL 服务器的版本信息和登录信息. MySQL 系统信息函数 version() 用于返回当前 MySQL 的版本号," ...

  7. 【MySQL作业】MySQL函数——美和易思日期和时间函数应用习题

    点击打开所使用到的数据库>>> 1.采用尽可能多的方式显示当前系统日期和时间. 下列 SQL 语句可以显示当前系统的日期和时间: curdate() 和 current_date() ...

  8. 【MySQL作业】MySQL函数——美和易思字符串函数应用习题

    点击打开所使用到的数据库>>> 1.将所有客户的姓名与电话以"-"作为分隔符进行连接显示. 使用 concat(s1,s2,-) 函数将所有客户的姓名与电话以&q ...

  9. 【MySQL作业】MySQL函数——美和易思数学函数和控制流函数应用习题

    点击打开所使用到的数据库>>> 1.添加一条商品记录.  商品编码  goodsCode 商品名 goodsName 种类 category 单价 unitPrice 02005 夏 ...

  10. 【MySQL作业】DDL 和 DML——美和易思使用 DML 删除表数据应用习题

    点击打开所使用到的数据库>>> 删除客户"刘一鸣". 执行 SQL 代码"delete from customer where cName=' 刘一鸣 ...

随机推荐

  1. #410(div2)B. Mike and strings

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  2. 局域网中使用的IP地址有哪些?

    当我们建设一个局域网的时候,需要为网络中的每台计算机分配一个IP地址.那么都有哪些IP地址可以使用在局域网中呢?局域网中的IP地址有什么规定呢? 在局域网中,我们是不能使用如202.106.45.11 ...

  3. p4171&bzoj1823 满汉全席

    传送门(洛谷) 传送门(bzoj) 题目 满汉全席是中国最丰盛的宴客菜肴,有许多种不同的材料透过满族或是汉族的料理方式,呈现在數量繁多的菜色之中.由于菜色众多而繁杂,只有极少數博学多闻技艺高超的厨师能 ...

  4. 22. Bypass X-WAF SQL注入防御(多姿势)

    0x00 前言 X-WAF是一款适用中.小企业的云WAF系统,让中.小企业也可以非常方便地拥有自己的免费云WAF. 本文从代码出发,一步步理解WAF的工作原理,多姿势进行WAF Bypass. 0x0 ...

  5. 用ORBSLAM2运行TUM Dataset数据集Monocular Examples

    参照https://github.com/raulmur/ORB_SLAM2/blob/master/README.md 运行 4. Monocular Examples TUM Dataset 数据 ...

  6. .net core 中使用NLog

    在.net standard 2.0.3 和.net core 2.1适用.其他版本的.net 应该也可以. 1.新建一个空白解决方案,再建一个类库 2.安装NLog.Config,会生成一个配置文件 ...

  7. GoWeb开发_Iris框架讲解(四):Iris框架设置操作

    路由组的使用 在实际开发中,我们通常都是按照模块进行开发,同一模块的不同接口url往往是最后的一级url不同,具有相同的前缀url.因此,我们期望在后台开发中,可以按照模块来进行处理我们的请求,对于这 ...

  8. Unity---UNet学习(1)----基本方法介绍

    目录 1.Network Manager 2.Network Manager HUD 3.Network Identity 4.Network Transform 5.特性 1.Network Man ...

  9. CF446B DZY Loves Modification 优先队列

    As we know, DZY loves playing games. One day DZY decided to play with a n × m matrix. To be more pre ...

  10. Exadata 上关于SAS盘的小秘密

    案例概述 一个X3-2 的Exadata临时客户,ORACLE原厂工程师在进行onecommand初始化的过程中,执行到第6步,calibrate检测存储节点磁盘性能时报错,后续工作无法继续.而由于一 ...