mysql笔试题写sql语句_sql语句⾯试50题(Mysql版附解析)本⼈最近在⾃学sql,从开始学到⾃⼰写完本练习50题⼤概花了12天的时间。
安卓开发主流框架学习路径:《sql基础教程》第1遍(3天)→知乎中的sql⽹课+leetcode刷题(4天)→⽜客⽹刷题(2天)→《sql基础教程》第2遍(1晚上)→sql ⾯试练习50题(3天)
⽂中的代码都是按⾃⼰思路写的,⼤家在做题的时候最好也按照⾃⼰的思路来写,实在想不通的再参考我的思路,毕竟每个⼈的编写思路不⼀样,唯有⾃⼰思考过才会印象深刻。有问题的⼩伙伴可以评论留⾔,⼀起探讨~
表名和字段
Student(s_id,s_name,s_birth,s_sex) --学⽣编号,学⽣姓名, 出⽣年⽉,学⽣性别
–2.课程表
Course(c_id,c_name,t_id) – --课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) --教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) --学⽣编号,课程编号,分数
测试数据
--建表
--学⽣表
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_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' , '⼥'); ins
ert 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('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);
mysql面试题笔试insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);
练习题⽬及sql语句
1、查询"01"课程⽐"02"课程成绩⾼的学⽣的信息及课程分数
-- [分析] 筛选出课程号01这门课的成绩⽐02这门课成绩⾼的学⽣,输出这些学⽣的信息及课程分(表:Student,Score)——表联结
select a.*, b.s_score as 01_score, c.s_score as 02_score
from Student as a inner join Score as b on a.s_id = b.s_id and b.c_id = '01'
inner join Score as c on a.s_id = c.s_id and c.c_id = '02'
where b.s_score > c.s_score;
2、查询"01"课程⽐"02"课程成绩低的学⽣的信息及课程分数
-- [分析] 与上体的思路⼀样,换个符合⽅向就可
select a.*, b.s_score as 01_score, c.s_score as 02_score
from student as a inner join score as b on a.s_id = b.s_id and b.c_id = '01'
inner join score as c on a.s_id = c.s_id and c.c_id = '02'
where b.s_score < c.s_score;
3、查询平均成绩⼤于等于60分的同学的学⽣编号和学⽣姓名和平均成绩
-- [分析] 要求平均成绩,就需要⽤group by对学⽣分组,然后利⽤聚合函数avg求出平均成绩,由于where语句中不能包含聚合函数,故再利⽤having语句和60分⽐较。
-- 输出的结果学⽣编号和学⽣姓名在student表中,成绩在score表中,故需要⽤到内联结。
select a.s_id, a.s_name, avg(s_score) as avg_score
from student as a inner join score as b on a.s_id = b.s_id
group by a.s_id,a.s_name
having avg(b.s_score) >= 60;
-- [分析] 和3题思路⼀样,改个符号⽅向即可
select a.s_id, a.s_name, avg(s_score) as avg_score
from student as a inner join score as b on a.s_id = b.s_id
group by b.s_id
having avg(b.s_score) < 60;
5、查询所有同学的学⽣编号、学⽣姓名、选课总数、所有课程的总成绩
-- [分析]学⽣编号、学⽣姓名在表student中;选课数可通过在表score中通过group by 学⽣编号后利⽤count(c_id)算出;所有课程的总成绩则⽤sum可以算出。
-- 因为要⽤到表student和表score,故需要对表进⾏联结。
select a.s_id, a.s_name, count(b.c_id), sum(b.s_score)
from student as a inner join score as b on a.s_id = b.s_id
group by a.s_id,a.s_name;
6、查询"李"姓⽼师的数量
-- [分析] 这⾥⽤到知识点字符串模糊查询,如:where 姓名 like '猴%' 即可查到猴什么什么。
-- 关于字符串模糊查询的知识点可查看我的另⼀篇⽂章《SQL学习笔记——汇总分析》
select count(t_name) as number
from teacher
where t_name like '李%';
7、查询学过"张三"⽼师授课的同学的信息
-- [分析] ⽼师的信息在表teacher中,可通过teacher.t_id与表course联结,
-- 再通过course.c_id与表score联结,
-
- 再再通过score.s_id与表student联结,查出上过张三⽼师课的同学的信息。
select s.*
from teacher as a inner join course as b on a.t_id = b.t_id
inner join score as c on b.c_id = c.c_id
inner join student as s on c.s_id = s.s_id
where a.t_name = '张三';
8、查询没学过"张三"⽼师授课的同学的信息
-- [分析]这⾥不能直接将“=”替换成“<>”,因为不等于张三时,还有别的⽼师。所以要⽤到⼦查询来解决。
select *
from student
where s_id not in
(select s.s_id
inner join score as c on b.c_id = c.c_id
inner join student as s on c.s_id = s.s_id
where a.t_name = '张三');
直播:葡萄牙vs瑞士比赛9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
-- [分析]由于既要学过01课程的,⼜要学过02课程的,所以共需要联结两次才能达到筛选效果
select *
from student
where s_id in (select a.s_id from student as a inner join score as b on a.s_id = b.s_id and b.c_id = '01'
inner join score as c on b.s_id = c.s_id and c.c_id = '02');
10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
-- [分析] 在上述语句中⽤and联结,注意不能直接将“=”换成“<>”
select *
from student
where s_id in (select a.s_id from student as a inner join score as b on a.s_id = b.s_id and b.c_id = '01')
and s_id not in (select a.s_id from student as a inner join score as c on a.s_id = c.s_id and c.c_id = '02');
11、查询没有学全所有课程的同学的信息
git常用命令技巧-- [分析]没学全所有课程的情况很多,如学0门,学1门,学2门。所以这⾥先筛选出学全所有课程的同学再取反,即没有学全所有课程的同学。
select *
from student
淘宝客返利
where s_id not in (select s_id
from score
group by s_id
having count(c_id) = (select count(distinct c_id) from course));
12、查询⾄少有⼀门课与学号为"01"的同学所学相同的同学的信息 *
-- [分析]⼦查询两次,第⼀次到学号为01的同学所学的课程,第⼆次再到⾄少⼀门课与学号01同学上的课相同的同学信息
select *
from student
where s_id in (select distinct a.s_id from score as a
where a.c_id in(select b.c_id from score as b
where b.s_id = '01')) and s_id <> '01';
-- ⽅法2:内联结+⼀次⼦查询
select distinct a.*

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