Mysql-练习题

1 .准备表:https://www.jianshu.com/p/476b52ee4f1b

create table student(id varchar(10),name varchar(10),age datetime,sex varchar(10));

create table course(id varchar(10),name nvarchar(10),teacherid varchar(10));

create table teacher(id varchar(10),name varchar(10));

create table score(studentid varchar(10), courseid varchar(10),score decimal(18,1));

insert into `course` (`id`, `name`, `teacherid`) values('01','语文','02');
insert into `course` (`id`, `name`, `teacherid`) values('02','数学','01');
insert into `course` (`id`, `name`, `teacherid`) values('03','英语','03');

insert into `teacher` (`id`, `name`) values('01','张三');
insert into `teacher` (`id`, `name`) values('02','李四');
insert into `teacher` (`id`, `name`) values('03','王五');

insert into `student` (`Id`, `name`, `age`, `sex`) values('01','赵雷','1990-01-01 00:00:00','男');
insert into `student` (`Id`, `name`, `age`, `sex`) values('02','钱电','1990-12-21 00:00:00','男');
insert into `student` (`Id`, `name`, `age`, `sex`) values('03','孙风','1990-12-20 00:00:00','男');
insert into `student` (`Id`, `name`, `age`, `sex`) values('04','李云','1990-12-06 00:00:00','男');
insert into `student` (`Id`, `name`, `age`, `sex`) values('05','周梅','1991-12-01 00:00:00','女');
insert into `student` (`Id`, `name`, `age`, `sex`) values('06','吴兰','1992-01-01 00:00:00','女');
insert into `student` (`Id`, `name`, `age`, `sex`) values('07','郑竹','1989-01-01 00:00:00','女');
insert into `student` (`Id`, `name`, `age`, `sex`) values('09','张三','2017-12-20 00:00:00','女');
insert into `student` (`Id`, `name`, `age`, `sex`) values('10','李四','2017-12-25 00:00:00','女');
insert into `student` (`Id`, `name`, `age`, `sex`) values('11','李四','2012-06-06 00:00:00','女');
insert into `student` (`Id`, `name`, `age`, `sex`) values('12','赵六','2013-06-13 00:00:00','女');
insert into `student` (`Id`, `name`, `age`, `sex`) values('13','孙七','2014-06-01 00:00:00','女');

insert into `score` (`studentId`, `courseId`, `score`) values('01','01','80.0');
insert into `score` (`studentId`, `courseId`, `score`) values('01','02','90.0');
insert into `score` (`studentId`, `courseId`, `score`) values('01','03','99.0');
insert into `score` (`studentId`, `courseId`, `score`) values('02','01','70.0');
insert into `score` (`studentId`, `courseId`, `score`) values('02','02','60.0');
insert into `score` (`studentId`, `courseId`, `score`) values('02','03','80.0');
insert into `score` (`studentId`, `courseId`, `score`) values('03','01','80.0');
insert into `score` (`studentId`, `courseId`, `score`) values('03','02','80.0');
insert into `score` (`studentId`, `courseId`, `score`) values('03','03','80.0');
insert into `score` (`studentId`, `courseId`, `score`) values('04','01','50.0');
insert into `score` (`studentId`, `courseId`, `score`) values('04','02','30.0');
insert into `score` (`studentId`, `courseId`, `score`) values('04','03','20.0');
insert into `score` (`studentId`, `courseId`, `score`) values('05','01','76.0');
insert into `score` (`studentId`, `courseId`, `score`) values('05','02','87.0');
insert into `score` (`studentId`, `courseId`, `score`) values('06','01','31.0');
insert into `score` (`studentId`, `courseId`, `score`) values('06','03','34.0');
insert into `score` (`studentId`, `courseId`, `score`) values('07','02','89.0');
insert into `score` (`studentId`, `courseId`, `score`) values('07','03','98.0');
Q1:查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数

SELECT s1.*,s2.* FROM 
(SELECT studentId,
       MAX(CASE courseId WHEN '01' THEN score ELSE 0 END) AS c1 ,
       MAX(CASE courseId WHEN '02' THEN score ELSE 0 END) AS c2 
       FROM score GROUP BY studentId  HAVING c1 > c2) s1 
LEFT JOIN student s2 ON  s1.studentId = s2.id

Q2:查询同时存在" 01 "课程和" 02 "课程的情况

SELECT t1.studentId  FROM 
(SELECT  studentid FROM score sc1 WHERE courseId = '01') AS t1 
INNER JOIN 
(SELECT studentId FROM score WHERE courseId = '02') AS t2 ON t1.studentId = t2.studentId

Q3:查询不存在" 01 "课程但存在" 02 "课程的情况
SELECT studentId,
MAX(CASE courseId WHEN '01' THEN 1 ELSE NULL END) AS t1,
MAX(CASE courseId WHEN '02' THEN 1 ELSE NULL END) AS t2 
FROM score GROUP BY studentId HAVING t1 IS NULL AND t2 IS NOT NULL

Q4:查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
SELECT t1.studentId,s.name,t1.average  FROM 
(SELECT  studentId ,AVG(score) AS  average FROM score GROUP BY studentId ) AS t1  
LEFT JOIN student s ON  t1.studentId = s.id 

Q5:没学过"张三"老师讲授的任一门课程的学生姓名
select * from student
    where student.sid not in(
        select sc.sid from sc where sc.cid in(
            select course.cid from course where course.tid in(
                select teacher.tid from teacher where tname = "张三"
            )
        )
    );

SELECT  studentId ,
SUM(CASE WHEN courseId IN (SELECT DISTINCT(id) FROM teacher WHERE NAME = '张三') THEN 1 ELSE 0 END) t
FROM score GROUP BY studentId HAVING t =0

Q6:查询下月过生日的学生
SELECT *
FROM student 
WHERE EXTRACT(MONTH FROM student.age)= EXTRACT(MONTH FROM DATE_ADD(CURDATE(),INTERVAL 1 MONTH))

猜你喜欢

转载自blog.csdn.net/Tiggcat/article/details/96475209