MySQL 45道基础题

一 . 表的创建

-- 学生表

create table student(
    sno varchar(20) primary key not null comment'学号(主码)',
    sname varchar(20) not null comment'学生姓名',
    ssex varchar(20) not null comment'学生性别',
    sbirthday datetime comment'学生出生年月',
    class varchar(20) comment'学生所在班级'
    );


-- 课程表

create table course(
    cno varchar(20) primary key not null comment'课程号(主码)',
    cname varchar(20) not null comment'课程名称',
    tno varchar(20) not null comment'教工编号'
);


-- 成绩表

create table score(
    id int primary key auto_increment comment'主键自增',
    sno varchar(20) not null comment'学号',
    cno varchar(20) not null comment'课程号',
    degree Decimal(4,1) comment'成绩'
);


-- 教师表

create table teacher(
    tno varchar(20) primary key not null 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'教工所在部门'
);

二 . 插入数据

insert into student values
('108','曾华','男','1977-09-01','95033'),
('105','匡明','男','1977-09-01','95031'),
('107','王丽','女','1977-09-01','95033'),
('101','李军','男','1977-09-01','95033'),
('109','王芳','女','1977-09-01','95031'),
('103','陆君','男','1977-09-01','95031');

insert into course values
('3-105','计算机导论','825'),
('3-245','操作系统','804'),
('6-166','数字电路','856'),
('9-888','高等数学','831');

insert into score(sno,cno,degree) values
('103','3-245','86'),
('105','3-245','75'),
('109','3-245','68'),
('103','3-105','92'),
('105','3-105','88'),
('109','3-105','76'),
('101','3-105','64'),
('107','3-105','91'),
('108','3-105','78'),
('101','6-166','85'),
('107','6-166','79'),
('108','6-166','81');

insert into teacher values
('804','李诚','男','1958-12-02','副教授','计算机系'),
('856','张旭','男','1969-03-12','讲师','电子工程系'),
('825','王萍','女','1972-05-05','助教','计算机系'),
('831','刘冰','女','1977-08-14','助教','电子工程系');

三.查询问题及解答

-- 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 in (85,86,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 cno ,degree desc;


-- 9、 查询“95031”班的学生人数。

select COUNT(*)
from student 
group by class
having class = '95031';


-- 10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)

 select max(degree) from score   #先写出score的最高分
 select * from score where degree = (select max(degree) from score);
 select sno,cno from score where degree = (select max(degree) from score);  -- 可以用已有的等于最大的
 
 select * from score order by degree desc limit 1;

-- 11、 查询每门课的平均成绩。

select sno,AVG(degree)
from score
group by sno;


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

  -- 先找出至少有5名学生选修的课程
    SELECT COUNT(*)
    from score
    group by cno 
    having count(*) >5;
    -- 再找出以三开头的课程
    select cno
    from score
    where cno like '3%';
 
select cno,count(*),avg(degree) from score where cno like '3%' group by cno having count(*) >= 5;

-- 13、查询分数大于70,小于90的Sno列。

select sno ,degree
from score
where degree >70 and degree < 90;


-- 14、查询所有学生的Sname、Cno和Degree列。(多表查询)

select sname,cno,degree 
from student a 
join score b on a.sno = b.sno;


-- 15、查询所有学生的Sno、Cname和Degree列。

select a.sno ,cname,degree
from student a
join score b on a.sno = b.sno
join course c on b.cno = c.cno;

select a.sno,cname,degree 
from student a ,score b ,course;


-- 16、查询所有学生的Sname、Cname和Degree列。

select sname,cname,degree 
from student ,score,course;


-- 17、查询“95033”班学生的平均分。

select AVG(degree)
from score ,student
where class = '95033';


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

  -- 先查询选修为3-105的课程的成绩
    SELECT distinct degree 
    from score
    where cno = '3-105' and sno = '109';
    
select *-- sno,cno,degree 
from score 
where cno = '3-105' and degree > (select distinct degree from score where cno like '3-105' and sno like '109');


-- 19、查询score中选学多门课程的同学中分数为非最高分成绩的记录。

-- 选多门课程的同学
select sno,COUNT(cno),degree
from score
group by sno ,degree
having count(cno) >1
order by degree DESC
limit 1,100;

select * 
from score a
where Sno in (select Sno from score group by Sno having count(*)>1 ) 
and Degree not in (select max(Degree) 
from score a 
where Sno in (select Sno from score group by Sno having count(*)>1 ));

-- 20、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。

-- 学号为109课程号为3-105的成绩
select distinct degree
from score 
where sno ='109' and cno = '3-105';
SELECT *
from score
where degree > (select distinct degree
from score 
where sno ='109' and cno = '3-105');


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

select sno,sname,sbirthday
from student
where sbirthday = (select sbirthday from student where sno = '108');


-- 22、查询“张旭“教师任课的学生成绩(姓名)。

select DISTINCT score.sno,sname,degree,tname 
from score,course,teacher,student
where tname = '张旭';


select tno from teacher where tname = '张旭';    #找出教师编号
select cno from course where tno = (select tno from teacher where tname = '张旭'); #找出课程编号
select sno,degree from score where cno = (select cno from course where tno = (select tno from teacher where tname = '张旭'));

select student.sno,degree,sname 
from score 
join student on score.sno = student.sno 
where student.sno in (select sno from score where cno = (select cno from course where tno = (select tno from teacher where tname = '张旭')));

-- 23、查询考计算机导论的学生成绩

select sno,a.cno,cname,degree 
from score a
join course b on a.cno = b.cno;


-- 24、查询李诚老师教的课程名称

select cname
from teacher a 
join course b on a.tno = b.tno
where tname = '李诚';


-- 25、教高等数学的老师是哪个系的

select depart
from teacher a 
join course b on a.tno = b.tno 
where cname = '高等数学';


-- 26、查询选修某课程的同学人数多于5人的教师姓名。

    -- 查询哪一门课程选秀的人数多于五人
SELECT cno-- ,COUNT(cno)
from score
group by cno
having count(cno) > 5

select tname 
from course a 
join teacher b on a.tno = b.tno 
where cno = (SELECT cno-- ,COUNT(cno)
from score
group by cno
having count(cno) > 5);


-- 27、查询95033班和95031班全体学生的记录。

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


-- 28、查询成绩表中存在有85分以上成绩的课程Cno.

select cno ,degree 
from score
where degree > 85;


-- 29、查询出“计算机系“教师所教课程的成绩表。

select a.*,tname,depart
from course a 
join teacher b on a.tno = b.tno
where depart = '计算机系';


-- 30、查询选修编号为“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');

-- 31、查询所有教师和同学的name、sex和birthday.

select sname,ssex,sbirthday,tname,tsex,tbirthday 
from student a 
join score b on a.sno = b.sno 
join course c on c.cno = b.cno 
join teacher d on d.tno = c.tno;
 select tname as name,tsex as sex,tbirthday as birthday from teacher union select sname,ssex,sbirthday from student;

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

select tname as name ,tsex as sex, tbirthday as birthday 
from teacher where tsex = '女'
union select sname , ssex,sbirthday from student
where ssex = '女';


-- 33、查询所有成绩比3-105课程平均成绩低的同学的成绩表。

    -- 查看3-105的平均成绩
    select AVG(degree)
    from score
    where cno = '3-105';
    -- 查询所有的成绩
select degree 
from score 
where degree < (select avg(degree) from score where cno = '3-105');


-- 34、查询所有任课教师的Tname和Depart.

select tname , depart 
from teacher ;


-- 35、查询所有未讲课的教师的Tname和Depart

    -- 连接四张表
select c.cno,tname,depart 
from teacher a
left join course b on a.tno = b.tno 
left join score c on b.cno = c.cno;
    -- 查询score表中不存在的cno
select tname,depart
from (select c.cno,tname,depart 
from teacher a
left join course b on a.tno = b.tno 
left join score c on b.cno = c.cno) s
where cno is null;


-- 36、查询至少有2名男生的班号。

select class,count(ssex) 
from student 
where ssex = '男' 
group by class 
having count(ssex)>=2;


-- 37、查询Student表中不姓“王”的同学记录。

select *
from student 
where sname not like '王%';


-- 38、查询Student表中每个学生的姓名和年龄。

select sname,year(now())-YEAR(sbirthday) age
from student;


-- 39、查询Student表中最大和最小的Sbirthday日期值。

select MAX(sbirthday),MIN(sbirthday)
from student;


-- 40、以班号和年龄从大到小的顺序查询Student表中的全部记录。

select *
from student 
order by class ,YEAR(NOW()-year(sbirthday));


-- 41、查询“男”教师及其所上的课程。

select tname, tsex,cname
from teacher a 
join course b on a.tno = b.tno
where tsex = '男';


-- 42、查询最高分同学的Sno、Cno和Degree列。

select a.sno,cno,degree
from student a 
join score b on a.sno =b.sno 
where degree = (select MAX(degree) from score);


-- 43、查询和“李军”同性别的所有同学的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 = '李军');


-- 45、查询所有选修“计算机导论”课程的“男”同学的成绩表。

select b.*
from student a 
join score b on a.sno = b.sno 
join course c on b.cno = c.cno
where cname = '计算机导论' and ssex = '男';
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值