数据库

首页 » 常识 » 问答 » mysql简单sql复习题
TUhjnbcbe - 2025/2/19 19:40:00

#mysql连接查询,和子查询的复习

##数据库表简介

1、grade年级

2、student学生

3、subject科目

4、result成绩

##子查询

一个查询语句包含了另一个查询语句的编码方式

where筛选分组前的内容,having筛选分组后的内容

只要是聚合函数做条件一定放在having后;

1考试成绩不及格(60分)的学生姓名

查询出成绩小于60的学生编号selectstudentnofromresultwherescore60;

查询出学生编号是多少的学生姓名selectstudentnamefromstudentwherestudentno;

整合:selectstudentnamefromstudentwherestudentnoin(selectstudentnofromresultwherescore60);

2学生“金蝶”的所有考试成绩信息

查询出学生姓名为“金蝶”的学生编号selectstudentnofromstudentwherestudentname=”金蝶”;

通过学生编号查询出全部成绩信息:selectscorefromresultwherestudentno;

整合:selectscorefromresultwherestuentnoin(或者这里将in改为=)(selectstuentnofromstudentwherestudentname=”金蝶”);

细节:=()中可以编写limit关键字,而in()中不可以编写limit关键字

3“C#基础”考试中得了分的学生姓名

在科目表中查询出科目为c#基础的科目编号:selectsubjectidfromsubjectwheresubjectname=”c#基础”;

通过查询出的科目编号在成绩表中查询成绩为的学生编号:selectstudentnofromresultwheresubjectid=?andscore=;

通过查询出学生编号在学生表中查询学生姓名selectstudentnamefromstudentwherestudentno=?

整合:selectstudentnamefromstudentwherestudentnoin(selectstudentnofromresultwheresubjectid=(selectsubjectidfromsubjectwheresubjectname=”c#基础”)andscore=);

4查询“第一阶段”中的出生日期在‘-1-1’之后的男生有多少个

在grade表中查询出第一阶段的gradeid:selectgradeidfromgradewheregradename=”第一阶段”

根据gradeid在student表中查询出出生日期在-1-1之后的男生个数:

selectcount(*)fromstudentwheresex=”男”andborndate’-1-1’andgrade=?

整合:selectcount(*)fromstudentwheresex=”男”andborndate’-1-1’andgrade=(selectgradeidfromgradewheregradename=”第一阶段”);

5查询“第三阶段”有多少科目课时超过70课时

在grade表中查询出第三阶段的id:selectgradeidfromgradewheregradename=”第三阶段”;

根据gradeid在subject表中查询出科目:selectcount(*)fromsubjectwherehour70andgradeid=?

整合:selectcount(*)fromsubjectwherehour70andgradeid=(selectgradeidfromgradewheregradename=”第三阶段”);

6查询参加过“第二阶段”课程考试的所有学生信息

在grade表中查询第二阶段的id:selectgradeidfromgradewheregradename=”第二阶段”;

根据gradeid在subject表中查询出subjectid:selectsubjectidfromsubjectwheregradeid=?

查询第二阶段所有科目的学生id

selectstudentidfromresultwheresubjectidin(selectsubjectidfromsubjectwheregradeid=(selectgradeidfromgradewheregradename=”第二阶段”));

根据学号查询出学生信息

select*fromstudentwherestudentnoin(selectstudentnofromresultwheresubjectidin(selectsubjectidfromsubjectwheregradeid=(selectgradeidfromgradewheregradename=”第二阶段”)));

7查询“C#高级”课程考试成绩高于本课程平均分的学生信息

查询出”c#高级”课程的id:selectsubjectidfromsubjectwheresubjectname=”c#高级”;

在成绩表中查询出平均成绩:selectavg(score)fromresultwheresubjectid=?;

在成绩表中查询出所有课程为c#高级的学生成绩:selectscorefromresultwheresubject=?;

查询出课程成绩大于平均成绩的学生id:selectstudentnofromresultwherescore(selectavg(score)fromresultwheresubjectid=(selectsubjectidfromsubjectwheresubjectname=”c#高级”))andsubjectid=(selectsubjectidfromsubjectwheresubjectname=”c#高级”);

根据studentno查询出学生信息:select*fromstudentwherestudentnoin(selectstudentnofromresultwherescore(selectavg(score)fromresultwheresubjectid=(selectsubjectidfromsubjectwheresubjectname=”c#高级”))andsubjectid=(selectsubjectidfromsubjectwheresubjectname=”c#高级”));

8查询“java基础”课程获得最高分的有几个人

查询出java基础的课程号:selectsubjectidfromsubjectwheresubjectname=“java基础”;

根据subjectid查询出java基础成绩最高的分数

selectmax(score)fromresultwheresubjectid=(selectsubjebctidfromsubjectwheresubjectname=”java基础”);

根据最高分查询出人数:selectcount(*)fromresultwherescore=(selectmax(score)fromresultwheresubjectid=(selectsubjebctidfromsubjectwheresubjectname=”java基础”))andsubjectid=(selectsubjectidfromsubjectwheresubjectname=“java基础”);

##综合练习

32查询年龄比“金辉”大的学生

select*fromstudentwhereborndate(selectborndatefromstudentwherestudentname=”金辉“);

查询手机号是11位的学生

select*fromstudentwherelength(phone)=11;

查询学生邮箱为空的信息

select*fromstudentwhereemailisnull;

查询学生邮箱不为空的信息

select*fromstudentwhereemailisnotnull;

查询出每个学生的年龄

selectstudentname,floor((to_days(now())-to_days(borndate))/.25)as年龄fromstudent;

33查询mysql数据库不及格的学生信息(姓名、性别等);

select*fromstudentwherestudentnoin(selectstudentnofromresultwheresubjectid=(selectsubjectidfromsubjectwheresubjectname=”mysql数据库”)andscore60);

34查询考试平均分最低的学生

selectstudentno,avg(score)fromresultgroupbystudentno;

分组之后查询最高,最低的结果使用limit

selectstudentno,avg(score)fromresultgroupbystudentnoorderbyavg(score)asclimit0,1;

select*fromstudentwherestudentno=(selectstudentnofromresultgroupbystudentnoorderbyavg(score)asclimit0,1);

37查询参加考试全部通过的学生信息

找到最低分高于60的学生。

selectstudentno,min(score)fromresultgroupbystudentnohavingmin(score)=60;

##连接查询

48查询学生信息不全(地址或者email为空)的学生中谁参加了考试,显示姓名,科目名和成绩

selectstudentname,subjectname,scorefromstudentsjoinresultrons.studentno=r.studentnojoinsubjectsubonsub.subjectid=r.subjectidwhereaddressisnulloremailisnull;

50查询电话是开头的学生考试平均分的前3名的姓名,电话,平均分,学生当前所处的阶段名

selectstudentname,phone,avg(score),gradenamefromstudentsjoingradegong.gradeid=s.gradeidjoinresultronr.studentno=s.studentnowherephonelike“%”groupbys.studentnoorderbyavg(score)desclimit0,3;

1
查看完整版本: mysql简单sql复习题