From 41791f033be860c77934df5cf6df54204428f73a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BA=84=E7=8F=8A=E7=8F=8A?= <3106036048@qq.com> Date: Wed, 8 Mar 2023 01:15:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=90=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7 \345\255\220\346\237\245\350\257\242.md" | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 "48 \345\272\204\347\217\212\347\217\212/20230307 \345\255\220\346\237\245\350\257\242.md" diff --git "a/48 \345\272\204\347\217\212\347\217\212/20230307 \345\255\220\346\237\245\350\257\242.md" "b/48 \345\272\204\347\217\212\347\217\212/20230307 \345\255\220\346\237\245\350\257\242.md" new file mode 100644 index 0000000..e86af91 --- /dev/null +++ "b/48 \345\272\204\347\217\212\347\217\212/20230307 \345\255\220\346\237\245\350\257\242.md" @@ -0,0 +1,105 @@ +# 1.笔记 + +```sql +select 子查询 from + +select *from 子查询 + +select *from 表 where 子查询 +``` + +# 2.作业 + +```sql +create database people charset utf8; +use people; +create table stuinfo( + stuNo varchar(5), + stuName varchar(12), + stuSex va('男''女'), + stuAge int, + stuAddress varchar(4), + stuSeat int +); +insert into stuinfo values ('s2501','张秋利','男','20','美国硅谷','1'); +insert into stuinfo values ('s2502','李斯文','女',18,'湖北武汉','2'), + ('s2503','马文才','男','22','湖北长沙','3'), + ('s2504','欧阳俊雄','女',21,'湖北武汉','4'), + ('s2505','梅超风','男',20,'湖北武汉',5), + ('s2506','陈旋风','男','19','美国硅谷',6); +select * from stuinfo; +create table stuExam( + examNo int, + stuNo varchar(5), + writtemExam int, + labExam int +); +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); +create table stuMarks( + examNo int, + stuID varchar(5), + score int +); +select * from stumarks; +insert into stuMarks values (1,'s2501',88); +insert into stuMarks values (2,'s2502',92), + (3,'s2503',53), + (4,'s2504',60), + (5,'s2505',99), + (6,'s2506',82); +select * from stumarks; +在如图的数据表上完成以下题目 +1.查询出年龄比班上平均年龄大的学生的信息 +select *from stuinfo where stuAge>(select avg(stuAge) from stuinfo); +2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) +select * from stuinfo right join stuExam sE on stuinfo.stuNo = sE.stuNo + right join stumarks s on sE.examNo = s.examNo + where score in (select max(score)from stumarks group by stuMarks.stuID); +3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +select stuinfo.stuNo,stuName,stuSex,(s.writtemExam+s.labExam)/2 from stuinfo left join stuexam s on stuinfo.stuNo = s.stuNo; +-- or +select stuName,pjz from stuinfo left join + (select stuNO,(writtemExam+labExam)/2 pjz from stuexam ) s + on stuinfo.stuNO=s.stuNO; +4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +select * from stuinfo where stuinfo.stuSex ='男' and stuAge>=20; +5.查询出年龄比所有男生年龄都大的女生的信息 +select * from stuinfo where stuAge > any + (select stuAge from stuinfo where stuinfo.stuSex='男') + and (stuinfo.stuSex ='女'); +6.查询出所有选修课程都及格的学生的信息 (stuMarks) +select * from stuinfo left join stuexam s on + stuinfo.stuNo = s.stuNo left join stumarks s2 + on s.examNo = s2.examNo where score >= 60; +7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select * from stumarks right join stuinfo on stuinfo.stuNo = stuMarks.stuID +where score is not null; +-- or +select * from stuinfo where stuNo in (select stuID from stuMarks); + 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select * from stuinfo s left join stumarks s1 on s.stuNo = s1.stuID +where score is null ; +select * from stuinfo where stuNo not in (select stuID from stumarks); + 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +select * from stuinfo s left join stumarks s2 on s.stuNo =s2.stuID where score>90; + 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +select * from stuinfo s left join stumarks s2 on s.stuNo =s2.stuID left join stuexam s3 on s2.examNo = s3.examNo where + (writtemExam+labExam)/2 in(select (writtemExam+labExam)/2 as ww from stuexam where (writtemExam+labExam)/2 >80); + 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +select * from stumarks right join stuinfo on stuinfo.stuNo=stumarks.stuid + where score>(select score from stumarks where stuID='s2501'); + 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) +select * from stumarks right join stuinfo on stuinfo.stuNo=stumarks.stuid + where score>((select writtemExam from stuexam where stuExam.stuNo='s2501') + or (select labExam from stuexam where stuExam.stuNo='s2501')); + 13.查询班上比所有男生年龄都要大的女生的信息 +select stuinfo.* from stuinfo where stuage>(select max(stuage) from stuinfo where stusex='男'); + 14.查询出只是比某个男生年龄大的女生的信息 +select stuinfo.* from stuinfo where stuage>=(select min(stuage) from stuinfo where stusex='男') and stusex='女'; +``` + -- Gitee