mysql练习题

1.查询Score表中成绩在60到80之间的所有记录

select * from scores where degree > 60 and degree < 80;

2.查询 score 表中成绩为85,86或88的记录

select * from scores where degree = 85 or degree = 86 or degree = 88;

3.以 cno 升序、degree降序查询 score 表的所有记录

select * from scores order by cno asc , degree desc ;

4.查询“95031”班的学生人数

select count(*) from students where class = "95031";

5.查询Score表中的最高分的学生学号和课程号

select sno,cno from scores order by degree desc limit 1;

6.查询‘3-105’号课程的平均分

select avg(degree) from scores where cno = '3-105' ;

7.查询Score表中至少有5名学生选修的并以3开头的课程的平均分数

select cno , avg(degree) from scores where cno like '3%' GROUP BY cno having count(*) > 5;

8.查询所有学生的Sname、Cno和Degree列

select sname ,cno , degree from students stu , scores sc where stu.sno = sc.sno

9.查询所有学生的Sno、Cname和Degree列。

select sc.sno, co.cname, degree from scores sc ,courses co where sc.cno = co.cno;

10.查询所有学生的Sname、Cname和Degree列

select st.sname Sname , sc.degree Degree , co.cname Cname from scores sc , students st, courses co where sc.sno = st.sno and sc.cno = co.cno;

select st.sname Sname , sc.degree Degree , co.cname Cname from scores sc join (students st ,courses co) on sc.sno = st.sno and sc.cno = co.cno;

11.查询“95033”班所选课程的平均分

select cno ,avg(sc.degree) from students st join scores sc on st.sno = sc.sno where class = '95033' group by cno ORDER BY cno desc;

12.查询所有同学的Sno、Cno和rank列

select sc.sno,sc.cno,gr.rank from scores sc join grade gr on sc.degree >= gr.low and sc.degree <= gr.upp order by gr.rank;

13.查询所有同学的sname、sno 、cno、cname和rank列

select st.sname,sc.sno,co.cname,sc.cno,gr.rank from scores sc join (students st ,courses co,grade gr) on sc.sno = st.sno and sc.cno = co.cno and sc.degree BETWEEN gr.low and gr.upp order by gr.rank;

14.查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录

select * from scores where cno = '3-105'and degree > (select degree from scores where sno = '109' and cno = '3-105') order by degree;

select sc1.cno ,sc1.sno,sc1.degree,sc1.id from scores sc1 join scores sc2 on sc1.degree > sc2.degree and sc2.sno = '109'and sc2.cno = '3-105' WHERE sc1.cno = '3-105'

15.查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。

select s1.sno,s1.sname,s1.sbirthday from students s1 join students s2 on year(s1.sbirthday) = year(s2.sbirthday) and s2.sno = '108' where s1.sno != '108'

select sno, sname, sbirthday FROM students WHERE sno != '108' and YEAR(sbirthday) = (SELECT YEAR(sbirthday)FROM students WHERE sno = '108');

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

select sc.cno ,sc.degree,sc.sno from teachers t join (courses co,scores sc) on t.tno = co.tno and co.cno = sc.cno where t.tname = '张旭'

SELECT * FROM scores WHERE cno =(SELECT cno FROM courses JOIN teachers ON courses.tno = teachers.tno AND tname = '张旭');

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

select t.tname from teachers t join courses co on t.tno = co.tno and co.cno = (select cno from scores GROUP BY cno having count(*) > 5);

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

select * from students where class in ('95033','95031')

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

SELECT DISTINCT cno from scores where degree > 85

select cno from scores GROUP BY cno having max(degree) > 85

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

select t.depart,co.cname ,sc.degree,sc.sno from teachers t join (courses co , scores sc) on t.tno =co.tno and co.cno = sc.cno where t.depart = '计算机系'

SELECT * FROM scores WHERE cno IN (SELECT cno FROM courses, teachers WHERE depart = '计算机系' AND courses.tno = teachers.tno);

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

SELECT tname name ,tsex sex ,tbirthday birthday from teachers union select sname name ,ssex sex ,sbirthday birthday from students

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

SELECT tname name ,tsex sex ,tbirthday birthday from teachers where teachers.tsex = '女' union select sname name ,ssex sex ,sbirthday birthday from students where students.sno = '女'

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

select * from scores sc1 where sc1.degree < (select avg(sc2.degree) from scores sc2 where sc1.cno = sc2.cno )

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

select t.tname ,t.depart from teachers t where t.tno in (select distinct courses.tno from scores join courses on scores.cno = courses.cno )

select distinct t.tname ,t.depart from teachers t join (scores sc,courses co) on sc.cno = co.cno and co.tno = t.tno

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

select class from students where ssex = '男' group by class having count(ssex) > 1

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

select * from students where sname not like '王%'

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

select sname,(year(now()) - year(sbirthday)) as age from students

你可能感兴趣的:(mysql练习题)