MySQL查询语句练习集合

  1. 查询student表中的所有记录的sname,ssex,class列
    select SNAME,SSEX,CLASS from student;
  2. 查询教师所有的单位即不重复的Depart列
    select distinct DEPART from teacher;
  3. 查询Student表的所有记录
    select * from student;
  4. 查询Score表中成绩在60到80之间的所有记录
    select * from score where degree between 60 and 80;
  5. 查询Score表中成绩为85,86或88的记录
    select * from score where degree=85 or degree=86 or degree=88;
  6. 查询Student表中“95031”班或性别为“女”的同学记录
    select * from student where class='95031' and ssex='女';
  7. 以Class降序查询Student表的所有记录
    select * from student order by class desc;
  8. 以Cno升序、Degree降序查询Score表的所有记录
    select * from score order by degree desc , cno;
  9. 查询“95031”班的学生人数
    select count(*) as '人数' from student where class='95031';
  10. 查询Score表中的最高分的学生学号和课程号
    select * from score order by degree desc limit 1;
  11. 查询‘3-105’号课程的平均分
    select avg(degree) from score where CNO='3-105';
  12. 查询Score表中至少有5名学生选修的并以3开头的课程的平均分数
    select cno,avg(degree) from score where cno like '3%' group by cno having count(sno)>5;
  13. 查询最低分大于70,最高分小于90的Sno列
    select sno from score group by sno having min(degree)>70 and max(degree)<90;
  14. 查询所有学生的Sname、Cno和Degree列.
    Select sname,cno ,degree from student inner join score on student.sno=scire.sno;
  15. 查询“95033”班所选课程的平均分
    Select cno,avg(degree) as avg from score where sno in(select sno from student where class=’95033’)group by cno;
  16. 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录
    Select degree from score where sno=’109’ and cno=’3-105’;
  17. 查询score中选学一门以上课程的同学中分数为非最高分成绩的记录
    Select sno from score group by sno having count(*)>1;
    Select * from score where sno in (Select sno from score group by sno having count(*)>1)
  18. 查询所有教师和同学的name、sex和birthday
    select sname as name,ssex as sex,sbirthday as birthday from student union select tname as name,tsex as sex,tbirthday as birthday from teacher;
  19. 查询所有“女”教师和“女”同学的name、sex和birthday
    select sname as name,ssex as sex,sbirthday as birthday from student where ssex='女' unio
  20. 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录
    select * from score where DEGREE>(select DEGREE from score where sno='109'and CNO='3-105');
  21. 查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列
    Select sno,sname,sbirthday from student where year(sbirthday)=(select year(sbirthday) from student where sno=108);
  22. 查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列
    Select sno,sname,sbirthday from student where year(sbirthday)=(select year(sbirthday) from student where sno=108);
  23. 查询Student表中不姓“王”的同学记录
    Select * from student where sname not like '王%'
  24. 查询“张旭“教师任课的学生成绩
    Select degree from score,teacher,course where score.cno=course.cno and course.tno=teacher.tno and teacher.tname='张旭';
  25. 查询选修某课程的同学人数多于5人的教师姓名
    Select cno from score group by cno having count(*)>5;
    Select tname from teacher as t inner join course as c
    On t.tno=c.tno where cno in(Select cno from score group by cno having count(*)>5);
  26. 查询95033班和95031班全体学生的记录
    select * from student where class='95033' or '95031';
  27. 查询出“计算机系“教师所教课程的成绩表
    Select score.*from teacher as inner join course as con t.tno=c.cno inner join score as con c,cno=s.scno
  28. 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
    Select degree from score where cno=’3-105’
    And degree>all(Select degree from score where cno=’3-245’)
  29. 查询所有姓李的同学的sno,sname,cno,degree
    Select s.sno,sname,cno,degree from student as s inner join score as c no s.sno=c.sno where sname like ‘李%’;
  30. 查询成绩比该课程平均成绩低的同学的成绩表
    Select * from score where degree<(select avg (degree)from score as s where cno=s.cno);
  31. 查询所有任课教师的Tname和Depart
    select tname,depart from teacher where tno in (select tno from course where cno in (select distinct cno from score));
  32. 查询所有未讲课的教师的Tname和Depart
    select tname,depart from teacher where tname not in( select distinct tname from teacher,course,score where teacher.tno=course.tno and course.cno=score.cno);
  33. 查询至少有2名男生的班号
    select class from student where ssex='男' group by class having count(*)>1;
  34. 查询“男”教师及其所上的课程
    select tname,cname from teacher ,course where tsex='男' and teacher.tno=course .tno;
  35. 查询和“李军”同性别并同班的同学Sname
    select sname from student where ssex=(select ssex from student where sname='李军') and sname not in ('李军') and class =(select class from student where sname='李军');
原创文章 53 获赞 323 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Long_UP/article/details/106124087