表名和字段
–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) –学生编号,课程编号,分数
set sql_mode = ‘‘;
set sql_mode = ‘NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES‘;
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);
SELECT a.*, d.01_score, d.02_score FROM student a INNER JOIN
(SELECT b.s_id, b.01_score, c.02_score FROM
(SELECT s_id, s_score as 01_score FROM score WHERE c_id=‘01‘)b INNER JOIN
(SELECT s_id, s_score as 02_score FROM score WHERE c_id=‘02‘)c ON b.s_id=c.s_id
WHERE b.01_score > c.02_score)d ON a.s_id=d.s_id;
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 = c.s_id AND
a.s_id = b.s_id AND
b.c_id = ‘01‘ AND
c.c_id = ‘02‘ AND
b.s_score > c.s_score;
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;
SELECT a.s_name, ROUND(AVG(b.s_score), 2) as avg_score
FROM student a JOIN score b ON a.s_id=b.s_id
GROUP BY a.s_id
HAVING avg_score >= 60
ORDER BY avg_score DESC;
# (包括有成绩的和无成绩的)
SELECT b.s_name, b.s_id, ROUND(SUM(a.s_score)/COUNT(a.c_id),2) as avg_score
FROM score a JOIN student b ON a.s_id=b.s_id
GROUP BY a.s_id HAVING avg_score < 60
UNION SELECT a.s_name, a.s_id, 0 as avg_score FROM student a WHERE a.s_id NOT in (SELECT DISTINCT s_id FROM score);
SELECT a.s_id, a.s_name, COUNT(b.c_id)as sum_course, SUM(b.s_score) as sum_score FROM student a JOIN score b on a.s_id=b.s_id GROUP BY a.s_id,a.s_name
UNION SELECT a.s_id, a.s_name, 0 as sum_sourse, 0 as sum_score FROM student a WHERE a.s_id NOT in (SELECT DISTINCT s_id FROM score);
SELECT COUNT(t_id) as ‘李姓老师数量‘ FROM teacher WHERE t_name LIKE "李%" GROUP BY t_id;
SELECT a.* FROM student a, score b, teacher c, course d
WHERE a.s_id=b.s_id AND
b.c_id = d.c_id AND
c.t_id = d.t_id AND
c.t_name = ‘张三‘;
SELECT a.* FROM student a WHERE a.s_id NOT in (SELECT a.s_id FROM student a, score b, teacher c, course d
WHERE a.s_id=b.s_id AND
b.c_id = d.c_id AND
c.t_id = d.t_id AND
c.t_name = ‘张三‘);
SELECT a.* FROM student a JOIN
(SELECT a.s_id FROM (SELECT s_id FROM score WHERE c_id=‘01‘)a INNER JOIN (SELECT s_id FROM score WHERE c_id=‘02‘)b on a.s_id=b.s_id)d on a.s_id=d.s_id;
SELECT c.* FROM student c JOIN
(SELECT a.s_id FROM score a WHERE a.c_id=‘01‘ AND a.s_id NOT in (SELECT s_id FROM score WHERE c_id=‘02‘))d ON c.s_id=d.s_id;
SELECT a.*, COUNT(b.c_id) as sum_course FROM student a JOIN score b ON a.s_id=b.s_id GROUP BY a.s_id HAVING sum_course < 3;
SELECT DISTINCT a.* FROM student a JOIN score b ON a.s_id=b.s_id WHERE b.c_id in
(SELECT c.c_id FROM score c WHERE c.s_id=‘01‘);
SELECT c.*, d.course FROM student c JOIN
(SELECT a.s_id, a.course FROM
(SELECT s_id, GROUP_CONCAT(c_id) as course FROM score GROUP BY s_id)a JOIN (SELECT s_id, GROUP_CONCAT(c_id) as course FROM score GROUP BY s_id HAVING s_id=‘01‘)b
ON a.course=b.course)d ON c.s_id=d.s_id WHERE c.s_id not in (1);
SELECT a.c_id, b.c_name, MAX(a.s_score) as max_score, MIN(a.s_score) as min_score, ROUND(AVG(a.s_score), 2) as avg_score, COUNT(a.s_id)as count_stu,
CONCAT(ROUND(SUM(CASE when a.s_score >= 60 THEN 1 ELSE 0 END)/COUNT(), 2)100, ‘%‘) as ‘及格率‘,
CONCAT(ROUND(SUM(CASE when a.s_score >= 70 and a.s_score < 80 THEN 1 ELSE 0 END)/COUNT(), 2)100, ‘%‘) as ‘中等率‘,
CONCAT(ROUND(SUM(CASE when a.s_score >= 80 and a.s_score < 80 THEN 1 ELSE 0 END)/COUNT(),2)100, ‘%‘) as ‘优良率‘,
CONCAT(ROUND(SUM(CASE when a.s_score >= 90 THEN 1 ELSE 0 END)/COUNT(),2)100, ‘%‘) as ‘优秀率‘
FROM score a JOIN course b ON a.c_id=b.c_id GROUP BY a.c_id ORDER BY count_stu DESC, a.c_id ASC;
select a.s_id,a.c_id,
@i:[email protected] +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;
SELECT a.s_id, a.sum_score,
@i:[email protected]+1 as i, # 保留排名
@k:=(CASE WHEN @score=a.sum_score THEN @k ELSE @i END) as rank, # 不保留排名
@score:=a.sum_score as score FROM
(SELECT s_id, sum(s_score)as sum_score FROM score GROUP BY s_id ORDER BY sum_score DESC)a, (SELECT @k:=0, @i:=0, @score:=0)s;
SELECT score.c_id, c_name,
CONCAT(ROUND(sum(CASE WHEN s_score <=100 and s_score>85 THEN 1 ELSE 0 END)/COUNT(),2)100,‘%‘) as ‘[100-85]‘,
CONCAT(ROUND(sum(CASE WHEN s_score <=85 and s_score>70 THEN 1 ELSE 0 END)/COUNT(),2)100,‘%‘) as ‘[85-70]‘,
CONCAT(ROUND(sum(CASE WHEN s_score <=70 and s_score>60 THEN 1 ELSE 0 END)/COUNT(),2)100,‘%‘) as ‘[70-60]‘,
CONCAT(ROUND(sum(CASE WHEN s_score <=60 and s_score>=0 THEN 1 ELSE 0 END)/COUNT(),2)100,‘%‘) as ‘[60-0]‘
FROM score JOIN course ON score.c_id=course.c_id GROUP BY score.c_id;
(SELECT c_id, s_score FROM score WHERE c_id = ‘01‘ ORDER BY s_score DESC LIMIT 3)
UNION (SELECT c_id, s_score FROM score WHERE c_id = ‘02‘ ORDER BY s_score DESC LIMIT 3)
UNION (SELECT c_id, s_score FROM score WHERE c_id = ‘03‘ ORDER BY s_score DESC LIMIT 3);
SELECT c_id, COUNT(s_id)as sum_stu FROM score GROUP BY c_id;
SELECT a.s_id, b.s_name, COUNT(a.c_id)as sum_course FROM score a JOIN student b ON a.s_id=b.s_id GROUP BY a.s_id HAVING sum_course = 2;
SELECT s_sex, COUNT(s_id)as sex_count FROM student GROUP BY s_sex;
SELECT * FROM student WHERE s_name LIKE ‘%风%‘;
SELECT s_name,s_sex, count()as ‘同名人数‘
FROM student
GROUP BY s_name,s_sex
HAVING COUNT() >1;
SELECT * FROM student WHERE YEAR(s_birth)=‘1990‘;
SELECT c_id, ROUND(AVG(s_score),2)as avg_score FROM score GROUP BY c_id ORDER BY avg_score DESC, c_id;
SELECT a.s_id,b.s_name, ROUND(SUM(a.s_score)/COUNT(a.c_id), 2) as avg_score FROM score a JOIN student b on a.s_id=b.s_id GROUP BY a.s_id ORDER BY avg_score DESC
SELECT a.s_id, b.c_name,c.s_name, a.s_score FROM score a JOIN course b on a.c_id=b.c_id JOIN student c ON a.s_id=c.s_id WHERE b.c_name=‘数学‘ AND a.s_score < 60;
SELECT * FROM score
SELECT d.s_name, f.c_name, e.s_score FROM student d JOIN score e ON d.s_id=e.s_id JOIN course f ON e.c_id = f.c_id WHERE d.s_id NOT in
(SELECT a.s_id FROM
(SELECT s_id, COUNT(c_id)as course_count FROM score GROUP BY s_id)a JOIN
(SELECT s_id, COUNT(c_id)as course_count FROM score WHERE s_score < 70 GROUP BY s_id)b ON (a.course_count=b.course_count and a.s_id=b.s_id));
SELECT a.s_id,b.s_name, c.c_name, a.s_score FROM score a JOIN student b ON a.s_id=b.s_id JOIN course c ON a.c_id=c.c_id WHERE s_score < 60;
SELECT a.s_id, b.s_name, a.c_id,a.s_score FROM score a JOIN student b ON a.s_id=b.s_id WHERE a.c_id=‘01‘ AND a.s_score >= 80;
SELECT c_id, COUNT(s_id) as count_stu FROM score GROUP BY c_id;
SELECT d.*, b.c_name, c.s_score FROM teacher a
JOIN course b ON a.t_id=b.t_id
JOIN score c ON b.c_id=c.c_id
JOIN student d ON c.s_id=d.s_id
WHERE a.t_name=‘张三‘ ORDER BY c.s_score DESC LIMIT 1;
select a.*,b.s_score,b.c_id,c.c_name from student a
LEFT JOIN score b on a.s_id = b.s_id
LEFT JOIN course c on b.c_id=c.c_id
where b.c_id =(select c_id from course c,teacher d where c.t_id=d.t_id and d.t_name=‘张三‘)
and b.s_score in (select MAX(s_score) from score where c_id=‘02‘)
select DISTINCT b.s_id,b.c_id,b.s_score from score a,score b where a.c_id != b.c_id and a.s_score = b.s_score;
select a.s_id,a.c_id,a.s_score from score a
where (select COUNT(1) from score b where b.c_id=a.c_id and b.s_score>=a.s_score)<=2 ORDER BY a.c_id;
SELECT c_id, COUNT(s_id)as stu_count FROM score GROUP BY c_id HAVING stu_count > 5 ORDER BY stu_count DESC, c_id;
SELECT s_id, COUNT(c_id)as course_count FROM score GROUP BY s_id HAVING course_count >= 2;
SELECT b.*,COUNT(a.c_id) as course_count FROM score a JOIN student b ON a.s_id=b.s_id GROUP BY a.s_id HAVING course_count=(SELECT COUNT(c_id) FROM course);
-- 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
MySQL 50题练习
原文:https://www.cnblogs.com/wsilj/p/12769760.html