SQL查询语句综合练习demo

在这里插入图片描述
写SQL语句之间首先罗列各表之间的字符段,主外键相互对应,方便联表查询
create database tongji;
use tongji;
create table student
(
Sno varchar(20) not null primary key comment"学号" ,
Sname varchar(20) not null comment"学生姓名",
Ssex varchar(20) not null comment"学生性别",
Sbirthday datetime comment"学生出生年月",
Class varchar(20) comment"学生所在班级"
)comment=“学生”;

create table teacher
(
Tno varchar(20) not null primary key comment"教工编号(主码)",
Tname varchar(20) not null comment"教工姓名 “,
Tsex varchar(20) not null comment “教工性别” ,
Tbirthday datetime comment"教工出生年月”,
Prof varchar(20) comment"职称",
Depart varchar(20) not null comment"教工所在部门"
)comment=“老师”;

create table Course
(
Cno varchar(20) not null primary key comment “课程号”,
Cname varchar(20) not null comment “课程名称” ,
Tno varchar(20) not null comment"教工编号" references teacher(Tno)
)comment=“课程表”;

create table Score
(
Sno varchar(20) not null comment “学号(外码)” references student(Sno) ,
Cno varchar(20) not null comment"课程号(外码)" references Course(Cno) ,
primary key(Sno,Cno),
Degree Decimal(4,1) comment"成绩"
)comment=“分数表”;
create table grade(low  int(3),upp  int(3),rank  char(1));
insert into grade values(90,100,‘A’),(80,89,‘B’),(70,79,‘C’),(60,69,‘D’),(0,59,‘E’);
– 1、 查询Student表中的所有记录的Sname、Ssex和Class列。
SELECT sname, ssex, class FROM student ;
– select Sname,Ssex,Class from student
– 2、 查询教师所有的单位即不重复的Depart列。
SELECT DISTINCT depart from teacher;
– select distinct depart from teacher
– 3、 查询Student表的所有记录。
SELECT * FROM student;
– select * from student
– 4、 查询Score表中成绩在60到80之间的所有记录。
SELECT * from score where degree between 60 and 80;
– select * from score where degree between 60 and 80
– 5、 查询Score表中成绩为85,86或88的记录。
SELECT * from score where degree in (85,86,88);
– select * from score where degree in(85,86,88)
– 6、 查询Student表中“95031"班或性别为“女"的同学记录。
SELECT * from student where class=“95031” or ssex =“女”;
– select * from student where class=‘95031’ or Ssex=‘女’
– 7、 以Class降序查询Student表的所有记录。
SELECT * from student ORDER BY class desc;
– select * from student order by class desc
– 8、 以Cno升序、Degree降序查询Score表的所有记录。
SELECT * FROM score ORDER BY cno asc ,degree desc;
– select * from score order by cno,degree desc 
– 9、 查询“95031"班的学生人数。
SELECT count(1) from student where class=“95031”;
– select count() from student where class=‘95031’
– 10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
SELECT sno, cno from score WHERE degree in (SELECT max(degree) from score);
– select sno,cno from score where degree =(select max(degree) from score)
– 11、 查询每门课的平均成绩。[分组的字段刚好和聚合函数对应]
SELECT cno 课程名称 ,avg(degree)平均分 from score GROUP BY cno ;
– select cno,avg(degree) from score group by cno
– 12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
SELECT avg(degree) 平均分 from score where cno like “3%” group by cno HAVING count(1)>5;
– select avg(degree) from score where cno like’3%’ group by cno having count(
)>=5
– select avg(degree) from score group by cno having count(*)>=5 and  cno like’3%’
– 13、查询分数大于70,小于90的Sno列。
SELECT sno from score where degree between 70 and 90;
– select sno from score where degree>70 and degree<90
– 14、查询所有学生的Sname、Cno和Degree列。
SELECT sname ,cno, degree from student s, score sc where s.sno=sc.sno;
– select sname,cno,degree from score join student on score.sno = student.sno
– 15、查询所有学生的Sno、Cname和Degree列。
SELECT sno, cname ,degree from course c, score sc where c.cno=sc.cno;
– select sno,cname,degree from score join course on score.cno=course.cno
– 16、查询所有学生的Sname、Cname和Degree列。
SELECT sname ,cname ,degree from student s,score sc,course c where s.sno=sc.sno and c.cno=sc.cno;
– select sname,cname,degree from score join student on student.sno=score.sno join course on score.cno = course.cno
– select sname,cname,degree from score,student,course where student.sno=score.sno and score.cno = course.cno
– 17、 查询“95033"班学生的平均分。
SELECT avg(degree) from score where sno in (SELECT sno from student where class=“95033”);
– select avg(degree) from score where sno in(select sno from student where class=‘95033’)
– 18、【1】-- 现查询所有同学的Sno、Cno和rank列。

– select sno,cno,rank from score join grade on degree between low and upp
– SELECT sno, cno ,rank from score ,garde where degree BETWEEN low and upp order by rank;
– select sno,cno,rank from score,grade where score.degree between grade.low and grade.upp;
– select score.cno,score.sno,grade.rank from score,grade where score.degree>grade.low and score.degree<=grade.upp
– 19【2】  查询选修“3-105"课程的成绩高于“109"号同学成绩的所有同学的记录。【】
SELECT * from score where cno=“3-105” and degree >(SELECT max(degree)from score where sno=“109”);
– select * from score where cno=‘3-105’ and degree>(select degree from score where sno=‘109’ and cno=‘3-105’)
– select * from score where cno=‘3-105’ and degree>(select max(degree) from score where sno=‘109’)
– 20、【3】查询score中选学多门课程的同学中分数为非最高分成绩的记录。
– 查询学生记录;选 学多门;成绩小于最高分[两者成绩必须要关联起来]
SELECT * from score s1 where sno in (SELECT sno from score GROUP BY sno HAVING count(1)>1) and degree <(SELECT max(degree)from score s2 where s1.cno=s2.cno);
– select * from score a where sno in (select sno from score group by sno having count(*)>1) and degree <(select max(degree) from score b where b.cno=a.cno)
– 21、查询成绩高于学号为“109"、课程号为“3-105"的成绩的所有记录。
SELECT * from score where degree >(select degree from score where sno="109"and cno=“3-105”);
– select * from score where degree>(select degree from score where sno=‘109’ and cno=‘3-105’)
– 22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
SELECT sno, sname,sbirthday from student where year(sbirthday)=(SELECT year(sbirthday)from student where sno=“108”);
– select * from student where YEAR(sbirthday) = (select YEAR(sbirthday) from student where sno=‘108’)
– 23、查询“张旭“教师任课的学生成绩。
SELECT * from score where cno in (select cno from course where tno in (SELECT tno from teacher where tname=“张旭”));
– select * from score where cno in(select cno from course where tno in(select tno from teacher where tname=‘张旭’))
– select from score join course on course.cno=score.cno join teacher on course.tno=teacher.tno where teacher.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(1)>5));
– select tname from teacher where tno in(select tno from course where cno in(select cno from score group by cno having count(
)>=5))
– 25、查询95033班和95031班全体学生的记录。
SELECT * from student where class in (“95033”,“95031”);
– select * from student where class in(‘95033’,‘95031’)
– 26、  查询存在有85分以上成绩的课程Cno.
SELECT DISTINCT cno from score where degree>85;
– select distinct cno from score where degree>85
– 27、查询出“计算机系“教师所教课程的成绩表。
SELECT * from score where cno in (select cno from course where tno in (select tno from teacher where depart=“计算机系”));
– select * from score where cno in(select cno from course where tno in(select tno from teacher where depart=‘计算机系’))
– 【4】 28、查询“计算机系"与“电子工程系“不同职称的教师的Tname和Prof。[【自查询】双不同]
SELECT tname,prof FROM teacher t1 where prof not in (select prof from teacher t2 where t1.depart !=t2.depart );
– select tname,prof from teacher where depart=‘计算机系’ and prof not in(select prof from teacher where depart=‘电子工程系’)union
– select tname,prof from teacher where depart=‘电子工程系’ and prof not in(select prof from teacher where depart=‘计算机系’)
– select tname,prof from teacher a where prof not in(select prof from teacher b where b.depart != a.depart)
– 29【5】、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245"的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。【为啥用any】

SELECT cno, sno, degree from score where cno=“3-105” and degree >any(SELECT degree from score where cno=“3-245”) ORDER BY degree desc;
– 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 cno, sno, degree from score where cno=“3-105” and degree >any(SELECT degree from score where cno=“3-245”) ;
– select * from score where cno=‘3-105’ and degree>any(select degree from score where cno=‘3-245’)
– 31、【6】 查询所有教师和同学的name、sex和birthday.【union】
SELECT sname,ssex,sbirthday from student union select tname ,tsex,tbirthday from teacher;
– select sname,ssex,sbirthday from student union select tname,tsex,tbirthday from teacher
– 32、查询所有“女"教师和“女"同学的name、sex和birthday.
SELECT concat(“学生”,sname),ssex,sbirthday from student where ssex= "女"union ALL select concat(“教师”,tname ),tsex,tbirthday from teacher where tsex= “女”;
– select sname,ssex,sbirthday from student where ssex=‘女’ unionselect tname,tsex,tbirthday from teacher where tsex=‘女’
– 33、 查询成绩比该课程平均成绩低的同学的成绩表。
SELECT * FROM score s1 where degree <(SELECT avg(degree)from score s2 where s1.cno=s2.cno );
– select * from score a where degree<(select avg(degree) from score b where b.cno=a.cno)
– 34、 查询所有任课教师的Tname和Depart.【学生上老师的课,cno不为空】
SELECT tname, depart from teacher where tno in(select tno from course where cno in (select DISTINCT cno from score));
– select tname,depart from teacher where tno in(select tno from course where cno in (select distinct cno from score))
– 35 、 查询所有未讲课的教师的Tname和Depart.
SELECT tname, depart from teacher where tno not in(select tno from course where cno in (select DISTINCT cno from score));
– select tname,depart from teacher where tno not in(select tno from course where cno in (select distinct cno from score))
– 36、查询至少有2名男生的班号。
SELECT class from student where ssex=“男” GROUP BY class HAVING count(1)>1;
– select class from student where ssex=‘男’ group by class having count() >1
– 37、查询Student表中不姓“王"的同学记录。
SELECT * from student where sname not like “王%”;
– select * from student where sname not like ‘王%’
– 38、查询Student表中每个学生的姓名和年龄。
SELECT sname, FLOOR(DATEDIFF(NOW(),sbirthday)/365) from student;
– select sname,year(NOW())-year(sbirthday)from student
– 39、查询Student表中最大和最小的Sbirthday日期值。
SELECT max(sbirthday),min(sbirthday) from student;
– select max(sbirthday),min(sbirthday) from student
– 40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
SELECT * from student order by class desc, sbirthday asc;
– select * from student order by class desc,sbirthday asc
– 41、查询“男"教师及其所上的课程。
SELECT tname ,cname from teacher t, course c where c.tno=t.tno and tsex =“男”;
– select tname,cname from course,teacher where course.tno = teacher.tno and teacher.tsex=‘男’
– 42、查询最高分同学的Sno、Cno和Degree列。
SELECT * from score where degree =(SELECT max(degree)from score );
– select * from score where degree = (select max(degree) from score)
– 43、查询和“李军"同性别的所有同学的Sname.
SELECT sname from student where ssex=(SELECT ssex from student where 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=“李军” );
– select sname from student where ssex=(select ssex from student where sname=‘李军’) and class = (select class from student where sname=‘李军’)
– 45、查询所有选修“计算机导论"课程的“男"同学的成绩表。
SELECT score.
from score where cno in (SELECT cno from course where cname=“计算机导论”)and sno in (select sno from student where ssex=“男”);
– select * from score where sno in(select sno from student where ssex=‘男’) and cno in(select cno from course where cname=‘计算机导论’)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_45442261

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值