【MySQL】- 05 sql 语句练习题

这篇具有很好参考价值的文章主要介绍了【MySQL】- 05 sql 语句练习题。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


查询结果就不放了,语句是否成功运行,结果是否正确都容易自行判断

一 测试数据的准备

–1. 学生表
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_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"课程成绩高的学生的信息及课程分数

写法1:

select a.* ,b.s_score as 01_score,c.s_score as 02_score from 
student a 
	join score b on a.s_id=b.s_id and b.c_id='01'
	left join score c on a.s_id=c.s_id and c.c_id='02' or c.c_id = NULL where b.s_score>c.s_score

写法2:

	select a.*,b.s_score as 01_score,c.s_score as 02_score from student 		  a,score b,score c 
			where a.s_id=b.s_id 
			and a.s_id=c.s_id 
			and b.c_id='01' 
			and c.c_id='02' 
			and b.s_score>c.s_score

查询结果如下:

【MySQL】- 05 sql 语句练习题

2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
select a.* ,b.s_score as 01_score,c.s_score as 02_score from 
	student a left join score b on a.s_id=b.s_id and b.c_id='01' or b.c_id=NULL 
	 join score c on a.s_id=c.s_id and c.c_id='02' where b.s_score<c.s_score
3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select b.s_id,b.s_name,ROUND(AVG(a.s_score),2) as avg_score from 
	student b 
	join score a on b.s_id = a.s_id
	GROUP BY b.s_id,b.s_name HAVING avg_score >=60;
4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩

– (包括有成绩的和无成绩的)

select b.s_id,b.s_name,ROUND(AVG(a.s_score),2) as avg_score from 
	student b 
	left join score a on b.s_id = a.s_id
	GROUP BY b.s_id,b.s_name HAVING avg_score <60
	union
select a.s_id,a.s_name,0 as avg_score from 
	student a 
	where a.s_id not in (
				select distinct s_id from score);
5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select a.s_id,a.s_name,count(b.c_id) as sum_course,sum(b.s_score) as sum_score from 
	student a 
	left join score b on a.s_id=b.s_id
	GROUP BY a.s_id,a.s_name;

查询结果如下所示:

6、查询"李"姓老师的数量
select count(t_id) from teacher where t_name like '李%';

查询结果如下所示:

7、查询学过"张三"老师授课的同学的信息
select a.* from student a 
join score b on a.s_id=b.s_id where b.c_id in(
select c_id from course where t_id =(
select t_id from teacher where t_name = '张三'));
8、查询没学过"张三"老师授课的同学的信息
select * from student c 
where c.s_id not in(
select a.s_id from student a join score b on a.s_id=b.s_id where b.c_id in(
select a.c_id from course a join teacher b on a.t_id = b.t_id where t_name ='张三'));
9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

方法1:

select a.* from student a,score b,score c 
where a.s_id = b.s_id  and a.s_id = c.s_id and b.c_id='01' and c.c_id='02';

方法2:

select a.* from student a 
where a.s_id in (select s_id from score where c_id='01' ) and a.s_id  in(select s_id from score where c_id='02');
10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
select a.* from student a 
where a.s_id in (select s_id from score where c_id='01' ) and a.s_id not in(select s_id from score where c_id='02');
11、查询没有学全所有课程的同学的信息

方法1:

select s.* from student s 
left join Score s1 on s1.s_id=s.s_id
group by s.s_id having count(s1.c_id)<(select count(*) from course)

方法2:

select *
from student
where s_id not in(
select s_id from score t1  
group by s_id having count(*) =(select count(distinct c_id)  from course)) 
12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
SELECT * 
	from student 
		WHERE s_id in(
		SELECT DISTINCT s_id 
			FROM score 
				where c_id in
					(SELECT c_id 
						FROM score 
							WHERE s_id ='01') 
								and s_id !='01'
)
13、查询和"01"号的同学学习的课程完全相同的其他同学的信息

方法1

SELECT
 student.*
FROM
 student
WHERE
 s_id IN (SELECT s_id FROM score GROUP BY s_id HAVING COUNT(s_id) = (
    #下面的语句是找到'01'同学学习的课程数
    SELECT COUNT(c_id) FROM score WHERE s_id = '01'
   )
 )
AND s_id NOT IN (
 #下面的语句是找到学过‘01’同学没学过的课程,有哪些同学。并排除他们
 SELECT s_id FROM score
 WHERE c_id IN(
   #下面的语句是找到‘01’同学没学过的课程
   SELECT DISTINCT c_id FROM score
   WHERE c_id NOT IN (
     #下面的语句是找出‘01’同学学习的课程
     SELECT c_id FROM score WHERE s_id = '01'
    )
  ) GROUP BY s_id
) #下面的条件是排除01同学
AND s_id NOT IN ('01')

方法2:

SELECT
 t3.*
FROM
 (
  SELECT
   s_id,
   group_concat(c_id ORDER BY c_id) group1
  FROM
   score
  WHERE
   s_id &lt;> '01'
  GROUP BY
   s_id
 ) t1
INNER JOIN (
 SELECT
  group_concat(c_id ORDER BY c_id) group2
 FROM
  score
 WHERE
  s_id = '01'
 GROUP BY
  s_id
) t2 ON t1.group1 = t2.group2
INNER JOIN student t3 ON t1.s_id = t3.s_id
14、查询没学过"张三"老师讲授的任一门课程的学生姓名
select a.s_name from student a where a.s_id not in (
select s_id from score where c_id = 
(select c_id from course where t_id =(
select t_id from teacher where t_name = '张三')));
15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select a.s_id,a.s_name,ROUND(AVG(b.s_score)) from 
student a 
left join score b on a.s_id = b.s_id
where a.s_id in(
select s_id from score where s_score<60 GROUP BY  s_id having count(1)>=2)
GROUP BY a.s_id,a.s_name;
16、检索"01"课程分数小于60,按分数降序排列的学生信息
select a.*,b.c_id,b.s_score from 
student a,score b 
where a.s_id = b.s_id and b.c_id='01' and b.s_score<60 ORDER BY b.s_score DESC;
17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

方法1:

select a.s_id,(select s_score from score where s_id=a.s_id and c_id='01') as 语文,
(select s_score from score where s_id=a.s_id and c_id='02') as 数学,
(select s_score from score where s_id=a.s_id and c_id='03') as 英语,
round(avg(s_score),2) as 平均分 from score a  GROUP BY a.s_id ORDER BY 平均分 DESC;

方法2:

SELECT a.s_id,MAX(CASE a.c_id WHEN '01' THEN a.s_score END ) 语文, 
MAX(CASE a.c_id WHEN '02' THEN a.s_score END ) 数学, 
MAX(CASE a.c_id WHEN '03' THEN a.s_score END ) 英语, 
avg(a.s_score),b.s_name FROM Score a JOIN Student b ON a.s_id=b.s_id GROUP BY a.s_id ORDER BY 5 DESC
18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率

–及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

select a.c_id,b.c_name,MAX(s_score),MIN(s_score),ROUND(AVG(s_score),2),
	ROUND(100*(SUM(case when a.s_score>=60 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 及格率,
	ROUND(100*(SUM(case when a.s_score>=70 and a.s_score<=80 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 中等率,
	ROUND(100*(SUM(case when a.s_score>=80 and a.s_score<=90 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 优良率,
	ROUND(100*(SUM(case when a.s_score>=90 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 优秀率
	from score a left join course b on a.c_id = b.c_id GROUP BY a.c_id,b.c_name
19、按各科成绩进行排序,并显示排名

– mysql没有rank函数

	select a.s_id,a.c_id,
        @i:=@i +1 as i,
        @k:=(case when @score=a.s_score then @k else @i end) as rank,
        @score:=a.s_score as score
    from (
        select s_id,c_id,s_score from score GROUP BY s_id,c_id,s_score ORDER BY s_score DESC
)a,(select @k:=0,@i:=0,@score:=0) s

写法2文章来源地址https://www.toymoban.com/news/detail-477695.html

(select * from (select 
t1.c_id,
t1.s_score,
(select count(distinct t2.s_score) from score t2 where t2.s_score>=t1.s_score and t2.c_id='01') rank
FROM score t1 where t1.c_id='01'
order by t1.s_score desc) t1)
union
(select * from (select 
t1.c_id,
t1.s_score,
(select count(distinct t2.s_score) from score t2 where t2.s_score>=t1.s_score and t2.c_id='02') rank
FROM score t1 where t1.c_id='02'
order by t1.s_score desc) t2)
union
(select * from (select 
t1.c_id,
t1.s_score,
(select count(distinct t2.s_score) from score t2 where t2.s_score>=t1.s_score and t2.c_id='03') rank
FROM score t1 where t1.c_id='03'
order by t1.s_score desc) t3)

到了这里,关于【MySQL】- 05 sql 语句练习题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 四十道MYSQL复杂查询练习题(难度较低)

    MYSQL简单查询练习题,想做复杂一点的连接如下: https://blog.csdn.net/weixin_50843918/article/details/123086590?spm=1001.2014.3001.5501 代码直接按顺序复制就可以 dept表: emp表: salgrade表: 雇员表: 记录了一个雇员的基本信息 EMP(雇员表) NO -----------字段 ---------------类型-----------------描述

    2024年02月02日
    浏览(42)
  • 六、MySQL---综合练习题(单表、多表、分组函数以及等级查询)

    数据库:dept_emp 1. dept表 :deptno’部门编号’、dname’部门名称’、loc’地理位置’; 2. emp表 :empno ‘员工编号’、ename ‘员工名字’、job ‘职位’、mgr ‘上司’、hiredate ‘入职时间’、sal ‘基本工资’,comm ‘补贴’,deptno ‘所属部门编号’; 3. salgrade :grade ‘工资等级’

    2024年01月21日
    浏览(50)
  • C语言循环语句进阶练习题

    第1关:求出分数序列前n项之和 100 任务要求 参考答案 评论98 任务描述 相关知识 scanf 分数序列 编程要求 测试说明 任务描述 本关需要你求出分数序列前 n 项之和。 相关知识 你需要使用到 scanf 函数和循环语句来完成本关任务。 scanf 函数名: scanf 功 能:执行格式化输入 。 用

    2024年02月05日
    浏览(48)
  • SQL经典练习题

    1.学生表 Student(SId,Sname,Sage,Ssex) SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别 2.课程表 Course(CId,Cname,TId) CId 课程编号,Cname 课程名称,TId 教师编号 3.教师表 Teacher(TId,Tname) TId 教师编号,Tname 教师姓名 4.成绩表 SC(SId,CId,score) SId 学生编号,CId 课程编号,score 分数 学生表 Studen

    2024年01月20日
    浏览(78)
  • sql练习题

    DQL练习1-学生表     创建如下学生表      create table student(     id int,     name varchar(20),     gender varchar(20),     chinese int,     math int,     english int     );                   insert into student values      (1,\\\'张明\\\',\\\'男\\\',89,78,90),     (2,\\\'李进\\\',\\\'男\\\',67,53,95),     (3,\\\'王五

    2024年02月16日
    浏览(46)
  • sql函数练习题

    1. 计算用户8月每天的练题数量 题目:现在运营想要计算出 2021年8月每天用户练习题目的数量 ,请取出相应数据。 示例:question_practice_detail id device_id question_id result date 1 2138 111 wrong 2021-05-03 2 3214 112 wrong 2021-05-09 3 3214 113 wrong 2021-06-15 4 6543 111 right 2021-08-13 5 2315 115 right 2021-08-1

    2024年02月07日
    浏览(44)
  • 50道SQL练习题

    https://www.bilibili.com/video/BV14h411R7F6/?p=4vd_source=76a21f3936db28f5e63b70544272d65e https://www.cnblogs.com/Diyo/p/11424844.html https://zhuanlan.zhihu.com/p/113173133 方法1:自连接 思路:先使用自连接score表找到01\\\"课程比\\\"02\\\"课程成绩高的的学生id,然后关联学生表获取学生信息 方法二: 长形数据变成宽型数

    2024年02月09日
    浏览(59)
  • 数据库多表查询练习题

    二、多表查询 1. 创建 student 和 score 表 CREATE TABLE student ( id INT ( 10 ) NOT NULL UNIQUE PRIMARY KEY , name VARCHAR ( 20 ) NOT NULL , sex VARCHAR ( 4 ) , birth YEAR , department VARCHAR ( 20 ) , address VARCHAR ( 50 ) ); 创建 score 表。 SQL 代码如下: CREATE TABLE score ( id INT ( 10 ) NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT , stu_

    2024年01月17日
    浏览(62)
  • sql-50练习题6-10

    sql真的非常灵活,一个题目可能有很多很多种解法,我记录的只是我自己的一个解题思路,如果大家有更好的不同解法欢迎在评论区一起探讨 ps:有些题可能解法会重复,比如求大于,我们下一个题可能是求小于,大家如果第一遍没有做出来,看了我写的之后有了思路,可以

    2024年02月08日
    浏览(45)
  • sql-50练习题11-15

    sql真的非常灵活,一个题目可能有很多很多种解法,我记录的只是我自己的一个解题思路,如果大家有更好的不同解法欢迎在评论区一起探讨 ps:有些题可能解法会重复,比如求大于,我们下一个题可能是求小于,大家如果第一遍没有做出来,看了我写的之后有了思路,可以

    2024年02月06日
    浏览(41)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包