Mysql学习笔记(查询语句练习题2)

表格详情:

student表:

teacher表:

course表:

score表:


 

Mysql查询语句练习:

23.查询“张旭“教师任课的学生成绩

select * from score where cno=(select cno from course where tno=(select tno from teacher where tname="张旭"));

select * from score where cno=(select x.cno from course x,teacher y where x.tno=y.tno and y.tname="张旭");

select A.* from score A join (teacher B,course C) on A.cno=C.cno and B.tno=C.tno and B.tname="张旭";

 

24.查询选修某课程的同学人数多于5人的教师姓名

select tname from teacher where tno in (select tno from course where cno in (select cno from score group by cno having count(cno)>5));

select tname from teacher where tno in (select x.tno from course x,score y where x.cno=y.cno group by y.cno having count(y.cno)>5);

select A.tname from teacher A join (course B,score C) on A.tno=B.tno and B.cno=C.cno group by C.cno having count(C.cno)>5;

 

25.查询95033班和95031班全体学生的记录

select * from score where sno in (select sno from student where class in("95033","95031"));

select A.* from score A join student B on A.sno=B.sno and B.class in ("95033","95031");

 

26.查询存在有85分以上成绩的课程Cno

select distinct cno from score where degree>85;

select cno from score group by cno having max(degree)>85;

 

27.查询出“计算机系“教师所教课程的成绩表

select * from score where cno in (select cno from course where tno in (select tno from teacher where depart="计算机系"));

select A.* from score A join (course B,teacher C) on A.cno=B.cno and B.tno=C.tno and C.depart="计算机系";

 

28.查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof

select tname,prof from teacher where depart="计算机系" and prof not in (select prof from teacher where depart = "电子工程系");

 

29.查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序

select * from score where cno="3-105" and degree> any(select degree from score where cno = "3-245") order by degree desc;

 

30.查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree

select * from score where cno="3-105" and degree> all(select degree from score where cno = "3-245");

 

31.查询所有教师和同学的name、sex和birthday.

select sname as name,ssex as sex,sbirthday as birthday from student union select tname ,tsex ,tbirthday  from teacher;

 

32.查询所有“女”教师和“女”同学的name、sex和birthday

select sname as name,ssex as sex,sbirthday as birthday from student where ssex="女" union select tname ,tsex ,tbirthday  from teacher where tsex="女";

 

33.查询成绩比该课程平均成绩低的同学的成绩表

select A.* from score A where A.degree<(select avg(degree) from score B where A.cno=B.cno);

 

34.查询所有任课教师的Tname和Depart

select tname,depart from teacher where tno in (select distinct tno from course);

select A.tname,A.depart from teacher A join course B on A.tno=B.tno;

 

35.查询所有未讲课的教师的Tname和Depart

select tname,depart from teacher where tno not in (select distinct tno from course);

 

36.查询至少有2名男生的班号

select class from student where ssex="男" group by class having count(class) > 1;

 

37.查询Student表中不姓“王”的同学记录

select * from student where sname not like '王%';

 

38.查询Student表中每个学生的姓名和年龄

select sname as "姓名",year(now())-year(sbirthday) as "年龄" from student;

 

39.查询Student表中最大和最小的Sbirthday日期值

select min(sbirthday) as "最大生日",max(sbirthday) as "最小生日" from student;

 

40.以班号和年龄从大到小的顺序查询Student表中的全部记录

select * from student order by class desc,sbirthday asc;

 

41.查询“男”教师及其所上的课程

select A.tname,B.cname from teacher A join course B on A.tno=B.tno and A.tsex="男";

 

42.查询最高分同学的Sno、Cno和Degree列

select sno,cno,degree from score A where degree=(select max(degree) from score B);

 

43.查询和“李军”同性别的所有同学的Sname

select sname from student where ssex=(select ssex from student where sname="李军");

 

44.查询和“李军”同性别并同班的同学Sname

select sname from student where ssex=(select ssex from student where sname="李军") and class=(select class from student where sname="李军");

 

45.查询所有选修“计算机导论”课程的“男”同学的成绩表

select * from score where sno in (select sno from student where ssex="男") and cno=(select cno from course where cname="计算机导论");

select A.* from score A join (student B,course C) on A.sno=B.sno and A.cno=C.cno and B.ssex="男" and C.cname="计算机导论";

 

  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一些MySQL查询语句练习题的例子: 1. 查询学生表中所有学生的姓名和年龄: SELECT sname, sage FROM student; 2. 查询表中所有课的名称和对应的教师姓名: SELECT cname, tname FROM course JOIN teacher ON course.tid = teacher.tid; 3. 查询成绩表中所有学生的姓名、课名称和对应的成绩: SELECT s.sname, c.cname, sc.score FROM student s JOIN sc ON s.sid = sc.sid JOIN course c ON sc.cid = c.cid; 4. 查询成绩表中成绩在80分以上的学生的姓名和课名称: SELECT s.sname, c.cname FROM student s JOIN sc ON s.sid = sc.sid JOIN course c ON sc.cid = c.cid WHERE sc.score > 80; 5. 查询教师表中教师姓名以"李"开头的教师的编号和姓名: SELECT tid, tname FROM teacher WHERE tname LIKE '李%'; 这些例子可以帮助你练习使用MySQL查询语句来检索和过滤数据库中的数据。你可以根据需要修改查询条件和字段选择来进一步练习。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [MySQL查询语句练习题45题版](https://blog.csdn.net/weixin_39718665/article/details/78161013)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [MySQL查询语句练习50题+答案](https://blog.csdn.net/qq_43278189/article/details/120110256)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值