diff --git "a/10 \346\234\261\345\277\227\351\271\217/\344\275\234\344\270\232.md" "b/10 \346\234\261\345\277\227\351\271\217/\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..1637e18733f51bad84a344675bf8cd53872e3da1 --- /dev/null +++ "b/10 \346\234\261\345\277\227\351\271\217/\344\275\234\344\270\232.md" @@ -0,0 +1,3 @@ +```sql + +``` diff --git "a/10 \346\234\261\345\277\227\351\271\217/\345\255\220\346\237\245\350\257\242\344\275\234\344\270\232.md" "b/10 \346\234\261\345\277\227\351\271\217/\345\255\220\346\237\245\350\257\242\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..9ece65de0dbe12d909a7f10b2ef7c87af7d85828 --- /dev/null +++ "b/10 \346\234\261\345\277\227\351\271\217/\345\255\220\346\237\245\350\257\242\344\275\234\344\270\232.md" @@ -0,0 +1,103 @@ +```sql +CREATE DATABASE td charset utf8; +use td; +CREATE table stuinfo( +stuNO VARCHAR(10) primary key, +stuName VARCHAR(10), +stuSex VARCHAR(10), +stuAge VARCHAR(10), +stuAddress VARCHAR(20), +stuSeat INT +); +CREATE table stuExam( +examNo int primary key auto_increment, +stuNO VARCHAR(10), +writtenExam int, +labExam INT +); +CREATE table stuMarks( +examNo int primary key auto_increment, +stuID VARCHAR(10), +score INT +); +alter table stuExam add foreign key (stuNO) references stuinfo(stuNO); +alter table stuMarks add foreign key (examNo) references stuExam(examNo); + + +INSERT into stuinfo VALUES +('s2501','张秋利','男',20,'美国硅谷',1), +('s2502','李斯文','女',18,'湖北武汉',2), +('s2503','马文才','男',18,'湖南长沙',3), +('s2504','欧阳俊雄','女',21,'湖北武汉',4), +('s2505','梅超风','男',20,'湖北武汉',5), +('s2506','陈旋风','男',19,'美国硅谷',6); + +INSERT INTO stuExam VALUES +(1,'s2501',50,70), +(2,'s2502',60,65), +(3,'s2503',86,70), +(4,'s2504',40,80), +(5,'s2505',70,85), +(6,'s2506',85,90); + +INSERT into stuMarks VALUES +(1,'s2501',88), +(2,'s2501',92), +(3,'s2501',53), +(4,'s2502',60), +(5,'s2502',99), +(6,'s2503',82); + + +# 1.查询出年龄比班上平均年龄大的学生的信息 +select * from stuinfo where stuAge>(select avg(stuAge) from stuinfo); + +# 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) +select a.stuNO,a.stuName,a.stuSex,max(score) from stuinfo a left join stumarks b on a.stuNO=b.stuID group by stuNO; + +# 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +select a.stuNO,a.stuName,a.stuSex,(b.writtenExam+b.labExam)/2 平均分 from stuinfo a left join stuexam b on a.stuNO=b.stuNO; + +# 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +select * from stuinfo where stuSex='男' and stuAge>=20; + +# 5.查询出年龄比所有男生年龄都大的女生的信息 +select * from stuinfo where stuSex='女' and stuAge>(select max(stuAge) from stuinfo where stuSex='男'); + +# 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +select a.* from stuinfo a, (select stuID from stumarks group by stuID having min(score)>=60) b where a.stuNO=b.stuID; + +# 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select a.* from stuinfo a,(select stuID from stumarks group by stuID) b where a.stuNO=b.stuID; + +# 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select * from stuinfo where stuNO not in(select stuID from stumarks group by stuID); + +# 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +select a.* from stuinfo a,(select stuID from stumarks group by stuID having max(score)>90) b where a.stuNO=b.stuID; + +# 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +select a.* from stuinfo a,(select stuID from stumarks group by stuID having avg(score)>80) b where a.stuNO=b.stuID; + +# 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +select a.* from +stuinfo a, +(select stuID from stumarks where stuID!='s2501' group by stuID having max(score)>(select max(score) from stumarks where stuID='s2501')) b +where + a.stuNO=b.stuID; + +# 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +select a.* from +stuinfo a, +(select stuID from stumarks where stuID!='s2501' group by stuID having max(score)>(select min(score) from stumarks where stuID='s2501')) b +where + a.stuNO=b.stuID; + +# 13.查询班上比所有男生年龄都要大的女生的信息 +select * from stuinfo where stuSex='女' and stuAge>(select max(stuAge) from stuinfo where stuSex='男'); + +# 14.查询出只是比某个男生年龄大的女生的信息 +select * from stuinfo where stuSex='女' and stuAge>(select min(stuAge) from stuinfo where stuSex='男'); + + +```