一、学生表 二、教师表mysql
三、课程表 四、分数表sql
2、练习题spa
一、查询Student表中的全部记录的Sname、Ssex和Class列。
mysql> select Sname,Ssex,Class from Student;
二、 查询教师全部的单位即不重复的Depart列
mysql> Select Depart from teacher group by Depart;
mysql> select distinct depart from teacher;
三、 查询Student表的全部记录。
mysql> select * from student;
四、 查询Score表中成绩在60到80之间的全部记录
mysql> select * from Score where Degree > 60 and Degree<80;
mysql> select * from score where degree between 60 and 80;
五、 查询Score表中成绩为85,86或88的记录。
mysql> select * from Score where Degree = 85 or Degree=86 or Degree = 88;
mysql> select * from Score where Degree in (85,86,88);
六、 查询Student表中“95031”班或性别为“女”的同窗记录。code
mysql> Select * from Student where Class=95031 or Ssex = '女';
七、 以Class降序查询Student表的全部记录。
mysql> Select * from Student order by Class desc;
八、 以Cno升序、Degree降序查询Score表的全部记录。 blog
mysql> Select * from (Select * from Score order by Cno) as s order by Degree desc;
mysql> select * from score ORDER BY cno asc,degree desc;
九、 查询“95031”班的学生人数。
mysql> Select count(1) from Student where Class = 95031;
十、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
mysql> Select Sno,Cno from Score where Degree = (Select max(Degree) from Score);
10.1查询Score表中除了每门课程最高分的学生学号和课程号。(子查询或者排序)排序
mysql> Select Sno,Cno from Score where Degree not in (Select max(Degree) from Score group by Cno);
十一、 查询每门课的平均成绩。io
mysql> Select Cno,avg(Degree) from Score group by Cno;
十二、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。class
mysql> Select Cno,avg(Degree) from group by Cno having count(1)>=5 and Cno like "3%";
mysql> Select Cno,avg(Degree) from Score group by Cno having count(1)>=5 and left(Cno,1)= "3";
mysql> Select Cno,avg(Degree) from Score group by Cno having count(Sno)>=3 and left(Cno,1)<>"3";
1三、查询分数大于等于70,小于等于90的Sno列。
select
mysql> Select Sno from Score where Degree between 70 and 90;
mysql> Select Sno from Score where Degree not between 70 and 90;
1四、查询全部学生的Sname、Cno和Degree列。
nio
mysql> Select Sname,Cno,Degree from Student join Score on Student.Sno=Score.Sno;
1五、查询全部学生的Sno、Cname和Degree列。
mysql> Select Sno,Cname,Degree from Score join Course on Course.Cno=Score.Cno;
1六、查询全部学生的Sname、Cname和Degree列。
mysql> Select Sname,Cname,Degree from Course join (Select Sname,Cno,Degree from Student join Score on Student.Sno=Score.Sno) as s on Course.Cno=s.Cno;
1七、 查询“95033”班学生的平均分。
mysql> Select avg(Degree) from (Select Degree from Score join (Select Sno from Student where Class=95033) as s on Score.Sno = s.Sno) as d;
mysql> Select avg(Degree) from Score where Sno in (Select Sno from Student where Class = 95033);
1八、现查询全部同窗的Sno、Cno和rank列
1九、查询选修“3-105”课程的,而且成绩高于“109”号同窗成绩的全部同窗的记录。
mysql> Select * from Score where Degree > (Select Degree from Score where Cno = "3-105" and Sno = 109) and Cno ="3-105";
2一、 查询成绩高于学号为“109”、课程号为“3-105”的成绩的全部记录。
mysql> Select * from Score where Degree > (Select Degree from Score where Cno = "3-105" and Sno = 109);
2二、查询和学号为108的同窗同年出生的全部学生的Sno、Sname和Sbirthday列。
mysql> Select Sno,Sname,Sbirthday from Student where year(Sbirthday) = (Select year(Sbirthday) from Student where Sno = 108);
2三、查询“张旭“教师任课的学生成绩。
mysql> Select Sno,Degree,Cno from Score where Cno in (Select Cno from Course where Tno in (Select Tno from Teacher where Tname = "张旭"));
2四、查询选修某课程的同窗人数多于5人的教师姓名。
mysql> Select Tname from Teacher where Tno in (Select Tno from Course where Cno in (Select Cno from Score group by Cno having count(1) > 5));
2五、查询95033班和95031班全体学生的记录。
mysql> Select * from Student where Class in (95033,95031);
2六、 查询存在有85分以上成绩的课程Cno.
mysql> Select distinct Cno from Score where Degree > 85;
2七、查询出“计算机系“教师所教课程的成绩表。
mysql> select * from score where cno in (select cno from course where Tno in (select tno from teacher where depart = "计算机系"));
2九、查询选修编号为“3-105“课程且成绩至少高于一个选修编号为“3-245”的同窗的Cno、Sno和Degree,并按Degree从高到低次序排序。
mysql> Select Sno,Cno,Degree from Score where Cno = "3-105" and Degree > any(Select Degree from Score where Cno="3-245");
30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同窗的Cno、Sno和Degree.
mysql> Select Sno,Cno,Degree from Score where Cno = "3-105" and Degree > all(Select Degree from Score where Cno="3-245");
3一、 查询全部教师和同窗的name、sex和birthday.
select sname,ssex,sbirthday from student union all select tname,tsex,tbirthday from teacher;
3二、查询全部“女”教师和“女”同窗的name、sex和birthday.
mysql> Select s.Sname,s.ssex,s.sbirthday from (select sname,ssex,sbirthday from student union all select tname,tsex,tbirthday from teacher) as s where s.Ssex = "女";
(select sname,ssex,sbirthday from student where ssex = "女") union all (select tname,tsex,tbirthday from teacher where tsex = "女");
3三、 查询成绩比该课程平均成绩低的同窗的成绩表
Select * from Score s1 where Degree < (Select avg(degree) from score s2 group by cno having s1.cno=s2.cno);
3四、 查询全部任课教师的Tname和Depart
mysql> Select tname,depart from teacher where tno in (select distinct tno from course where cno in (select distinct cno from score group by cno));
3六、查询至少有2名男生的班号
错误:
mysql> select s.class from (select * from student where ssex = "男") as s group by s.class having count(1);
正确:
select class from student where ssex='男' group by class having count(1) >=2
3七、查询Student表中不姓“王”的同窗记录
mysql> select * from student where left(sname,1) <>"王";
mysql> select * from student where sname not like '王%';
3八、查询Student表中每一个学生的姓名和年龄
mysql> select sname,year(now())-year(Sbirthday) as age from student;
3九、查询Student表中最大和最小的Sbirthday日期值。
mysql> select max(Sbirthday),min(Sbirthday) from student;
40、以班号和年龄从大到小的顺序查询Student表中的所有记录。
mysql> select * from student order by class desc,Sbirthday;
mysql> select * from student ORDER BY class,year(now())-year(sbirthday);
4一、查询“男”教师及其所上的课程。
mysql> select * from course where tno in (select tno from teacher where tsex = "男");
4二、查询最高分同窗的Sno、Cno和Degree列。
mysql> select sno,cno,degree from score where degree = (select max(degree) from score);
4三、查询和“李军”同性别的全部同窗的Sname.
mysql> Select sname from student where ssex = (select ssex from student where sname ="李军");
4四、查询和“李军”同性别并同班的同窗Sname.
mysql> Select sname from student where (ssex,class) in (select ssex,class from student where sname ="李军");
4五、查询全部选修“计算机导论”课程的“男”同窗的成绩表.
mysql> select * from score where cno = (select cno from course where cname = "计算机导论") and sno in (select sno from student where ssex = "男")
4七、查询各科选课总人数和及格人数,以以下形式显示:课程编号,课程名称,总人数,及格人数,按课程编号升序排列
select s.cno,cname,allnum,passnum from (select s1.cno,allnum,passnum from (select Cno,count(*) as allnum from score group by cno) as s1 join (select Cno,count(*) as passnum from score where degree >= 80 group by cno) as s2 on s1.cno=s2.cno) as s join course on s.cno=course.cno order by s.cno;