MySQL 50题

MySQL 50题

表的介绍

本文中共用到了四张表:学生表、课程表、教师表、成绩表

1.学生表

student(s_id,s_name,s_age,s_sex)
s_id : 学⽣编号、s_name: 学⽣姓名、s_age: 出⽣年⽉、s_sex: 学⽣性别

2.课程表

course(c_id,c_name,t_id)​
c_id: 课程编号、c_name: 课程名称、t_id: 教师编号

3.教师表

teacher(t_id,t_name)
t_id: 教师编号、t_name: 教师姓名

4.成绩表

sc(s_id,c_id,score)
s_id: 学生编号、c_id: 课程编号、score: 分数

创建数据库

本文中我创建了库名为MySQL50的数据库

CREATE DATABASE IF NOT EXISTS MySQL50;

使用该数据库

为了避免创建表时创建在错误的数据库,所以先使用该数据库

USE MySQL50;

创建表

1.学生表

CREATE TABLE student(
s_id INT PRIMARY KEY AUTO_INCREMENT, 
s_name VARCHAR(10),
s_age DATETIME,
s_sex VARCHAR(10)
);

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

2.课程表

CREATE TABLE course(
c_id INT PRIMARY KEY AUTO_INCREMENT,
c_name VARCHAR(10),
t_id INT(10)
);

INSERT INTO course(c_name,t_id) VALUES  
('数学' , 01),
('语文' , 02),
('英语' , 03);

3.教师表

CREATE TABLE teacher(
t_id INT PRIMARY KEY AUTO_INCREMENT,
t_name VARCHAR(10)
);

INSERT INTO teacher(t_name) VALUES
('张三'),
('李四'),
('王五');

4.成绩表

CREATE TABLE sc(
s_id INT(10),
c_id INT(10),
score INT(10)
);

INSERT INTO sc(s_id,c_id,score) VALUES
(01 , 01 , 80),
(01 , 02 , 90),
(01 , 03 , 99),
(02 , 01 , 70),
(02 , 02 , 60),
(02 , 03 , 80),
(03 , 01 , 80),
(03 , 02 , 80),
(03 , 03 , 80),
(04 , 01 , 50),
(04 , 02 , 30),
(04 , 03 , 20),
(05 , 01 , 76),
(05 , 02 , 87),
(06 , 01 , 31),
(06 , 03 , 34),
(07 , 02 , 89),
(07 , 03 , 98);

1.查询课程1比课程2成绩高的学生的信息以及课程1课程2成绩

SELECT a1.s_id,a1.s_name,a1.s_sex,b1.s1 AS "课程1成绩",b1.s2 AS "课程2成绩" FROM student a1, (
SELECT a.s_id AS s_id,a.score AS s1,b.score AS s2 FROM sc a,sc b
WHERE a.c_id=1 AND b.c_id=2 AND a.s_id=b.s_id
)b1  
WHERE s1>s2 AND a1.s_id=b1.s_id;								

2.找出平均分大于60分的学生的学号和平均分

select s_id,avg(score) AS "平均分" from sc group by s_id having avg(score)>60;

3.查询每个学生的课程总数及所有课程的总成绩

SELECT s_id,COUNT(*) AS "课程总数",SUM(score) AS "总分"FROM sc GROUP BY s_id;	

4.查询李姓老师的人数

SELECT COUNT(*)AS "人数" FROM teacher WHERE t_name LIKE "李%";		

5.查询学过张三老师的学生的信息

SELECT a.* FROM student a 
JOIN sc b ON a.s_id=b.s_id
JOIN course c ON b.c_id=c.c_id
JOIN teacher d ON c.t_id=d.t_id AND d.t_name="张三";

6.查询学过即学过课程1有学过课程2的学生信息(姓名、ID)

SELECT c.s_name AS "姓名",c.s_id AS "ID" FROM student AS c,
(SELECT a.s_id AS s_id FROM (SELECT * FROM sc WHERE c_id=1) a ,(SELECT * FROM sc WHERE c_id=2) b 
WHERE a.s_id=b.s_id)d 
WHERE c.s_id=d.s_id;

7.查询学过课程1但没有学过课程2的学生信息(姓名、ID)

SELECT c.s_name AS "姓名",c.s_id  AS "ID"FROM student AS c,
(SELECT  DISTINCT(a.s_id) AS s_id FROM (SELECT * FROM sc WHERE c_id=1)a,
(SELECT * FROM sc WHERE s_id NOT IN (SELECT s_id FROM sc WHERE c_id=2))b
WHERE a.s_id =b.s_id) d
WHERE c.`s_id`=d.s_id;

8.查询没有学全所有课程的学生的信息(姓名、ID)

SELECT a.s_name AS "姓名",b.s_id AS "ID" FROM student AS a,sc AS b 
WHERE a.s_id=b.s_id GROUP BY a.s_id HAVING COUNT(*)<(SELECT COUNT(DISTINCT c_name)AS a FROM course);

9.查询至少有一门课程与学号为1的学生相同的学生信息(姓名、ID)

SELECT  a.s_name AS "姓名",a.s_id AS "ID" FROM student AS a,
(SELECT DISTINCT(s_id) FROM sc WHERE c_id IN(SELECT c_id FROM sc WHERE s_id=1))b WHERE a.s_id=b.s_id;

10.查询与1号同学所修课程数相同的同学学号、课程数

SELECT s_id,COUNT(*) AS "课程数" FROM sc WHERE c_id  IN
(SELECT c_id FROM sc WHERE s_id=1)  
GROUP BY s_id HAVING COUNT(*)=(SELECT COUNT(*) FROM sc WHERE s_id=1);

11.查询没学过张三老师课的学生的学号

SELECT DISTINCT s_id FROM sc WHERE s_id NOT IN
( SELECT s_id FROM sc WHERE c_id=
(SELECT c_id FROM course WHERE t_id=
(SELECT t_id FROM teacher WHERE t_name="张三") ) );

12.查询成绩不及格的课程数大于2的学生的学号和课程数

SELECT s_id,COUNT(*) FROM sc WHERE sc<60 GROUP BY s_id HAVING COUNT(*)>=2;

13.查询课程1小于60分的学生的学号、成绩按降序排序

SELECT s_id,score FROM sc WHERE score<60  AND c_id=1 ORDER BY score DESC;

14.按平均成绩高低显示所有学生所有课程的成绩及平均成绩

SELECT a.*,b.avg FROM  sc AS a ,
(SELECT s_id,AVG(score) AS AVG FROM sc GROUP BY s_id )b 
WHERE a.s_id =b.s_id ORDER BY b.avg DESC;

15.查询各科成绩最高分、最低分和平均分

select c_id,max(score) as "最高分",min(score) as "最低分",avg(score) as "平均分" from sc  group by c_id;

16.按各科成绩进行排序、并显示排名(允许并列、无间隔)

SELECT c_id,s_id,score,rank() over (PARTITION BY c_id ORDER BY score DESC) "排名"FROM sc ;

17.查询学生的总成绩并进行排名

SELECT s_id AS "ID",SUM(score) AS "总分",rank()over(ORDER BY SUM(score) DESC) "排名"  
FROM sc GROUP BY s_id ; 

18.查询不同老师所教不同课程平均分从高到低显示 (显示结果:教师编号、教师姓名、课程名、课程平均分)

SELECT a.c_id,c.t_name,b.c_name,AVG(a.score)AS "平均分" FROM sc a 
JOIN course b ON a.c_id=b.c_id 
JOIN teacher c ON b.t_id=c.t_id
GROUP BY a.c_id ORDER BY AVG(a.score) DESC;

19.查询所有课程的成绩第2名到第3名的学生信息及该课程编号、成绩、排名

SELECT c.*,d.c_id,d.score,d.r AS "排名"
FROM student AS c,(SELECT * FROM 
(SELECT * ,dense_rank() over(PARTITION BY c_id ORDER BY score DESC)r FROM sc)a
WHERE r BETWEEN 2 AND 3)d 
WHERE d.s_id=c.s_id;

20.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比

SELECT e.c_name,a.c_id,a.c1 AS"100-85分人数",
a.c1/6 AS "100-85分所占百分比",b.c2 AS "85-70分人数",
b.c2/6 AS "85-70百分所占比",c.c3 AS "70-60分人数",
c.c3/6 AS "70-60所占百分比",d.c4 AS "60分以下人数",
d.c4/6 AS "60以下所占百分比" 
FROM (SELECT c_id,COUNT(*) c1 FROM sc WHERE score<60 GROUP BY c_id)a
LEFT JOIN (SELECT c_id,COUNT(*) c2 FROM sc WHERE score>= 60 AND score<70 GROUP BY c_id)b ON a.c_id=b.c_id
LEFT JOIN (SELECT c_id,COUNT(*) c3 FROM sc WHERE score>= 70 AND score<85 GROUP BY c_id)c ON a.c_id=c.c_id
LEFT JOIN (SELECT c_id,COUNT(*) c4 FROM sc WHERE score>= 85  GROUP BY c_id)d ON a.c_id=d.c_id
LEFT JOIN course AS e ON a.c_id=e.c_id;

21.查询学生平均成绩及排名(排名有并列、间隔)

SELECT s_id,AVG(score),rank() over(ORDER BY AVG(score) DESC) "排名"FROM sc GROUP BY s_id ;

22.查询各科成绩前三名的记录(排名有并列、间隔)

SELECT s_id,c_id,score,r AS "排名" FROM
(SELECT *,rank() over(PARTITION BY c_id ORDER BY score DESC)r FROM sc) a WHERE r<4;

23.查询每门课程被选修的学生数

SELECT c_id,COUNT(*) AS "人数" FROM sc GROUP BY c_id;

24.查询出只有两门课程的全部学生的学号和姓名

SELECT  a.s_id,a.s_name FROM student AS a,sc b WHERE a.s_id=b.s_id GROUP BY b.s_id HAVING COUNT(b.c_id)=2;

25.查询男生、女生人数

SELECT s_sex,COUNT(*) AS "人数" FROM student GROUP BY s_sex;

26.查询名字中含有"风"字的学生信息

SELECT * FROM student WHERE s_name LIKE"%风%";

27.查询同名同性学生信息、并统计同名人数

建表时没有录入同名同姓的学生,查询不到数据

SELECT a.s_name,a.s_sex,COUNT(*) FROM student a 
JOIN student b ON a.s_name=b.s_name AND a.s_id!=b.s_id 
AND a.s_sex=b.s_sex GROUP BY a.s_name,a.s_sex;

28.查询1990年出生的学生的信息

SELECT * FROM student WHERE YEAR(s_age)="1990" ;

29.查询每门课程的平均成绩并降序排列

SELECT c_id,AVG(score) FROM sc GROUP BY c_id ORDER BY AVG(score) DESC;

30.查询平均成绩大于等于85的学生信息

SELECT a.*,AVG(b.score) AS "平均分" FROM student a,sc b 
WHERE a.s_id =b.s_id GROUP BY s_id HAVING AVG(score)>85;

31.查询"数学"课分数低于60的学生姓名、分数

SELECT a.s_name AS "姓名",b.score AS "分数" FROM student a,sc b 
WHERE a.s_id=b.s_id AND c_id=(SELECT c_id FROM course WHERE c_name="数学") 
AND score<60;

32.查询学生的课程及分数情况,显示结果为学生姓名、各科课程名(课程名下显示成绩)

SELECT DISTINCT a.s_id,e.s_name AS "姓名",b.score AS "语文",
c.score AS "学",d.score AS "英语" 
FROM sc a  LEFT JOIN sc b ON a.s_id=b.s_id  AND b.c_id=1 
LEFT JOIN sc c ON a.s_id=c.s_id AND c.c_id=2 
LEFT JOIN sc d ON a.s_id=d.s_id AND d.c_id=3 
LEFT JOIN student e ON a.s_id=e.s_id;

33.查询任何一门课程成绩在70分以上的学生姓名、课程名、成绩

SELECT a.s_name,b.c_name,c.score FROM sc c 
JOIN course b ON c.c_id=b.c_id AND c.score>70 
JOIN student a ON a.s_id=c.s_id ;

34.查询不及格的课程的学生的姓名、课程名、成绩

SELECT a.s_name,b.c_name,c.score FROM sc c 
JOIN course b ON c.c_id=b.c_id AND c.score<60 
JOIN student a ON a.s_id=c.s_id ;

35.查询课程编号为 01 且课程成绩在 79 分以上的学生的学号和姓名

SELECT a.s_id,a.s_name FROM student a,sc b WHERE a.s_id=b.s_id AND b.score>79 AND b.c_id=1;

36.求每门课程的学生人数,显示课程名、人数

SELECT a.c_name AS "课程名",COUNT(*) AS "人数" FROM course a,sc b WHERE a.c_id=b.c_id GROUP BY b.c_id;

37.查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩(仅一名学生)

SELECT d.*,a.score AS "成绩" FROM sc a 
JOIN course b ON a.c_id=b.c_id 
JOIN teacher c ON b.t_id=c.t_id AND c.t_name="张三" 
JOIN student d ON a.s_id=d.s_id ORDER BY score DESC LIMIT 1;

38.查询课程编号不同但成绩相同的学生的学生编号、课程编号、学生成绩

 SELECT DISTINCT (a.s_id),a.c_id,a.score FROM sc a 
 JOIN sc b ON a.s_id=b.s_id  AND a.c_id!=b.c_id AND a.score=b.score;

39.查询每门成绩最好的前两名(名次可重复,取不同成绩,显示结果为课程编号、学号、成绩、排名)

SELECT c_id AS "课程编号",s_id AS "学号",score AS "成绩",
a AS "排名" FROM (SELECT c_id,s_id,score,dense_rank() over(PARTITION BY c_id ORDER BY score DESC)a 
FROM sc)b WHERE a<3; 

40.查询至少选修两门课程的学生学号,姓名,课程数

 SELECT b.s_name,b.s_id,COUNT(*) AS "选修课程门数" 
 FROM sc a,student b WHERE a.s_id=b.s_id GROUP BY a.s_id 
 HAVING COUNT(a.c_id)>=2; 

41.查询选修了全部课程的学生姓名、学号

 SELECT b.s_name,b.s_id FROM sc a,student b 
 WHERE a.s_id=b.s_id GROUP BY a.s_id 
 HAVING COUNT(*)=(SELECT COUNT(*) FROM course);

42.查询学生的姓名,学号,年龄

SELECT  s_name,s_id,TIMESTAMPDIFF(YEAR, s_age, CURDATE()) AS "年龄" FROM student;

43.查询本周过生日的学生信息

 SELECT * FROM student WHERE WEEKOFYEAR(s_age) = WEEKOFYEAR(CURDATE());

44.查询本月过生日的学生信息

 SELECT * FROM student WHERE MONTH(s_age) = MONTH(CURDATE());

45.查询12月份过生日的男同学的信息(学号、姓名、性别、年龄)

SELECT  s_id,s_name,s_sex,TIMESTAMPDIFF(YEAR, s_age, CURDATE()) 
AS "年龄" FROM student WHERE s_sex="男" 
AND SUBSTR(s_age,6,7)=12;

你可能感兴趣的:(MySQL,mysql)