sql50题

2021-05-15  本文已影响0人  小幸运Q

初始化

---学生

CREATE TABLE `Student`(
`s_id` VARCHAR(20),
`s_name` VARCHAR(20) NOT NULL DEFAULT '',
`s_birth` VARCHAR(20) NOT NULL DEFAULT '',
`s_sex` VARCHAR(10) NOT NULL DEFAULT '',
PRIMARY KEY(`s_id`)
);

--课程表

CREATE TABLE `Course`(
`c_id` VARCHAR(20),
`c_name` VARCHAR(20) NOT NULL DEFAULT '',
`t_id` VARCHAR(20) NOT NULL,
PRIMARY KEY(`c_id`)
);

--教师表

CREATE TABLE `Teacher`(
`t_id` VARCHAR(20),
`t_name` VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(`t_id`)
);

--成绩表

CREATE TABLE `Score`(
`s_id` VARCHAR(20),
`c_id` VARCHAR(20),
`s_score` INT(3),
PRIMARY KEY(`s_id`,`c_id`)
);

--插入学生表测试数据

insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');

--课程表测试数据

insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');

--教师表测试数据

insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');

--成绩表测试数据

insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);

  1. 查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号
select distinct b.s_id
from student st
inner join (select s_id,s_score from score where c_id="01") a on st.s_id
inner join (select s_id,s_score from score where c_id="02") b on a.s_id=b.s_id
where a.s_score>b.s_score;

我的解法:貌似后面有where就只能用on+多个判定来实现where的功能了

select distinct a.s_id from score a
inner join (select s_id,s_score,c_id from score) as b 
on a.s_id=b.s_id and a.c_id="01" and b.c_id="02"
where a.s_score>b.s_score;
  1. 查询平均成绩大于60分的学生的学号和平均成绩

group by 没法搭配where只能搭配having

select s_id,avg(s_score) from score group by s_id 
having avg(s_score)>60;

我的解法:

select * from (select s_id,avg(s_score) as avg from score 
group by s_id) as a where a.avg>60;
  1. 查询所有学生的学号、姓名、选课数、总成绩
select A.s_id,A.s_name,count(B.s_id),count(B.s_score) 
from student A join score B on B.s_id=A.s_id group by A.s_id,A.s_name;
  1. 查询没学过“张三”老师课的学生的学号、姓名

where xxx not in

select s_id,s_name from student where s_id not in (select s_id from score sc left join course cs on cs.c_id=sc.c_id inner join teacher tc on tc.t_id=cs.t_id where tc.t_name="张三");
  1. 查询学过“张三”老师所教的所有课的同学的学号、姓名
select st.s_id,st.s_name from student st join score sc on sc.s_id=st.s_id join course cs on cs.c_id=sc.c_id join teacher tc on tc.t_id=cs.t_id where tc.t_name="张三";
  1. 查询学过编号为“01”的课程并且也学过编号为“02”的课程的学生的学号、姓名

select a.id from () a inner join () b on a.id=b.id

select s_id,s_name from student where s_id in 
(select a.s_id from 
(select s_id,c_id from score where c_id="01") a 
inner join (select s_id,c_id from score where c_id="02") b 
on a.s_id=b.s_id);

我的:

select s_id,s_name from student where s_id in (select s_id from 
(select s_id from score sc inner join course cs 
on cs.c_id=sc.c_id and cs.c_id="01") a 
where s_id in 
(select s_id from score sc inner join course cs 
on cs.c_id=sc.c_id and cs.c_id="02")
);
  1. 查询没有学全所有课的学生的学号、姓名
select st.s_id,st.s_name from student st inner join score sc on 
st.s_id=sc.s_id group by st.s_id,st.s_name 
having count(c_id)<(select count(distinct c_id) from course);

我的:

select s_id,s_name from student where s_id in 
(select s_id from score group by s_id having count(s_id)<
(select count(c_id) from course)
);
上一篇 下一篇

猜你喜欢

热点阅读