mysql查询所有姓王的信息_MySQL的查询练习

student表

6a6a43b855031fae60d1538534ea65dc.png

teacher表

a747d97a7ab95d4f36b0779d54148074.png

course表

090d9a207d00bcdc9a79c74a755d9f7f.png

score表

9a0338991f2d0ce16b4e421cac7d0944.png

对这四个表进行一些练习。

1:查询student表中所有记录。

select *from student;

2:查询student表中name/sex/classid这些列。

select  name,sex,classid from student;

3:查询教师中不重复的单位department。

select distinct department from teacher;

distinct 列(一个或多个) 获取不重复的记录

4:查询score表中成绩再60至80之间的记录。

select *from score where degree between 60 and 80;

5:查询score表中成绩为85,86,88的记录。

select *from score where degree in (86,85,88);

6:查询student表中为95031班的或性别为女生的。

select *from student where classid=95031 or sex='女';

7:以classid列降序查询student表的所有记录。

select *from student order by desc classid ;

8:以degree降序courseid升序输出score表中所有记录。

select *from score order by courseid asc, degree desc;

9:查询95031班级的人数。

select count(id) from student where classid=95031;

10:查询score表中成绩最高者的记录。

select *from score where degree=(select max(degree) from score);

或: select id,courseid,degree order by degree desc limit 0,1;

11:查询每门课的平均数。

select courseid,avg(degree) from score group by courseid;

12:查询score表中至少2名学生选修以3开头的课程的平均分数。

select courseid, avg(degree) as average from score group by courseid having count(courseid)>1 and courseid like '3%';

13:查询score中分数大于70小于90的id列。

select  *from score where degree between 70 and 90;

14:查询学生的name列和degree列和classid列(多表查询)。

select name,degree,classid from student,score where student.id=score.id

15:查询学生的课程id课程名和degree列(多表查询)。

select coursename,degree from score,course where course.courseid=score.courseid;

16:查询学生的姓名成绩和对应课程名。(三表查询)。

select  name,degree,coursename from student,course,score where score.id=student.id and score.courseid=course.courseid;

17:查询95031班学生每门课的平均成绩(子查询)。

select avg(degree) from score where id in(select id from student where classid=95031) group by courseid;

18:查询选修3105课程高于109好同学3105课程成绩的同学记录。

select *from score where courseid=3105 and degree>(select degree from score where id=109 and courseid=3105);

19:查询成绩高于学号109课程号为3105的同学的记录。

select *from score where  degree>(select degree from score where id=109 and courseid=3105);

20:查询学号与101和108的同学同年出生的学生的记录。

select name, brithday,classid from student where year(brithday) in (select year(brithday) from student where id=108 or id=101);

21:查询张旭老师教的课程学生的成绩。

select *from score where courseid in (select courseid from course where teacherid=(select id from teacher where name='张旭'));

22:查询选修课多余5人的老师的记录。

select *from teacher where id in (select teacherid from course where courseid in (select courseid from score group by courseid having count(courseid)>5));

23:查询95033和95031班级同学的记录

select *from student where classid in (95033,95031);

24:查询88分以上成绩的课程name。

select coursename from course where courseid in(select courseid from score where degree>88);

25:查询计算机系老师所教课程同学的成绩。

select *from score where courseid in(select courseid from course where teacherid in(select id from teacher where department='计算机系'));

26:查询计算机系与电子工程系不同职称的教师的记录。

select *from teacher where department='计算机系'and professional  not in (select professional from  teacher where department='电子工程系' )

-> union

-> select *from teacher where department='电子工程系'and professional  not in (select professional from  teacher where department='计算机系' );

27:查询选修编号为3105的课程成绩至少高于部分选修3245课程学生的成绩的记录。

select *from score where courseid=3105  and degree> any(select degree from score where courseid=3245);

28:查询选修编号为3105的课程成绩高于所有选修3245课程学生的成绩的记录。

select *from score where courseid=3105  and degree> all(select degree from score where courseid=3245);

29:查询所有教师和学生的name ,sex ,brithday。

select name,sex,brithday from student

-> union

-> select name,sex,brithday from teacher;

30:查询女教师和女同学的name sex brithday。

select name,sex,brithday from student where sex='女'

-> union

->  select name,sex,brithday from teacher where sex='女';

31:查询成绩比该课程平均分低的同学的成绩。

select *from score a where degree

32:查询任课老师的name和department。

select *from teacher where id in(select teacherid from course where courseid in(select courseid from score group by courseid));

33:查询班级中至少有2名男生的班级。

select classid from student where sex='男' group by classid having count(classid)>1;

34:查询班级中不姓王的同学。

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

35:查询所有学生的姓名和年龄。

select name,year(now())-year(brithday) as age from student;

36:查询学生中年龄最大和年龄最小的数据。

select max(year(now())-year(brithday)),min(year(now())-year(brithday)) from student;

37:以班号和年龄从大到小顺序查询student表中所有记录。

select *from student order by classid,year(now())-year(brithday);

38:查询男教师所上的课程。

select coursename from course where teacherid in (select  id from teacher where sex='男');

39:查询最高分同学的信息。

select *from score where degree =(select max(degree) from score);

40:查询和季军同性别的所有同学。

select *from student where sex=(select sex from student where name='季军');

41:查询和季军同性别并同班的同学的信息。

select *from student where sex=(select sex from student where name='季军') and classid=(select classid from student where name='季军');

42:查询所有选修计算机导论课程的男同学的成绩。

select *from score where courseid=(select courseid from course where coursename='计算机导论') and id in(select id from student where sex='男' );

标签:courseid,练习,查询,score,student,MySQL,where,select

来源: https://www.cnblogs.com/zhangyang4674/p/11604017.html

  • 7
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值