From b6ad03fede98e0c18b1c8a01a13bcce01b51ef06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=AF=97=E9=BE=99?= <2483323272@qq.com> Date: Wed, 8 Mar 2023 06:54:43 +0000 Subject: [PATCH] =?UTF-8?q?=E9=BB=84=E8=AF=97=E9=BE=99=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 黄诗龙 <2483323272@qq.com> --- ...06\345\255\220\346\237\245\350\257\242.md" | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 "18 \351\273\204\350\257\227\351\276\231/20230306\345\255\220\346\237\245\350\257\242.md" diff --git "a/18 \351\273\204\350\257\227\351\276\231/20230306\345\255\220\346\237\245\350\257\242.md" "b/18 \351\273\204\350\257\227\351\276\231/20230306\345\255\220\346\237\245\350\257\242.md" new file mode 100644 index 0000000..a399d3d --- /dev/null +++ "b/18 \351\273\204\350\257\227\351\276\231/20230306\345\255\220\346\237\245\350\257\242.md" @@ -0,0 +1,91 @@ +```sql +create database class9 charset utf8; +use class9; + +create table stuinfo( + stuNO varchar(10) primary key, + stuName varchar(20), + stuSex varchar(10), + stuAge varchar(20), + stuAddress varchar(20), + stuSeat char +); + +create table stuExam( + ExamNo char primary key, + stuNo varchar(20), + writtenExam varchar(20), + labExam char(11), + foreign key(stuNo) references stuinfo(stuNo) +); + +create table stuMarks( + examNo varchar(10), + stuID varchar(10), + score varchar(10), + foreign key(examNo) references stuExam(examNo) +); + +insert into stuinfo values ('s2501','张秋利','男',20,'美国硅谷',1),('s2502','李斯文','女',18,'湖北武汉',2),('s2503','马文才','男',22,'湖南长沙',3),('s2504','欧阳俊雅','女',21,'湖北武汉',4),('s2505','梅超风','男',20,'湖北武汉',5),('s2506','陈旋风','男',19,'美国硅谷',6); + +desc stuexam; + +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 stuNo,stuName,stuSex,max(score) from stuinfo a inner join stuMarks b on a.stuNO=b.stuID group by stuNo; +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +select stuinfo.stuNO,stuName,stuSex,aug from stuinfo left join (select stuNo,(writtenExam+labExam)/2 aug from stuexam) li on stuinfo.stuNO=li.stuNO; + +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +select * from stuinfo where stuAge>=20 and stuSex='男'; + +select * from stuinfo left join (select * from stuinfo where stuage>=20) aug on aug.stuNO=stuinfo.stuNO where aug.stuSex='男' + +-- 5.查询出年龄比所有男生年龄都大的女生的信息 +select * from stuinfo where stuAge>any(select stuAge from stuinfo where stuSex='男') and stuSex='女'; +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +select * from stuinfo a left join stumarks b on a.stuNo=b.stuID where score>=60; +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select distinct a.* from stuinfo a inner join stumarks b on a.stuNo=b.stuID +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +SELECT* FROM stuinfo a LEFT JOIN ( SELECT DISTINCT stuid FROM stuMarks ) b ON a.stuNO = b.stuid WHERE stuid IS NULL; + +select * from stuinfo where stuNO not in(select distinct stuID from stumarks); + +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +select * from stuinfo where stuNO in(select stuID from stuMarks where score>90); +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +select * from stuinfo where stuNO =(select stuNO from stuexam where (writtenexam + labexam)/2>80); + + +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +select * from stuinfo where stuNO=any(select stuNO from stuexam where writtenExam > (SELECT writtenExam FROM stuexam AS a +INNER JOIN (SELECT stuNO FROM stuinfo WHERE stuName = '张秋利' ) AS b ON a.stuNO = b.stuNO) +AND labExam > ( +SELECT labExam FROM stuexam AS a INNER JOIN ( SELECT stuNO FROM stuinfo WHERE stuName = '张秋利' ) AS b ON a.stuNO = b.stuNO) ); + + + +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +select * from stuinfo where stuNO=any(select stuNO from stuexam where writtenExam > (SELECT writtenExam FROM stuexam AS a +INNER JOIN (SELECT stuNO FROM stuinfo WHERE stuName = '张秋利' ) AS b ON a.stuNO = b.stuNO) +xor labExam > ( +SELECT labExam FROM stuexam AS a INNER JOIN ( SELECT stuNO FROM stuinfo WHERE stuName = '张秋利' ) AS b ON a.stuNO = b.stuNO) ); + + +-- 13.查询班上比所有男生年龄都要大的女生的信息 +select * from stuinfo where stuAge>all(select stuAge from stuinfo where stuSex='男') and stuSex='女'; +-- 14.查询出只是比某个男生年龄大的女生的信息 +select * from stuinfo where stuAge>any(select stuAge from stuinfo where stuSex='男') and stuSex='女'; +``` + -- Gitee