Mysql 查询练习1-10

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zx6571269/article/details/80099323

Mysql 练习1-10


数据表和数据

drop table  if exists student;
create table student(
stuNo VARCHAR(32),
stuName varchar(32),
stuBrithday datetime,
stuSex CHAR(2)
)
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' , '女');

drop table if exists course;
create table course(
courseNo varchar(32),
courseName varchar(32),
courseTeaNo varchar(32)
)

insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');

drop table if exists teacher;
create table teacher(
teacherNo varchar(32),
teacherName varchar(32)
)

insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');

drop table if exists stuScore;
create table stuScore(
    stuNo varchar(10),
    courseNo varchar(10),
    score decimal(18.1)
)
insert into stuScore values('01' , '01' , 80);
insert into stuScore values('01' , '02' , 90);
insert into stuScore values('01' , '03' , 99);
insert into stuScore values('02' , '01' , 70);
insert into stuScore values('02' , '02' , 60);
insert into stuScore values('02' , '03' , 80);
insert into stuScore values('03' , '01' , 80);
insert into stuScore values('03' , '02' , 80);
insert into stuScore values('04' , '01' , 50);
insert into stuScore values('04' , '02' , 30);
insert into stuScore values('04' , '03' , 20);
insert into stuScore values('05' , '01' , 76);
insert into stuScore values('05' , '02' , 87);
insert into stuScore values('06' , '01' , 31);
insert into stuScore values('06' , '03' , 34);
insert into stuScore values('07' , '02' , 89);
insert into stuScore values('07' , '03' , 98);

1.查询” 01 “课程比” 02 “课程成绩高的学生的信息及课程分数

-- 左连接写法
select a.*,b.* from student a LEFT JOIN (select * from stuscore where courseNo = '01') b 
on a.stuNo = b.stuNo    LEFT JOIN (select * from stuscore where courseNo = '02') c 
on b.stuNo = c.stuNo
where b.score > c.score

select a.*,b.* from  student a,(select * from stuscore where courseNo = '01') b,(select * from stuscore where courseNo='02') c
where a.stuNo = b.stuNo and b.stuNo = c.stuNo and b.score > c.score

2.查询同时存在” 01 “课程和” 02 “课程的情况

select a.* ,b.*from  (select * from stuscore where courseNo = '01') a

LEFT JOIN (select * from stuscore where courseNo = '02') b 

on a.stuNo = b.stuNo 

where b.stuNo  is not null

3.查询不存在” 01 “课程但存在” 02 “课程的情况

-- 解析: 查询出课程01的成绩单,查询02的成绩单, 通过 not in ()过滤出

select * from stuscore where courseNo = '01'; 

select * from stuscore where courseNo = '02';


select a.*,b.courseNo from (select * from stuscore where courseNo = '02') a  LEFT JOIN

(select * from stuscore where courseNo = '01') b 

on a.stuNo = b.stuNo

where a.stuNo not in(select stuNo from stuscore where courseNo = '01')

4.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩

-- 4.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩

select student.*,AVG(stuscore.score) from stuscore RIGHT JOIN student 
on student.stuNo = stuscore.stuNo
GROUP BY stuscore.stuNo HAVING AVG(stuscore.score) > 60

5.查询在 SC 表存在成绩的学生信息


-- 写法1:使用 GROUP BY stuNo 获取stuNo
select * from student where stuNo in(select  stuNo from stuscore GROUP BY stuNo);

-- 写法2: 使用 DISTINCT 去掉重复 stuNo

SELECT * from student where stuNo in (select DISTINCT stuNo from stuscore);

6.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为null)

select b.*,c.totalCourse,c.totalScore from student b LEFT JOIN (select a.stuNo,COUNT(a.score) totalCourse, SUM(a.score) totalScore from stuscore a GROUP BY a.stuNo) c
on  c.stuNo = b.stuNo

7.查有成绩的学生信息

select b.*,c.totalCourse,c.totalScore from student b,(select a.stuNo,COUNT(a.score) totalCourse, SUM(a.score) totalScore from stuscore a GROUP BY a.stuNo) c
where  c.stuNo = b.stuNo

8.查询「李」姓老师的数量

select COUNT(*) from teacher where teacherName LIKE '李%'

9.查询学过「张三」老师授课的同学的信息

select teacherNO from teacher where teacherName = '张三'; -- 张三老师的编号

select courseNo from course where courseTeaNo = (
        select teacherNO from teacher where teacherName = '张三') -- 获取张三老师教学的编号courseNO 的信息

select stuNo from  stuscore where courseNo = (
            select courseNo from course where courseTeaNo = (
                select teacherNO from teacher where teacherName = '张三'))-- 获取学过张三老师课程的stuNO

SELECT * FROM student where stuNo in(
    select stuNo from  stuscore where courseNo = (
            select courseNo from course where courseTeaNo = (
                select teacherNO from teacher where teacherName = '张三'))
)

10.查询没有学全所有课程的同学的信息

select count(courseNo) from course -- 查询总课程数

select stuNo from stuscore  GROUP BY stuNo HAVING COUNT(score)< (
    select count(courseNo) from course) -- 查询stuscore 


select * from student where stuNo in(
        select stuNo from stuscore  GROUP BY stuNo HAVING COUNT(score)< (
            select count(courseNo) from course));
阅读更多
换一批

没有更多推荐了,返回首页