MySQL 做業題及答案

MySQL 測試題

1、 表關係:sql

請建立以下表,並建立相關約束ide

 

建立表sql以下:學習

/*
Navicat MySQL Data Transfer

Source Server         : 192.168.118.14
Source Server Version : 50544
Source Host           : 192.168.118.14:3306
Source Database       : db3

Target Server Type    : MYSQL
Target Server Version : 50544
File Encoding         : 65001

Date: 2019-02-15 16:34:46
*/

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) DEFAULT NULL,
  PRIMARY KEY (`cid`),
  UNIQUE KEY `caption` (`caption`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

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

-- ----------------------------
-- 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
-- ----------------------------
INSERT INTO `course` VALUES ('1', '生物', '1');
INSERT INTO `course` VALUES ('2', '物理', '2');
INSERT INTO `course` VALUES ('3', '體育', '3');
INSERT INTO `course` VALUES ('4', '美術', '2');

-- ----------------------------
-- 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,
  `number` int(11) NOT NULL,
  PRIMARY KEY (`sid`),
  UNIQUE KEY `student_id` (`student_id`,`course_id`),
  KEY `fk_score_course` (`course_id`),
  CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`),
  CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=66 DEFAULT CHARSET=utf8;

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

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

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

-- ----------------------------
-- 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
-- ----------------------------
INSERT INTO `teacher` VALUES ('1', '張磊老師');
INSERT INTO `teacher` VALUES ('2', '李平老師');
INSERT INTO `teacher` VALUES ('3', '劉海燕老師');
INSERT INTO `teacher` VALUES ('4', '朱雲海老師');
INSERT INTO `teacher` VALUES ('5', '李傑老師');
表結構及數據

 

 

2、表操做及答案測試

1. 查詢「生物」課程比「物理」課程成績高的全部學生的學號spa

select A.student_id, sw, wl from 
(SELECT score.student_id, number as sw from score LEFT JOIN course on score.course_id = course.cid where course.cname = '生物') as     A
LEFT JOIN
(SELECT score.student_id, number as wl from score LEFT JOIN course on score.course_id = course.cid where course.cname = '物理') as B
on A.student_id = B.student_id where sw > wl

 

 

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

SELECT student_id, avg(number) from score GROUP BY score.student_id HAVING avg(number) > 60

 

 

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

SELECT score.student_id, student.sname, count(score.course_id), sum(number) from score
LEFT JOIN student on score.student_id = student.sid
GROUP BY score.student_id

 

 

 4. 查詢姓「李」的老師的個數ci

SELECT count(1) from teacher where teacher.tname like '李%'

 

 

 5. 查詢沒學過「李平老師」課的同窗的學號、姓名get

SELECT sid, sname from student where student.sid  not in (
SELECT DISTINCT score.student_id from score where score.course_id in (
SELECT course.cid from course
LEFT JOIN teacher on course.teacher_id = teacher.tid
where teacher.tname = '李平老師'))

 

 

 6. 查詢學過「001」而且也學過編號「002」課程的同窗的學號、姓名it

SELECT sid, sname from student where student.sid in (
SELECT score.student_id from score where score.course_id = 1 or score.course_id = 2
GROUP BY score.student_id HAVING count(1) > 1);

 

 

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

SELECT sid, sname from student where student.sid in (
SELECT score.student_id from score where score.course_id in 
(SELECT cid from course LEFT JOIN teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老師')
GROUP BY score.student_id HAVING count(1) = (SELECT count(1) from course 
LEFT JOIN teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老師'))

 

 

 8. 查詢課程編號「002」的成績比課程編號「001」課程低的全部同窗的學號、姓名

SELECT sid, sname from student where student.sid in (
select A.student_id from 
(select student_id, number as num_2 from score where score.course_id = 2) as A
LEFT JOIN
(select student_id, number as num_1 from score where score.course_id = 1) as B
on A.student_id = B.student_id where A.num_2 < B.num_1)

 

 

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

SELECT DISTINCT student.sid, student.sname from score
LEFT JOIN student on score.student_id = student.sid where score.number < 60;

 

 

 10. 查詢沒有學全全部課的同窗的學號、姓名

SELECT sid, sname from student where sid in (
SELECT score.student_id from score GROUP BY student_id HAVING count(1) != (SELECT count(1) from course))

 

 

11. 查詢至少有一門課與學號爲「1」的同窗所學相同的同窗的學號和姓名

SELECT sid, sname from student where sid in (
SELECT DISTINCT student_id from score where score.course_id in (SELECT course_id from score where student_id = 1))
and sid != 1;

 

12. 查詢至少學過學號爲「1」同窗全部課的其餘同窗學號和姓名 *****

SELECT sid, sname from student where sid in (
SELECT student_id from score where score.course_id in (SELECT course_id from score WHERE score.student_id =1 )
GROUP BY student_id HAVING count(1) = (SELECT count(1) from score WHERE score.student_id =1))

 

 

13. 查詢和「1」號的同窗學習的課程徹底相同的其餘同窗學號和姓名

SELECT student.sid, sname from score
LEFT JOIN student on score.student_id = student.sid 
where score.student_id in (
SELECT score.student_id from score where score.student_id != 1 GROUP BY student_id HAVING count(1) = (SELECT count(1) from score where score.student_id = 1)
and course_id in (SELECT course_id from score where score.course_id in (SELECT course_id from score where score.student_id = 1)
GROUP BY student_id HAVING count(1) = (SELECT count(1) from score where score.student_id = 1)
))

 

 

14. 刪除學習「葉平」老師課的score表記錄

DELETE from score where course_id in (
SELECT course.cid from course LEFT JOIN teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老師');

 

 

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

INSERT into score (student_id, course_id, number)
SELECT sid, 1, (SELECT avg(number) from score where score.course_id = 1)
from student where student.sid not in (select student_id from score where score.course_id = 1)

 

 

16. 按平均成績從低到高 顯示全部學生的「生物」、「物理」、「體育」三門的課程成績,按以下形式顯示: 學生ID,生物、物理、體育,有效課程數,有效平均分

SELECT sc.student_id,
(SELECT avg(number) from score LEFT JOIN course on score.course_id = course.cid where score.student_id = sc.student_id and course.cname = '生物') as sw,
(SELECT avg(number) from score LEFT JOIN course on score.course_id = course.cid where score.student_id = sc.student_id and course.cname = '物理') as wl,
(SELECT avg(number) from score LEFT JOIN course on score.course_id = course.cid where score.student_id = sc.student_id and course.cname = '體育') as ty,
count(sc.course_id),
avg(sc.number) as avg_n
from score sc GROUP BY sc.student_id  ORDER BY avg_n asc;

 

 

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

select course_id, max(number), min(number) from score GROUP BY course_id

 

 

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

SELECT score.course_id, avg(number) as avg_n,
(sum(case when score.number > 60 then 1 else 0 end) / count(1) * 100) as percent
from score GROUP BY score.course_id ORDER BY avg_n asc, percent desc;

 

 

19. 課程平均分從高到低顯示(現實任課老師)

SELECT score.course_id, avg(score.number) as avg_n, teacher.tname from score
LEFT JOIN course on score.course_id = course.cid
LEFT JOIN teacher on course.teacher_id = teacher.tid
GROUP BY score.course_id ORDER BY avg_n desc;

 

 

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

SELECT 
sc.course_id,
(SELECT number from score where score.course_id = sc.course_id ORDER BY number desc limit 0,1) as first_num,
(SELECT number from score where score.course_id = sc.course_id GROUP BY number ORDER BY number desc limit 1,1) as second_num,
(SELECT number from score where score.course_id = sc.course_id GROUP BY number ORDER BY number desc limit 2,1) as third_num
from score sc GROUP BY sc.course_id

 

 

21. 查詢每門課程被選修的學生數

SELECT course_id, count(student_id) from score GROUP BY course_id

 

 

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

SELECT student.sid, student.sname from score
LEFT JOIN student on score.student_id = student.sid
GROUP BY student_id HAVING count(score.course_id) =1 ;

 

 

23. 查詢男生、女生的人數

SELECT * from 
(SELECT count(1) as boy from student where student.gender = '') as A,
(SELECT count(1) as girl from student where student.gender = '') as B

 

 

24. 查詢姓「張」的學生名單

SELECT * from student where student.sname like '張%'

 

 

25. 查詢同名同姓學生名單,並統計同名人數

SELECT sname, count(sname) from student GROUP BY sname HAVING count(1) > 1;

 

 

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

SELECT course_id, avg(number) as avg_n from score GROUP BY course_id ORDER BY avg_n asc, course_id desc;

 

 

27. 查詢平均成績大於80的全部學生的學號、姓名和平均成績

SELECT score.student_id, student.sname, avg(number) from score
LEFT JOIN student on score.student_id = student.sid
GROUP BY student_id HAVING avg(number) > 80;

 

 

28. 查詢課程名稱爲「生物」,且分數低於60的學生姓名和分數

SELECT * from score 
LEFT JOIN student on score.student_id = student.sid
where score.number < 60 and score.course_id in (SELECT cid from course where course.cname = '生物')

 

 

 29. 查詢課程編號爲003且課程成績在80分以上的學生的學號和姓名

SELECT student.sid, student.sname from score 
LEFT JOIN student on score.student_id = student.sid 
where score.course_id = 3 and score.number > 80;

 

 

 30. 求選了課程的學生人數

SELECT count(DISTINCT student_id) from score where score.course_id is not null;

 

 

 31. 查詢選修 劉海燕老師 所授課程的學生中,成績最高的學生姓名及其成績

SELECT student.sname, max(score.number) from score
LEFT JOIN student on score.student_id = student.sid
where score.course_id in (SELECT cid from course 
LEFT JOIN teacher on course.teacher_id = teacher.tid where teacher.tname = '劉海燕老師')

 

 

 32. 查詢各個課程及相應的選修人數

SELECT course_id, count(1) from score GROUP BY course_id 

 

 

 33. 查詢不一樣課程但成績相同的學生的學號、課程號、學生成績

SELECT DISTINCT s1.course_id, s2.course_id, s1.number, s2.number from score as s1, score as s2 
where s1.number = s2.number and s1.course_id != s2.course_id order by s1.course_id

 

 

 34. 查詢每門課程成績最好的前兩名

SELECT sc.course_id,
(SELECT number as f_n from score where course_id = sc.course_id ORDER BY number desc limit 0, 1) as first_num,
(SELECT number as f_n from score where course_id = sc.course_id GROUP BY number ORDER BY number desc limit 1, 1) as second_num
from score sc GROUP BY sc.course_id

 

 

35. 檢索至少選修兩門課程的學生學號

SELECT student_id, count(1) from score GROUP BY student_id HAVING count(1) > 1;

 

 

36. 查詢所有學生都選修的課程的課程號和課程名

SELECT score.course_id, course.cname from score
LEFT JOIN course on score.course_id = course.cid
GROUP BY course_id HAVING count(1) = (SELECT count(1) from student)

 

 

37. 查詢沒學過 李平老師 講授的任一門課程的學生姓名

SELECT sid, sname from student where sid not in (
SELECT DISTINCT student_id from score where score.course_id in (
SELECT cid from course 
LEFT JOIN teacher on course.teacher_id = teacher.tid where teacher.tname = '李平老師'))

 

 

38. 查詢兩門以上不及格課程的同窗的學號及其平均成績

SELECT sc.student_id, (SELECT avg(number) from score where score.student_id = sc.student_id) as avg_n from score sc  
where sc.number < 60 GROUP BY student_id HAVING count(1) > 1;

 

 

39. 檢索「004」課程分數小於60,按分數降序排列的同窗學號

SELECT student_id from score where score.course_id = 4 and score.number < 60 ORDER BY score.number desc;

 

 

40. 刪除「002」同窗的「001」課程的成績

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