mysql经典45道题_MYSQL数据库45道练习题--第⼀题查询Student表中的所有记录的Sname、Ssex和Class列。
select Sname,Ssex,Class from Student;
--第⼆题查询教师所有的单位即不重复的Depart列。
select distinct Depart from Teacher;
--第三题、 查询Student表的所有记录
select * from Student;
--第四题、 查询Score表中成绩在60到80之间的所有记录。
select * from Score where Degree between 60 and 80;
--第五题、 查询Score表中成绩为85,86或88的记录。
select * from Score where Degree = 85 or Degree = 86 or Degree = 88;
--第六题、 查询Student表中“95031”班或性别为“⼥”的同学记录。如何制作html网页相册
select * from Student where Class = 95031 or Ssex = '⼥';
--第七题、 以Class降序查询Student表的所有记录。
select * from Student order by Class asc;
--第⼋题、 以Cno升序、Degree降序查询Score表的所有记录。
select * from Score order by Cno asc , Degree desc;
--第九题、 查询“95031”班的学⽣⼈数。
select count(*) from Student where Class = 95031;
--第⼗题、 查询Score表中的最⾼分的学⽣学号和课程号。(⼦查询或者排序)
select Sno,Cno from Score where Degree = (select max(Degree) from Score);
--第⼗⼀题、 查询每门课的平均成绩。
select avg(Degree) from Score where Cno='3-245';
select avg(Degree) from Score where Cno = '3-105';
select avg(Degree) from Score where Cno = '6-166';
select distinct Cno from Score;
select Cno,avg(Degree) from Score where select distinct Cno from Score;
--上⾯是错误历程-------------------------------------------------------------------------------------
select Cno,avg(Degree) from Score group by Cno;
--第⼗⼆题、查询Score表中⾄少有5名学⽣选修的并以3开头的课程的平均分数。
select CNO,avg(Degree) from Score where Cno like '3%' group by Cno having count(*)>4;
--先取⾸字母为3的 在分组 分组 完成后筛选条件为 Cno数量⼤于等于5;
--where 后⾯不能加聚合函数-----------------------------------------------------------------------------------------
--第⼗三题、查询分数⼤于70,⼩于90的Sno列。
select Sno,Degree from Score where Degree>70 and Degree<90;
--第⼗四题、查询所有学⽣的Sname、Cno和Degree列。
select Sname,Cno,Degree from Student join Score on Student.Sno = Score.Sno;
--⽅法⼆
select Student.Sname,Score.Cno,Score.Degree from Student,Score where Student.Sno = Score.Sno;
--第⼗五题、查询所有学⽣的Sno、Cname和Degree列。linux查看文件占用空间大小
编程培训推荐
select Sno,Cname,Degree from Course join Score on (Score.Cno = Course.Cno);
--第⼗六题、查询所有学⽣的Sname、Cname和Degree列。
select Sname,Cname,Degree from Student join Score on (Student.Sno = Score.Sno) join Course on (Course.Cno = Score.Cno);
--第⼗七题、 查询“95033”班学⽣的平均分。
select Class,avg(Degree) from Score join Student on(Student.Sno = Score.Sno) where Class = 95033;
--第⼗⼋题、 假设使⽤如下命令建⽴了⼀个grade表:
create table grade(low int(3),upp int(3),rank char(1))
insert into grade values(90,100,'A');
insert into grade values(80,89,'B')
insert into grade values(70,79,'C')
insert into grade values(60,69,'D')
insert into grade values(0,59,'E')
--现查询所有同学的Sno、Cno和rank列。
select Sno,Cno,rank from grade join Score;
select Sno,Cno,rank from grade join Score on (Degree between low and upp);
--第⼗九题、 查询选修“3-105”课程的成绩⾼于“109”号同学成绩的所有同学的记录。
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') group by Sno having Cno = '3-105';
select Sno from Score where Cno = '3-105' and Degree > (select max(Degree) from Score where Sno = 109 and Cno = '3-105');
--第⼆⼗题、查询score中选学多门课程的同学中分数为⾮最⾼分成绩的记录。
select Sno from Score group by Sno having count(Cno)>1--选多门课同学的学号
select max(Degree) from Score--最⾼分
select * from Score where Degree < (select max(Degree) from Score)--⼩于最⾼分的学⽣信息
--选取选修多门课程且⾮最⾼成绩的学⽣学号
select sno from Score where Degree < (select max(Degree) from Score) group by Sno having count(
Cno)>1;
--该学号所有信息
select * from Score where sno in (select sno from Score where Degree < (select max(Degree) from Score) group by Sno having count(Cno)>1)
--第⼆⼗⼀题、 查询成绩⾼于学号为“109”、课程号为“3-105”的成绩的所有记录。
select * from Score where Degree > (select max(Degree) from Score where Sno = 109) and Cno = '3-105';
--第⼆⼗⼆题、查询和学号为108的同学同年出⽣的所有学⽣的Sno、Sname和Sbirthday列。
android studio网址
select Sno,Sname,Sbirthday from Student where year(Sbirthday) = (select year(Sbirthday) from Student where Sno = 107);
--第⼆⼗三题、查询“张旭“教师任课的学⽣成绩。
select Tno from Teacher where Tname = '张旭'
select Cno from Course where Tno = (select Tno from Teacher where Tname = '张旭')
select Degree from Score where Cno = (select Cno from Course where Tno = (select Tno from Teacher where Tname = '张旭'))
--第⼆⼗四题、查询选修某课程的同学⼈数多于5⼈的教师姓名。
select Cno from Score group by Cno having count(Cno)>5
select Tno from Course where Cno in (select Cno from Score group by Cno having count(Cno)>5)
select Tname from Teacher where Tno in (select Tno from Course where Cno in (select Cno from Score group by Cno having count(Cno)>5))
--第⼆⼗五题、查询95033班和95031班全体学⽣的记录。
select * from Student where class = 95033 or class = 95031
--第⼆⼗六题、 查询存在有85分以上成绩的课程Cno.
select distinct Cno from Score where Degree > 85
--第⼆⼗七题、查询出“计算机系“教师所教课程的成绩表。
select Tno from Teacher where Depart = '计算机系'
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 = '计算机
系'))
--第⼆⼗⼋题、查询“计算 机系”与“电⼦⼯程系“不同职称的教师的Tname和Prof。
select Tname,Prof from Teacher where Prof in (select Prof from Teacher where Depart = '计算机系' or Depart = '电⼦⼯程系' group by Prof having count(Prof) = 1)
--select Prof from Teacher where Depart = '计算机系' or Depart = '电⼦⼯程系' group by Prof having count(Prof) = 1
--第⼆⼗九题、查询选修编号为“3-105“课程且成绩⾄少⾼于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从⾼到低次序排序。
select Cno,Sno,Degree from Score where Cno = '3-105' and Degree >= (select max(Degree) from Score where Cno = '3-
245')
order by Degree asc
--第三⼗题、查询选修编号为“3-105”且成绩⾼于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
select Cno,Sno,Degree from Score where Cno = '3-105' and Degree >= (select max(Degree) from Score where Cno = '3-
245')
--第三⼗⼀题、 查询所有教师和同学的name、sex和birthday.
select Tname,Tsex,Tbirthday from Teacher union select Sname,Ssex,Sbirthday from Student
--第三⼗⼆题、查询所有“⼥”教师和“⼥”同学的name、sex和birthday.
select Tname,Tsex,Tbirthday from Teacher where Tsex = '⼥' union select Sname,Ssex,Sbirthday fro
m Student where Ssex = '⼥'
--第三⼗三题、 查询成绩⽐该课程平均成绩低的同学的成绩表。
select Cno,avg(Degree) from Score group by Cno;
select * from Score join (select Cno as Pno,avg(Degree) as pj from Score group by Pno) as pjb on (Score.Cno = pjb.Pno)
select * from Score join (select Cno as Pno,avg(Degree) as pj from Score group by Pno) as pjb on (Score.Cno = pjb.Pno)
where Degree < pj;
--第三⼗四题、 查询所有任课教师的Tname和Depart.
select * from Score group by Cno
select distinct Tname,Depart from Score join Course on Course.Cno = Score.Cno join Teacher on Teacher.Tno = Course.Tno where Course.Cno in
absolutely not
(select Cno from Score group by Cno)
--第三⼗五题、 查询所有未讲课的教师的Tname和Depart.
select Cno from Score group by Cno
select Tno from Course where Cno not in (select Cno from Score group by Cno)
select Tname,Depart from Teacher where Tno in (select Tno from Course where Cno not in (select Cno from Score group
by Cno))
--第三⼗六题、查询⾄少有2名男⽣的班号。
select * from Student group by Class having count(Ssex) > 1 and Ssex = '男'
select Class from Student where Sno in (select Sno from Student group by Class having count(Ssex) > 1 and Ssex = '男')
--第三⼗七题、查询Student表中不姓“王”的同学记录。
select * from Student where Sname not in (select Sname from Student where Sname like '李%')
--第三⼗⼋题、查询Student表中不姓“王”的同学记录。
select sname from Student where Sname not like '王%';mysql面试题34道经典
-- 第三⼗九题、查询Student表中最⼤和最⼩的Sbirthday⽇期值。
select MAX(sbirthday) as '最⼤值',MIN(sbirthday)'最⼩值' from Student
-- 第四⼗题、以班号和年龄从⼤到⼩的顺序查询Student表中的全部记录。
select class,sbirthday from Student order by Class desc,Sbirthday asc
-- 第四⼗⼀题、查询“男”教师及其所上的课程。
select cname from course where tno in(select tno from teacher where tsex=’男’)
select tname,cname from teacher ,course = and tsex='男'
-- 第四⼗⼆题、查询最⾼分同学的Sno、Cno和Degree列。
(1)select * from score where Degree = (select max(Degree) from score)
(2)select top 1 * from score order by degree desc
-- 第四⼗三题、查询和“李军”同性别的所有同学的Sname.
select sname from Student where Ssex=(select Ssex from Student where Sname='李军')
-- 第四⼗四题、查询和“李军”同性别并同班的同学Sname.
select sname from Student where Ssex=(select Ssex from Student where Sname='李军')and class=(select Class from Student where Sname='李军');
-- 第四⼗五题、查询所有选修“计算机导论”课程的“男”同学的成绩表。
select Degree from score where Sno in(select Sno from student where Ssex='1') and Cno in (select Cno from course where Cname = '计算机导论');

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。