MySQL多表查詢綜合練習答案

1、綜合練習

1.1 init.sql文件內容

/*
 數據導入:
 Navicat Premium Data Transfer

 Source Server         : localhost
 Source Server Type    : MySQL
 Source Server Version : 50624
 Source Host           : localhost
 Source Database       : sqlexam

 Target Server Type    : MySQL
 Target Server Version : 50624
 File Encoding         : utf-8

 Date: 10/21/2016 06:46:46 AM
*/

SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
--  Table structure for `class`
-- ----------------------------
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (
  `cid` int(11) NOT NULL AUTO_INCREMENT,
  `caption` varchar(32) NOT NULL,
  PRIMARY KEY (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

-- ----------------------------
--  Records of `class`
-- ----------------------------
BEGIN;
INSERT INTO `class` VALUES ('1', '三年二班'), ('2', '三年三班'), ('3', '一年二班'), ('4', '二年九班');
COMMIT;

-- ----------------------------
--  Table structure for `course`
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
  `cid` int(11) NOT NULL AUTO_INCREMENT,
  `cname` varchar(32) NOT NULL,
  `teacher_id` int(11) NOT NULL,
  PRIMARY KEY (`cid`),
  KEY `fk_course_teacher` (`teacher_id`),
  CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

-- ----------------------------
--  Records of `course`
-- ----------------------------
BEGIN;
INSERT INTO `course` VALUES ('1', '生物', '1'), ('2', '物理', '2'), ('3', '體育', '3'), ('4', '美術', '2');
COMMIT;

-- ----------------------------
--  Table structure for `score`
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (
  `sid` int(11) NOT NULL AUTO_INCREMENT,
  `student_id` int(11) NOT NULL,
  `course_id` int(11) NOT NULL,
  `num` int(11) NOT NULL,
  PRIMARY KEY (`sid`),
  KEY `fk_score_student` (`student_id`),
  KEY `fk_score_course` (`course_id`),
  CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`),
  CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`)
) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=utf8;

-- ----------------------------
--  Records of `score`
-- ----------------------------
BEGIN;
INSERT INTO `score` VALUES ('1', '1', '1', '10'), ('2', '1', '2', '9'), ('5', '1', '4', '66'), ('6', '2', '1', '8'), ('8', '2', '3', '68'), ('9', '2', '4', '99'), ('10', '3', '1', '77'), ('11', '3', '2', '66'), ('12', '3', '3', '87'), ('13', '3', '4', '99'), ('14', '4', '1', '79'), ('15', '4', '2', '11'), ('16', '4', '3', '67'), ('17', '4', '4', '100'), ('18', '5', '1', '79'), ('19', '5', '2', '11'), ('20', '5', '3', '67'), ('21', '5', '4', '100'), ('22', '6', '1', '9'), ('23', '6', '2', '100'), ('24', '6', '3', '67'), ('25', '6', '4', '100'), ('26', '7', '1', '9'), ('27', '7', '2', '100'), ('28', '7', '3', '67'), ('29', '7', '4', '88'), ('30', '8', '1', '9'), ('31', '8', '2', '100'), ('32', '8', '3', '67'), ('33', '8', '4', '88'), ('34', '9', '1', '91'), ('35', '9', '2', '88'), ('36', '9', '3', '67'), ('37', '9', '4', '22'), ('38', '10', '1', '90'), ('39', '10', '2', '77'), ('40', '10', '3', '43'), ('41', '10', '4', '87'), ('42', '11', '1', '90'), ('43', '11', '2', '77'), ('44', '11', '3', '43'), ('45', '11', '4', '87'), ('46', '12', '1', '90'), ('47', '12', '2', '77'), ('48', '12', '3', '43'), ('49', '12', '4', '87'), ('52', '13', '3', '87');
COMMIT;

-- ----------------------------
--  Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `sid` int(11) NOT NULL AUTO_INCREMENT,
  `gender` char(1) NOT NULL,
  `class_id` int(11) NOT NULL,
  `sname` varchar(32) NOT NULL,
  PRIMARY KEY (`sid`),
  KEY `fk_class` (`class_id`),
  CONSTRAINT `fk_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;

-- ----------------------------
--  Records of `student`
-- ----------------------------
BEGIN;
INSERT INTO `student` VALUES ('1', '男', '1', '理解'), ('2', '女', '1', '鋼蛋'), ('3', '男', '1', '張三'), ('4', '男', '1', '張一'), ('5', '女', '1', '張二'), ('6', '男', '1', '張四'), ('7', '女', '2', '鐵錘'), ('8', '男', '2', '李三'), ('9', '男', '2', '李一'), ('10', '女', '2', '李二'), ('11', '男', '2', '李四'), ('12', '女', '3', '如花'), ('13', '男', '3', '劉三'), ('14', '男', '3', '劉一'), ('15', '女', '3', '劉二'), ('16', '男', '3', '劉四');
COMMIT;

-- ----------------------------
--  Table structure for `teacher`
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
  `tid` int(11) NOT NULL AUTO_INCREMENT,
  `tname` varchar(32) NOT NULL,
  PRIMARY KEY (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

-- ----------------------------
--  Records of `teacher`
-- ----------------------------
BEGIN;
INSERT INTO `teacher` VALUES ('1', '張磊老師'), ('2', '李平老師'), ('3', '劉海燕老師'), ('4', '朱雲海老師'), ('5', '李傑老師');
COMMIT;

SET FOREIGN_KEY_CHECKS = 1;

1.2 從init.sql文件中導入數據

# 準備表、記錄
mysql> create database db1;
mysql> use db1;
mysql> source /root/init.sql

202-MySQL多表查詢-01.png

1.3 基礎練習

  1. 查詢男生、女生的人數;sql

  2. 查詢姓「張」的學生名單;學習

  3. 課程平均分從高到低顯示code

  4. 查詢有課程成績小於60分的同窗的學號、姓名;regexp

  5. 查詢至少有一門課與學號爲1的同窗所學課程相同的同窗的學號和姓名;blog

  6. 查詢出只選修了一門課程的所有學生的學號和姓名;排序

  7. 查詢各科成績最高和最低的分:以以下形式顯示:課程ID,最高分,最低分;utf-8

  8. 查詢課程編號「2」的成績比課程編號「1」課程低的全部同窗的學號、姓名;ci

  9. 查詢「生物」課程比「物理」課程成績高的全部學生的學號;rem

  10. 查詢平均成績大於60分的同窗的學號和平均成績;

  11. 查詢全部同窗的學號、姓名、選課數、總成績;

  12. 查詢姓「李」的老師的個數;

  13. 查詢沒學過「張磊老師」課的同窗的學號、姓名;

  14. 查詢學過「1」而且也學過編號「2」課程的同窗的學號、姓名;

  15. 查詢學過「李平老師」所教的全部課的同窗的學號、姓名;

1.4 進階練習

  1. 查詢沒有學全全部課的同窗的學號、姓名;
  2. 查詢和「002」號的同窗學習的課程徹底相同的其餘同窗學號和姓名;
  3. 刪除學習「葉平」老師課的SC表記錄;
  4. 向SC表中插入一些記錄,這些記錄要求符合如下條件:①沒有上過編號「002」課程的同窗學號;②插入「002」號課程的平均成績;
  5. 按平均成績從低到高顯示全部學生的「語文」、「數學」、「英語」三門的課程成績,按以下形式顯示: 學生ID,語文,數學,英語,有效課程數,有效平均分;
  6. 查詢各科成績最高和最低的分:以以下形式顯示:課程ID,最高分,最低分;
  7. 按各科平均成績從低到高和及格率的百分數從高到低順序;
  8. 查詢各科成績前三名的記錄:(不考慮成績並列狀況)
  9. 查詢每門課程被選修的學生數;
  10. 查詢同名同姓學生名單,並統計同名人數;
  11. 查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列;
  12. 查詢平均成績大於85的全部學生的學號. 姓名和平均成績;
  13. 查詢課程名稱爲「數學」,且分數低於60的學生姓名和分數;
  14. 查詢課程編號爲003且課程成績在80分以上的學生的學號和姓名;
  15. 求選了課程的學生人數
  16. 查詢選修「楊豔」老師所授課程的學生中,成績最高的學生姓名及其成績;
  17. 查詢各個課程及相應的選修人數;
  18. 查詢不一樣課程但成績相同的學生的學號、課程號、學生成績;
  19. 查詢每門課程成績最好的前兩名;
  20. 檢索至少選修兩門課程的學生學號;
  21. 查詢所有學生都選修的課程的課程號和課程名;
  22. 查詢沒學過「葉平」老師講授的任一門課程的學生姓名;
  23. 查詢兩門以上不及格課程的同窗的學號及其平均成績;
  24. 檢索「004」課程分數小於60,按分數降序排列的同窗學號;
  25. 刪除「002」同窗的「001」課程的成績;

2、基礎練習答案

一、查詢「生物」課程比「物理」課程成績高的全部學生的學號;

select * from( 

 (select * from score where course_id in (select cid from course where cname = '生物')) t1  

left join 

 (select * from score where course_id in (select cid from course where cname = '物理')) t2  

on  t1.student_id = t2.student_id) 

where t1.num > t2.num;

 

二、查詢平均成績大於60分的同窗的學號和平均成績;

# 先查看每一個同窗的平均分數

select student_id,avg(num) from score group by student_id;

# 在篩選成績大於60分的同窗的學號和平均成績;

# select student_id,avg(num) from score group by student_id having avg(num) > 60;

 

三、查詢全部同窗的學號、姓名、選課數、總成績;

# 先查看每一個同窗的總成績

select student_id,sum(num) from score group by student_id;

# 學生和課程的關係只有成績表中存在,所以要獲取每一個學生選擇的課程,須要經過score表

select count(sid),student_id from score group by student_id;

# 將上面兩步合併

select sum(num),count(sid),student_id from score group by student_id;

# 將學生的信息和成績選課狀況拼在一塊兒

select sid,sname,sum_num ,count_stu 

from student  

left join 

 (select sum(num) sum_num,count(sid) count_stu,student_id from score group by student_id) t2  

on  sid = student_id;

# 還能夠更嚴謹,那些沒有選課的同窗選課數和總成績應該是0

select sid,sname,

 (

           CASE

           WHEN sum_num is  null THEN 0   

   ELSE sum_num

           END

         ) as sum_num ,

 (

           CASE

           WHEN count_stu is  null THEN 0   

   ELSE count_stu

           END

         ) as count_stu 

from student  

left join 

 (select sum(num) sum_num,count(sid) count_stu,student_id from score group by student_id) t2  

on  sid = student_id;

 

四、查詢姓「李」的老師的個數;

# 找到全部姓李的

 # 方法一

 # select * from teacher where tname like '李%';

 # 方法二

 # select * from teacher where tname regexp '^李';

# 統計個數

 select count(tid) from teacher where tname regexp '^李';

 或者

 select count(id) from teacher where tname like '李%';

 

五、查詢沒學過「張磊老師」課的同窗的學號、姓名;

# 找到張磊老師的id 

select tid from teacher where tname == '張磊老師';

# 找到張磊老師所教課程

select cid from course where teacher_id = (select tid from teacher where tname = '張磊老師');

# 找到全部學習這門課的學生id

select student_id from score where course_id = (select cid from course where teacher_id = (select tid from teacher where tname = '張磊老師'));

# 找到沒有學過這門課的學生對應的學生學號、姓名

select sid,sname from student where sid not in 

 (select student_id from score where course_id = (select cid from course where teacher_id = (select tid from teacher where tname = '張磊老師'))

);

 

六、查詢學過「1」而且也學過編號「2」課程的同窗的學號、姓名;

# 先查詢學習課程id爲1的全部學生

select * from score where course_id = 1;

# 先查詢學習課程id爲2的全部學生

select * from score where course_id = 2;

# 把這兩張表按照學生的id 內鏈接起來 去掉只學習某一門課程的學生

select t1.student_id from

(select student_id from score where course_id = 1)  t1

inner join

(select student_id from score where course_id = 2) t2

on t1.student_id = t2.student_id

# 根據學號在學生表中找到對應的姓名

select sid,sname from student where sid in (select t1.student_id from (select student_id from score where course_id = 1)  t1 inner join (select student_id from score where course_id = 2) t2 on t1.student_id = t2.student_id);

 

七、查詢學過「李平老師」所教的全部課的同窗的學號、姓名;

#找到李平老師的tid

select tid from teacher where tname ='李平老師';

# 找到李平老師教的全部課程cid

 select cid from course where teacher_id in (select tid from teacher where tname ='李平老師');

# 找到李平老師教的全部課程數

 select count(cid) from course where teacher_id in (select tid from teacher where tname ='李平老師');

# 找到全部學習李平老師課程的學生

select * from score where course_id in ( select cid from course where teacher_id in (select tid from teacher where tname ='李平老師'));

# 查看全部學習李平老師課程的學生選課數

select student_id,count(course_id) from score where course_id in ( select cid from course where teacher_id in (select tid from teacher where tname ='李平老師')) group by student_id;

# 找到全部選擇了李平老師全部課程的學生id

select  student_id from (

select student_id,count(course_id) course_count from score where course_id in ( select cid from course where teacher_id in (select tid from teacher where tname ='李平老師')) group by student_id) t1

where t1.course_count =

(select count(cid) from course where teacher_id in (select tid from teacher where tname ='李平老師'));

# 找到學生的其餘信息

select sid,sname from student where sid in (

select  student_id from (

select student_id,count(course_id) course_count from score where course_id in ( select cid from course where teacher_id in (select tid from teacher where tname ='李平老師')) group by student_id) t1

where t1.course_count =

(select count(cid) from course where teacher_id in (select tid from teacher where tname ='李平老師'))

);

 

八、查詢課程編號「2」的成績比課程編號「1」課程低的全部同窗的學號、姓名;

# 先找到每一個學生的課程編號「1」的和課程編號「2」的成績組成一張表

select t1.student_id from (select num num2,student_id from score where course_id = 2) t2 inner join (select student_id,num num1 from score where course_id = 1) t1 on t1.student_id = t2.student_id

# 再找到課程編號「2」的成績比課程編號「1」課程低的全部學生的學號

select t1.student_id from (select num num2,student_id from score where course_id = 2) t2 inner join (select student_id,num num1 from score where course_id = 1) t1 on t1.student_id = t2.student_id where num2 < num1

# 再找到全部學生的學號、姓名

select sid,sname from student where sid in(select t1.student_id from (select num num2,student_id from score where course_id = 2) t2 inner join (select student_id,num num1 from score where course_id = 1) t1 on t1.student_id = t2.student_id where num2 < num1);

 

九、查詢有課程成績小於60分的同窗的學號、姓名;

# 先查詢成績小於60分的同窗的學號

select distinct student_id from score where num < 60;

# 再查詢有課程成績小於60分的同窗的學號、姓名

select sid,sname from student where sid in (select distinct student_id from score where num < 60);

 

十、查詢至少有一門課與學號爲1的同窗所學課程相同的同窗的學號和姓名;

# 先看看學號爲1的同窗都學了哪些課程

select course_id from score where student_id = 1

# 找到學習 學號爲1的同窗所學課程 的學號

select distinct student_id from score where course_id in (select course_id from score where student_id = 1);

#  找到學習 學號爲1的同窗所學課程 的學號\姓名

select sid,sname from student where sid in (select distinct student_id from score where course_id in (select course_id from score where student_id = 1));

 

十一、課程平均分從高到低顯示

select course_id,avg(num) avg_num from score group by course_id order by avg_num desc;

 

十二、查詢出只選修了一門課程的所有學生的學號和姓名;

# 查詢出只選修了一門課程的所有學生的學號

select student_id,count(student_id) from score group by student_id having count(student_id) =1;

# 查詢出只選修了一門課程的所有學生的學號和姓名;

select sid,sname from student where sid in (select student_id from score group by student_id having count(student_id) =1);

 

1三、查詢男生、女生的人數;

select gender,count(sid) from student group by gender;

 

1四、查詢姓「張」的學生名單;

select * from student where sname like '張%';

 

1五、查詢各科成績最高和最低的分:以以下形式顯示:課程ID,最高分,最低分;

# 查詢成績的最高分

select course_id c1,max(num) from score group by course_id

# 查詢成績的最低分

select course_id c1,min(num) from score group by course_id

# 查詢成績的最高分和最低分拼接

select * from ( (select course_id c1,max(num) from score group by course_id) t1 inner join (select course_id c2,min(num) from score group by course_id) t2 on t1.c1 = t2.c2 );

# 格式整理

select t1.c1,t1.max_num,t2.min_num from ( (select course_id c1,max(num) max_num from score group by course_id) t1 inner join (select course_id c2,min(num) min_num from score group by course_id) t2 on t1.c1 = t2.c2 );

3、進階練習答案

一、查詢沒有學全全部課的同窗的學號、姓名;

# 先統計一共有多少門課程

select count(cid) from course;

# 查看每一個學生選擇的課程書

select count(course_id) from score group by student_id;

# 查詢所學課程數小於總課程數的學生學號

select student_id

from (select count(course_id) c_course_id,student_id from score group by student_id) t1 

where t1.c_course_id <  (select count(cid) from course) ;

# 查詢沒有學全全部課的同窗的學號、姓名;

select sid,sname from student where sid in (

 select student_id from (select count(course_id) c_course_id,student_id from score group by student_id

 ) t1 where t1.c_course_id <  (select count(cid) from course)

) ;

 

二、查詢和「002」號的同窗學習的課程徹底相同的其餘同窗學號和姓名;

# 先查詢2號同窗學了哪些課程

select * from score where student_id =2;

# 找到學習了2號同窗沒學習課程的全部同窗(找到全部和2號同窗學習的課程不同的同窗)

select student_id from score where course_id not in (select course_id from score where student_id=2)

# 找到score表中全部的學生而且把 2號同窗 以及(和2號同窗學習的課程不同的同窗)排除出去

select student_id from score where student_id not in (select student_id from score where course_id not in (select course_id from score where student_id=2)) and student_id !=2

# 對剩餘的和2號同窗所選課程沒有不一樣的同窗所選課程數進行統計,若是和2號同窗的課程數相同,就是選擇了相同的課程

select student_id from score where student_id not in (

 select student_id from score where course_id not in (select course_id from score where student_id=2)

 ) and student_id !=2

group by student_id 

having count(course_id)= (select count(course_id) from score where student_id=2);

 

三、刪除學習「葉平」老師課的SC(score)表記錄;

# 先查出李平老師的id

select tid from teacher where tname = '李平老師';

# 查看李平老師所教授的課程

select cid from course where teacher_id = (select tid from teacher where tname = '李平老師');

# 查看李平老師所教課程的成績數據

select * from score where course_id in (select cid from course where teacher_id = (select tid from teacher where tname = '李平老師'));

# 執行刪除命令

delete from score where course_id in (select cid from course where teacher_id = (select tid from teacher where tname = '李平老師'));

四、向SC表中插入一些記錄,這些記錄要求符合如下條件:①沒有上過編號「002」課程的同窗學號;②插入「002」號課程的平均成績; 

#  先找尋上過2號課程的同窗

select student_id from score where course_id = 2;

# 再找到沒上過2號課程的全部同窗

select * from student where sid not in (select student_id from score where course_id = 2);

#  計算出學習2號課程的同窗的平均成績

select avg(num) from score where course_id = 2 group by course_id;

# 用笛卡爾積將上述兩個表拼起來

select * from (select sid from student where sid not in (select student_id from score where course_id = 2)) t1,(select avg(num) from score where course_id = 2 group by course_id) t2;

#  向SC表中插入記錄

insert into score (course_id,student_id,num)   select 2,t1.sid,t2.avg_num from (select sid from student where sid not in (select student_id from score where course_id = 2)) t1,(select avg(num) avg_num from score where course_id = 2 group by course_id) t2;

 

五、按平均成績從低到高顯示全部學生的「語文」、「數學」、「英語」三門的課程成績,按以下形式顯示: 學生ID,語文,數學,英語,有效課程數,有效平均分;

# 查看每一個學生的數學成績

select student_id,num from score where course_id = (select cid from course where cname = '數學');

#  查看每一個學生的語文成績

select student_id,num from score where course_id = (select cid from course where cname = '語文');

#  查看每一個學生的英語成績

select student_id,num from score where course_id = (select cid from course where cname = '英語');

# 查看每一個學生的平均成績

select student_id,avg(num),count(num) from score group by student_id;

# 將上面的幾張表拼接起來,爲了生成全部學生的信息,用student表做爲左鏈接的第一張表

select sid 學生ID,t2.num 語文,t1.num 數學, t3.num 英語,t4.count_course 有效課程數,t4.avg_num 有效平均分 from student 

 left join (select student_id,num from score where course_id = (select cid from course where cname = '數學')) t1

 on student.sid = t1.student_id

 left join (select student_id,num from score where course_id = (select cid from course where cname = '語文')) t2

 on student.sid = t2.student_id

 left join (select student_id,num from score where course_id = (select cid from course where cname = '英語')) t3

 on student.sid = t3.student_id

 left join (select student_id,avg(num) avg_num,count(num) count_course from score group by student_id)  t4

 on student.sid = t4.student_id

 

六、查詢各科成績最高和最低的分:以以下形式顯示:課程ID,最高分,最低分;

select course_id 課程ID,max(num) 最高分,min(num) 最低分 from score group by course_id;

 

七、按各科平均成績從低到高和及格率的百分數從高到低順序;

# 方法1:

# 先求平均成績

select course_id,avg(num) from score group by course_id;

# 解決計算各科及格率的問題

全部及格的人/全部人數

select t1.course_id,t1.count1/t2.count2 from 

(select course_id,count(course_id) count1 from score where num>60 group by course_id) t1 

left join

(select course_id,count(course_id) count2 from score group by course_id) t2

on t1.course_id = t2.course_id;

# 根據上述內容進行表的拼接

select  t_out1.course_id,t_out1.avgnum, t_out2.pass_per from 

(select course_id,avg(num) avgnum from score group by course_id ) t_out1

left join 

(select t1.course_id,t1.count1/t2.count2 pass_per from 

(select course_id,count(course_id) count1 from score where num>60 group by course_id) t1 

left join

(select course_id,count(course_id) count2 from score group by course_id) t2

on t1.course_id = t2.course_id) t_out2

on  t_out1.course_id = t_out2.course_id

# 加上排序

select  t_out1.course_id,t_out1.avgnum, t_out2.pass_per from  (select course_id,avg(num) avgnum from score group by course_id ) t_out1 left join  (select t1.course_id,t1.count1/t2.count2 pass_per from  (select course_id,count(course_id) count1 from score where num>60 group by course_id) t1  left join (select course_id,count(course_id) count2 from score group by course_id) t2 on t1.course_id = t2.course_id) t_out2 on  t_out1.course_id = t_out2.course_id order by avgnum ,pass_per desc;

 

# 方法2 

# 使用case when直接計算合格率

select 

sum(case when num>60 then 1 else 0 end)/count(course_id)

from score group by course_id

# 加上課程id和平均值

select  course_id,avg(num),

sum(case when num>60 then 1 else 0 end)/count(course_id)

from score group by course_id

# 加上排序

select  course_id,avg(num) avgnum,

sum(case when num>60 then 1 else 0 end)/count(course_id) pass_per 

from score group by course_id

 order by avgnum ,pass_per desc;

 

 

八、查詢各科成績前三名的記錄:(不考慮成績並列狀況) 

select
t1.sid,t1.student_id,t1.course_id,t1.num from score t1
left join
    (
    select sid,course_id,
    (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 0, 1) as first_num,
    (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 1, 1) as second_num,
    (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 2, 1) as third_num
    from score as s1
    ) t2
on t1.sid = t2.sid
where t1.num = t2.first_num or t1.num = t2.second_num or t1.num = t2.third_num;
 


九、查詢每門課程被選修的學生數;

select course_id,count(course_id) from score group by course_id;

 

十、查詢同名同姓學生名單,並統計同名人數;

select sname,count(1) as count from student group by sname;

 

十一、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列;

select course_id,avg(if(isnull(num), 0 ,num)) as avg from score group by course_id order by avg  asc,course_id desc;

 

十二、查詢平均成績大於85的全部學生的學號、姓名和平均成績;

select student_id,sname, avg(if(isnull(num), 0 ,num)) from score left join student on score.student_id = student.sid group by student_id;

 

1三、查詢課程名稱爲「數學」,且分數低於60的學生姓名和分數;

select student.sname,score.num from score

left join course on score.course_id = course.cid

left join student on score.student_id = student.sid

where score.num < 60 and course.cname = '數學'

 

 

1四、查詢課程編號爲003且課程成績在80分以上的學生的學號和姓名; 

select * from score where score.student_id = 3 and score.num > 80

 

1五、求選了課程的學生人數

select sid,sname from student where sid not in (select student_id from score group by student_id);

 

1六、查詢選修「楊豔」老師所授課程的學生中,成績最高的學生姓名及其成績;

# 先找到「楊豔」老師的教師id

select tid from teacher where tname = '楊豔';

# 再找到楊豔老師教的全部課程

select cid from course where teacher_id in (select tid from teacher where tname = '楊豔');

# 再找到楊豔老師教的全部課程的最高分

select max(num) from score where course_id in (select cid from course where teacher_id in (select tid from teacher where tname = '李平老師'));

# 再找到楊豔老師教的全部課程的最高分對應的學生

select distinct student_id,num from score 

where num = (select max(num) from score where course_id in (select cid from course where teacher_id in (select tid from teacher where tname = '李平老師'))) 

and course_id in   (select cid from course where teacher_id in (select tid from teacher where tname = '李平老師'));

# 找到學生的姓名

select student.sname,t1.num from(

select distinct student_id,num from score 

where num = (select max(num) from score where course_id in (select cid from course where teacher_id in (select tid from teacher where tname = '李平老師'))) 

and course_id in   (select cid from course where teacher_id in (select tid from teacher where tname = '李平老師'))

) t1

left join

student

on 

t1.student_id = student.sid;

 

1七、查詢各個課程及相應的選修人數;

select course.cname,count(1) from score

left join course on score.course_id = course.cid

group by course_id;

 

1八、查詢不一樣課程但成績相同的學生的學號、課程號、學生成績;

select DISTINCT s1.course_id,s2.course_id,s1.num,s2.num from score as s1, score as s2 where s1.num = s2.num and s1.course_id != s2.course_id;

 

1九、查詢每門課程成績最好的前兩名;

   先查詢每條數據對應學科成績的第一名和第二名,這裏必需要保留全部的s1,以便後續進行連表查詢

select sid,course_id,
    (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 0, 1) as first_num,
    (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 1, 1) as second_num
from score as s1

按照sid連表,把學生的成績和對應的第一名、第二名成績連起來
select
* from score t1
left join
    (
    select sid,course_id,
    (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 0, 1) as first_num,
    (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 1, 1) as second_num
    from score as s1
    ) t2
on t1.sid = t2.sid

判斷若是學生的成績是第一名、第二名的成績,那麼就符合條件,顯示學生的id、學科和成績
select
t1.sid,t1.student_id,t1.course_id,t1.num from score t1
left join
    (
    select sid,course_id,
    (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 0, 1) as first_num,
    (select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 1, 1) as second_num
    from score as s1
    ) t2
on t1.sid = t2.sid
where t1.num = t2.first_num or t1.num = t2.second_num;

20、檢索至少選修兩門課程的學生學號;

select student_id from score group by student_id having count(student_id) > 1;

 

2一、查詢所有學生都選修的課程的課程號和課程名;

# 先查看一共有多少學生

select count(sid) from student;

#  查看哪一門課選秀的學生個數和學生的總個數相等

select course_id from score group by course_id having count(student_id) = (select count(sid) from student);


2二、查詢沒學過「葉平」老師講授的任一門課程的學生姓名;

# 先查看要查找老師的id

select tid from teacher where tname = '李平老師';

# 查看該老師交了哪些課程

select cid from course where teacher_id in (select tid from teacher where tname = '李平老師')

# 看看有多少學生學習了該老師的課程

select distinct student_id from score where course_id in (select cid from course where teacher_id in (select tid from teacher where tname = '李平老師'));

# 把不在上表中的學生姓名查出來

select sname from student where sid not in (select distinct student_id from score where course_id in (select cid from course where teacher_id in (select tid from teacher where tname = '李平老師')));


2三、查詢兩門以上不及格課程的同窗的學號及其平均成績;

select student_id,avg(num) from score where num<60 group by student_id having count(num)>=2;

 
2四、檢索「004」課程分數小於60,按分數降序排列的同窗學號;

select student_id from score where num< 60 and course_id = 4 order by num desc;


2五、刪除「002」同窗的「001」課程的成績;

delete from score where course_id = 1 and student_id = 2;
相關文章
相關標籤/搜索