MySQL小白入门 46条sql语句练习(学生表 教师表 课程表 分数表)

4TabforMySQL

    • 1.表说明
    • 2.创建表
    • 3.插入数据
    • 4.查询语句(46条)

1.表说明

– 数据库版本:mysql8
– 数据表说明:student(学生表)、teacher(教师表)、course(课程表)、sc(分数表)
--:此注释格试适用Navicat
– 参考原文(Oracle版本)链接:
https://blog.csdn.net/weixin_30298497/article/details/101609611
– 目的:鉴于网上MySQL全方位练习(学生表 教师表 课程表 分数表)版本多为老版本(oracle/hive),或者不为46条的短版本,于是我基于mysql 的8.0版本重写一遍,并且部分语句添加了自己的分解步骤, 比较适合 MySQL 小白入门,不涉及调优,如果您已经十分清楚SQL语句的执行顺序等,请忽略本帖,如不嫌弃,可评论对应执行语句上的调优方法,谢谢!!

2.创建表

-- 学生表
create table student(  
   sno varchar(10) primary key,  
   sname varchar(20),  
   sage int(2),  
   ssex varchar(5)  
);  

-- 教师表
create table teacher(  
   tno varchar(10) primary key,  
   tname varchar(20)  
); 

-- 课程表
create table course(  
   cno varchar(10),  
   cname varchar(20),  
   tno varchar(20),  
   constraint pk_course primary key (cno,tno)  
); 

-- 分数表
create table sc(  
   sno varchar(10),  
   cno varchar(10),  
   score float(4,2),  
   constraint pk_sc primary key (sno,cno)  
);  

3.插入数据

/*******初始化学生表的数据******/  
insert into student values ('s001','张三',23,'男');  
insert into student values ('s002','李四',23,'男');  
insert into student values ('s003','吴鹏',25,'男');  
insert into student values ('s004','琴沁',20,'女');  
insert into student values ('s005','王丽',20,'女');  
insert into student values ('s006','李波',21,'男');  
insert into student values ('s007','刘玉',21,'男');  
insert into student values ('s008','萧蓉',21,'女');  
insert into student values ('s009','陈萧晓',23,'女');  
insert into student values ('s010','陈美',22,'女');  
commit;  
/******************初始化教师表***********************/  
insert into teacher values ('t001', '刘阳');  
insert into teacher values ('t002', '谌燕');  
insert into teacher values ('t003', '胡明星');  
commit;  
/***************初始化课程表****************************/  
insert into course values ('c001','J2SE','t002');  
insert into course values ('c002','Java Web','t002');  
insert into course values ('c003','SSH','t001');  
insert into course values ('c004','Oracle','t001');  
insert into course values ('c005','SQL SERVER 2005','t003');  
insert into course values ('c006','C#','t003');  
insert into course values ('c007','JavaScript','t002');  
insert into course values ('c008','DIV+CSS','t001');  
insert into course values ('c009','PHP','t003');  
insert into course values ('c010','EJB3.0','t002');  
commit;  
/***************初始化成绩表***********************/  
insert into sc values ('s001','c001',78.9);  
insert into sc values ('s002','c001',80.9);  
insert into sc values ('s003','c001',81.9);  
insert into sc values ('s004','c001',60.9);  
insert into sc values ('s001','c002',82.9);  
insert into sc values ('s002','c002',72.9);  
insert into sc values ('s003','c002',81.9);  
insert into sc values ('s001','c003','59');  
commit;  

4.查询语句(46条)

–1、查询“c001”课程比“c002”课程成绩高的所有学生的学号;

SELECT a.sno from 
	(SELECT * from sc where cno="c001") a;
SELECT  a.* from sc a,
	sc b
where a.cno = 'c001'and b.cno='c002' and a.score>b.score AND a.sno=b.sno;
SELECT a.sno from 
	(SELECT * from sc where cno="c001") a,
	(SELECT * from sc  where cno="c002") b
	where a.score>b.score and a.sno=b.sno ;

– 2、查询平均成绩大于70 分的同学的学号和平均成绩

SELECT sno,avg(score) from sc GROUP BY sno HAVING AVG(score)>70;

– 3、查询所有同学的学号、姓名、选课数、总成绩;

SELECT sno,COU	NT(cno),sum(score) FROM sc GROUP BY sno;
select a.*,st.sname from student st,
	(SELECT sno,COUNT(cno),sum(score) FROM sc GROUP BY sno) a 
WHERE st.sno=a.sno; 

– 4、查询姓“刘”的老师的个数;

SELECT COUNT(tname) from teacher WHERE tname LIKE "刘%";

– 5、查询没学过“谌燕”老师课的同学的学号、姓名;

-- 查询“谌燕”老师课
SELECT DISTINCT t.tno, t.tname,c.cno,c.cname from teacher t,
	course c where t.tno=c.tno and t.tname="谌燕" ;

-- 学过“谌燕”老师课的同学的学号
SELECT DISTINCT s.sno from sc s,
	  (
		SELECT DISTINCT t.tno, t.tname,c.cno,c.cname FROM teacher t,
		course c where t.tno=c.tno and t.tname="谌燕"
		) a
		WHERE s.cno = a.cno;
-- 完整语句
SELECT st.sno,st.sname from student st where st.sno NOT in(
		SELECT DISTINCT s.sno from sc s,
	  (
		SELECT DISTINCT t.tno, t.tname,c.cno,c.cname FROM teacher t,
		course c where t.tno=c.tno and t.tname="谌燕"
		) a
		WHERE s.cno = a.cno);

-- 推荐写法
select st.* from student st where st.sno not in
	(
 		select distinct sno from sc s 
			 join course c on s.cno=c.cno
				 join teacher t on c.tno=t.tno 
				where t.tname='谌燕'
);

– 6、查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名;

SELECT a.sno,st.sname FROM sc a 
	JOIN sc b on a.sno=b.sno 
		JOIN student st  on a.sno= st.sno
	WHERE a.cno='c001' AND b.cno ="c002";

– 7、查询学过“谌燕”老师所教的所有课的同学的学号、姓名;

SELECT DISTINCT st.sno,st.sname FROM student st 
	JOIN sc  a on st.sno = a.sno
		JOIN course b on a.cno=b.cno
			JOIN teacher c on b.tno=c.tno
			where c.tname="谌燕";

– 8、查询课程编号“c002”的成绩比课程编号“c001”课程低的所有同学的学号、姓名;

SELECT DISTINCT st.sno,st.sname FROM student st 
	JOIN sc a on st.sno=a.sno
		JOIN sc b ON a.sno=b.sno
	where a.cno="c002"AND b.cno="c001" and a.score

– 9、查询所有课程成绩小于60 分的同学的学号、姓名;

select st.*,s.score from student st
	 join sc s on st.sno=s.sno
		 join course c on s.cno=c.cno
		 where s.score <60;
SELECT DISTINCT st.sno,st.sname FROM student st 
   JOIN sc a on st.sno=a.sno
   where a.score<60;

– 10、查询没有学全所有课的同学的学号、姓名;

– 都没学完所有课程

 select stu.sno,stu.sname,count(sc.cno) from student stu
	 join sc on stu.sno=sc.sno
	 group by stu.sno having count(sc.cno)<(select count(distinct cno)from course);
select stu.sno,stu.sname,count(sc.cno) from student stu
    left join sc on stu.sno=sc.sno
	group by stu.sno having count(sc.cno)<(select count(distinct cno)from course);

– 11、查询至少有一门课与学号为“s001”的同学所学相同的同学的学号和姓名;

SELECT sno,sname FROM student 
	WHERE sno in (
		SELECT DISTINCT sno FROM sc 
		where cno IN ( 
				SELECT cno FROM sc WHERE sno="s001") and sno <>"s001");
select st.* from student st,
	(select distinct a.sno from
		(select * from sc) a,
  			(select * from sc where sc.sno='s001') b
 		 where a.cno=b.cno
	 ) h
 where st.sno=h.sno and st.sno<>'s001';

– 12、查询至少学过学号为“s001”同学所有一门课的其他同学学号和姓名
– 其实查询同上

 select * from sc
	 left join student st on st.sno=sc.sno
 where sc.sno<>'s001' 
  and sc.cno in (select cno from sc where sno='s001');

– 13、把“SC”表中“谌燕”老师教的课的成绩都更改为此课程的平均成绩;

-- 查询课程成绩
SELECT s.cno,s.score FROM sc s 
JOIN course c on s.cno=c.cno
JOIN teacher t on c.tno=t.tno 
and t.tname="谌燕" ;
-- 么有成功更改成绩版本
UPDATE sc c SET score=
(
	SELECT AVG(c.score) FROM course a,teacher b
	WHERE a.tno=b.tno AND b.tname="谌燕" AND a.cno = c.cno
	GROUP BY c.cno
)
WHERE cno in 
(
	SELECT cno FROM course a,teacher b WHERE a.tno = b.tno and b.tname = "谌燕"
);

– 14、查询和“s001”号的同学学习的课程完全相同的其他同学学号和姓名;

-- 网络资料为oracle版
select* from sc where sno<>'s002'
minus
( select* from sc minus select * from sc where sno='s001')
-- mysql版
SELECT DISTINCT cno,sno,count(*) from sc  
GROUP BY sno
HAVING COUNT(*) = (SELECT count(cno)  FROM sc WHERE sno="s002") 
and cno in (SELECT cno  FROM sc WHERE sno="s002") 
and sno<>"s002" ;

– 15、删除学习“谌燕”老师课的SC 表记录;

 
DELETE FROM sc WHERE cno IN
(
SELECT cno FROM course c 
	JOIN teacher t on c.tno=t.tno and t.tname ="谌燕"
);
COMMIT;

SELECT * FROM sc;

-- 后边有用到
insert into sc values ('s001','c001',78.9);  
insert into sc values ('s002','c001',80.9);  
insert into sc values ('s003','c001',81.9);  
insert into sc values ('s004','c001',60.9);  
insert into sc values ('s001','c002',82.9);  
insert into sc values ('s002','c002',72.9);  
insert into sc values ('s003','c002',81.9);  
insert into sc values ('s001','c003','59');  
commit; 

– 16.向SC 表中插入一些记录,这些记录要求符合以下条件:没有上过编号“c002”课程的同学学号、“c002”号课的平均成绩;

SELECT AVG(score) from sc WHERE cno="c002";

-- 执行顺序 explain 
INSERT into sc (sno,cno,score) 
	SELECT DISTINCT st.sno,sc.cno,(SELECT AVG(score) from sc WHERE cno="c002") FROM 
	student st,sc WHERE not EXISTS (SELECT * from sc WHERE cno="c002" and sc.sno=st.sno) and sc.cno="c002";

SELECT * FROM sc;

– 17、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分

SELECT cno ,MAX(score),MIN(score) from sc GROUP BY cno;

– 18、按各科平均成绩从低到高和及格率的百分数从高到低顺序

SELECT cno,AVG(score),SUM(CASE 	WHEN score>=60 THEN	1	ELSE 0 END)/COUNT(*) as passratio FROM sc
GROUP BY cno ORDER BY AVG(score),passratio DESC;

– 19、查询不同老师所教不同课程平均分从高到低显示

select t.tno,t.tname,c.cno,c.cname,c.cno,avg(score) from sc , course c,teacher t
where sc.cno=c.cno and c.tno=t.tno
group by c.cno
order by avg(score) desc;

– 20、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]

select sc.cno,c.cname,
sum(case  when score between 85 and 100 then 1 else 0 end) AS "[100-85]",
sum(case  when score between 70 and 85 then 1 else 0 end) AS "[85-70]",
sum(case  when score between 60 and 70 then 1 else 0 end) AS "[70-60]",
sum(case  when score <60 then 1 else 0 end) AS "[<60]"
from sc, course c
where  sc.cno=c.cno
group by sc.cno ,c.cname;

– 21、查询各科成绩前三名的记录:(不考虑成绩并列情况)

-- 单纯使用group by 只能按cno 将score进行聚合, 是无法将3个筛选出来的, 只能满足这些需求, 例如: 每个课程的总分 sum(score), 最高分 max(score), 考试学生数count(score) 等
SELECT sno,cno,max(score) FROM sc GROUP BY cno;

– 先排序
SELECT sno,cno,score FROM sc ORDER BY cno,score DESC;
– 再分组

SELECT s.sno,s.cno,SUBSTRING_INDEX(GROUP_CONCAT(s.score ORDER BY score DESC ),',',3) top3 from 
(
	SELECT sno,cno,score FROM sc  ORDER BY cno,score DESC
) s GROUP BY s.cno;

– 22、查询每门课程被选修的学生数
SELECT cno,COUNT(score) FROM sc GROUP BY cno;
– 23、查询出只选修了一门课程的全部学生的学号和姓名

SELECT sc.sno,s.sname,sc.cno,COUNT(score) FROM sc,student s 
WHERE sc.sno=s.sno
GROUP BY sno HAVING COUNT(score)=1;

– 24、查询男生、女生人数
SELECT ssex,COUNT(*) FROM student GROUP BY ssex;
– 25、查询姓“张”的学生名单
SELECT * FROM student WHERE sname LIke'张%';
– 26、查询同名同性学生名单,并统计同名人数
SELECT sname,COUNT(*) FROM student GROUP BY sname HAVING COUNT(*)>1;
– 27、2000 年出生的学生名单(注:Student 表中Sage 列的类型是int)
SELECT * from student WHERE DATE_FORMAT(SYSDATE(),"%Y")-sage=2000;
– 28、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
SELECT cno,AVG(score) FROM sc GROUP BY cno ORDER BY score ,cno DESC;
– 29、查询平均成绩大于75 的所有学生的学号、姓名和平均成绩

SELECT  sc.sno,s.sname,AVG(score) FROM sc,student s WHERE sc.sno=s.sno GROUP BY sc.sno,s.sname HAVING AVG(score)>75;

– 30、查询课程名称为“SSH”,且分数低于60 的学生姓名和分数

SELECT s.sname,sc.score,c.cname FROM sc 
JOIN student s ON sc.sno=s.sno
JOIN course c ON sc.cno=c.cno where c.cname="SSH" AND sc.score<60;
select sname,c.cname,score from student st,sc,course c
where st.sno=sc.sno and sc.cno=c.cno and c.cname='SSH' and sc.score<60;

– 31、查询所有学生的选课情况;

select s.sno,s.sname,c.cname FROM student s
JOIN sc on s.sno=sc.sno
JOIN course c on sc.cno = c.cno;
select s.sno,s.sname,c.cname from student s, sc, course c where s.sno=sc.sno and sc.cno=c.cno;

– 32、查询任何一门课程成绩在70 分以上的姓名、课程名称和分数;

SELECT sc.sno,s.sname FROM sc,student s WHERE sc.sno=s.sno AND score>70 GROUP BY sc.sno;
SELECT sc.sno,s.sname FROM sc,student s WHERE sc.sno=s.sno AND score>70 GROUP BY sc.sno;

SELECT s.sname,c.cname,sc.score FROM student s,course c, sc WHERE s.sno=sc.sno AND sc.cno=c.cno AND score>70;

– 33、查询不及格的课程,并按课程号从大到小排列

SELECT * from sc WHERE score<60 ORDER BY cno DESC;
– 34、查询课程编号为c001 且课程成绩在80 分以上的学生的学号和姓名;

SELECT s.sno,s.sname,sc.score FROM student s,sc WHERE s.sno=sc.sno AND sc.cno="c001" AND sc.score>80;

– 35、求选了课程的学生人数
SELECT COUNT(DISTINCT sno) FROM sc;

– 36、查询选修“谌燕”老师所授课程的学生中,成绩最高的学生姓名及其成绩

SELECT s.sname,c.cname,sc.score FROM student s,sc,course c,teacher t
where s.sno=sc.sno AND sc.cno=c.cno AND c.tno=t.tno 
AND t.tname="谌燕" ORDER BY sc.score DESC LIMIT 1;
select st.sname,c.cname,score from student st,sc ,course c,teacher t
where
st.sno=sc.sno and sc.cno=c.cno and c.tno=t.tno and t.tname='谌燕' and sc.score=
(select max(score)from sc where sc.cno=c.cno);

– 37、查询各个课程及相应的选修人数

SELECT c.cno,c.cname,COUNT(DISTINCT sno) FROM course c,sc WHERE c.cno = sc.cno GROUP BY c.cno;
select cno,count(sno) from sc group by cno;

– 38、查询不同课程成绩相同的学生的学号、课程号、学生成绩

select a.* from sc a ,sc b where a.score=b.score and a.cno<>b.cno;
– 不同学号
SELECT DISTINCT s1.* from sc s1,sc s2 WHERE s1.score=s2.score AND s1.sno<>s2.sno;
– 39、查询每门功课成绩最好的前两名(复习语句)

SELECT s.sno,s.cno,SUBSTRING_INDEX(GROUP_CONCAT(s.score ORDER BY score DESC ),',',2) top2 from 
(
	SELECT sno,cno,score FROM sc  ORDER BY cno,score DESC
) s GROUP BY s.cno;

– 40、统计每门课程的学生选修人数(超过3 人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

SELECT cno,COUNT(cno) FROM sc GROUP BY cno HAVING COUNT(cno)>3 ORDER BY COUNT(cno) desc,cno;

– 41、检索至少选修两门课程的学生学号
SELECT sno from sc GROUP BY sno HAVING COUNT(cno)>1;

– 42、查询全部学生都选修的课程的课程号和课程名
– 查询全部学生都选修的课程的课程号和课程名

select distinct(c.cno),c.cname from course c ,sc where sc.cno=c.cno;
– 查询全部学生都选修的课程的课程号和课程名
tip:为了检验第26句,加了一名学生

SELECT cno,COUNT(sc.sno) from sc,student s WHERE sc.sno=s.sno GROUP BY cno HAVING COUNT(sc.sno)=(SELECT COUNT(*) from student)-1;

– 43、查询没学过“谌燕”老师讲授的任一门课程的学生姓名(复习第5句)

select st.* from student st where st.sno not in
	(
 		select distinct sno from sc s 
			 join course c on s.cno=c.cno
				 join teacher t on c.tno=t.tno 
				where t.tname='谌燕'
);
select st.sname from student st where st.sno not in
(select distinct sc.sno from sc,course c,teacher t where sc.cno=c.cno and c.tno=t.tno and t.tname='谌燕');

– 44、查询两门以上不及格课程的同学的学号及其平均成绩

select sno,avg(score)from sc
where sno in
(
 select sno from sc where sc.score<80
 group by sno having count(sno)>1
) group by sno;

– 语句执行顺序理解有问题版本:
SELECT sno,AVG(score) from sc where score<80 GROUP BY sno HAVING COUNT(cno)>1;
– 45、检索“c001”课程分数小于80,按分数降序排列的同学学号

SELECT sno,cno,score FROM sc WHERE cno="c001" AND score<80 ORDER BY score DESC;

– 46、删除“s002”同学的“c001”课程的成绩
DELETE FROM sc WHERE sno="s002" AND cno = "c001";

相信您敲完了这些sql语句之后有了新的问题并迫不及待的想去查询相关资料了

你可能感兴趣的:(MySQL,mysql,数据库,SQL,SQL语句,SQL初学者,SQL语句提升)