sql50题

作者: 小幸运Q | 来源:发表于2021-05-15 12:12 被阅读0次

初始化

---学生

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)
);

相关文章

  • sql50题

    mysql经典问题四表查询(教师,学生,成绩,课程表)_小龙人的博客-CSDN博客_查询不同课程成绩相同的学生的学...

  • SQL50题

    数据表介绍 --1.学生表 Student(SId, Sname, Sage, Ssex)SId 学生编号, Sn...

  • sql50题

    初始化 ---学生 --课程表 --教师表 --成绩表 --插入学生表测试数据 --课程表测试数据 --教师表测试...

  • 经典SQL50题

    二刷经典SQL面试50题,当当,决定将解法重新梳理一遍。从一刷的磕磕绊绊,自己就是常见错误解法,到二刷思维...

  • SQL50题---1~5题

    首先,创建表格和数据。我用的是Toad for Oracle这款软件,再最开始创建表格添加数据时,发现全选所有代码...

  • SQL50题及答案

    习题来源于网络,sql语句是自己的答案,部分有参考。欢迎指正及探讨。 测试用数据表 1. 学生表 Student ...

  • SQL 练习

    原文链接网上找来SQL50道练习题,逐个练习,做一记录。 1.学生表student(sid,sname,sage,...

  • 学习笔记|SQL50题-图解运算过程01

    学习SQL的时候,总是被代码绕晕,遇到SQL提升瓶颈,后来看到UML图后,发现可以用基于类图的方式展示运算过程,思...

  • SQL50道练习题部分题的多种写法(MYSQL)

    12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息 写法一: 写法二: 14、查询没学过"张三"老师...

  • 周末作业题

    第1题 第2题 第3题 第4题 第5题 第6题 第7题 第8题 第9题 第10题 第11题 第12题 第13题 第...

网友评论

      本文标题:sql50题

      本文链接:https://www.haomeiwen.com/subject/jvmqjltx.html