SQL经典五十题个人实战与总结(1-10题)

建表:

-- 建表

-- 学生表

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);

*题目一:查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数

select n1.s_id, score1, score2

from (select s_id, s_score as score1 from score where c_id=01) n1 join (select s_id, s_score as score2 from score where c_id=02) n2 on n1.s_id=n2.s_id

where score1>score2;

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

select n1.s_id, score1, score2

from (select s_id, s_score as score1 from score where c_id=01) n1 join (select s_id, s_score as score2 from score where c_id=02) n2 on n1.s_id=n2.s_id;

1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )

select n1.s_id, score1, score2

from (select s_id, s_score as score1 from score where c_id=01) n1 left join (select s_id, s_score as score2 from score where c_id=02) n2 on n1.s_id=n2.s_id;

1.3 查询不存在" 01 "课程但存在" 02 "课程的情况

select n2.s_id, score1, score2

from (select s_id, s_score as score1 from score where c_id=01) n1 right join (select s_id, s_score as score2 from score where c_id=02) n2 on n1.s_id=n2.s_id;

题目二:查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩

select st.s_id, st.s_name, avg(s_score) as avg_score

from student st join score sc on st.s_id=sc.s_id

group by st.s_id

having avg_score >= 60;

题目三:查询在 SC 表存在成绩的学生信息

select distinct st.s_id, st.s_name, avg(sc.s_score)

from student st join score sc on st.s_id=sc.s_id

group by st.s_id;

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

select st.s_id, st.s_name, count(s_score) as num_course, sum(s_score) as sum_score

from student st left join score sc on st.s_id=sc.s_id

group by st.s_id;

 4.1 查有成绩的学生信息

select st.s_id, st.s_name, count(s_score) as num_course, sum(s_score) as sum_score

from student st join score sc on st.s_id=sc.s_id

group by st.s_id;

题目五:查询「李」姓老师的数量

select count(t_name) as num_teacher

from teacher

where t_name like '李%';

(或者使用regexp '李.')

题目六:查询学过「张三」老师授课的同学的信息

select a.s_id, a.s_name,a.s_birth, a.s_sex, a.c_id, b.t_name

from (select st.s_id, st.s_birth, st.s_sex, s_name, c_id from student st join score sc on st.s_id=sc.s_id) a

join (select co.c_id, te.t_name from teacher te join course co on te.t_id=co.t_id) b on a.c_id=b.c_id

where b.t_name='张三';

经过与他人答案对比,有以下更简单的方法:

select student.*

from student, score sc

where student.s_id = sc.s_id

and sc.c_id in (select c_id from course, teacher where course.t_id = teacher.t_id and T_name = '张三')

*6.1查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

select * from student

where s_id in (select s_id from score where c_id=01)

and s_id in (select s_id from score where c_id=02);

6.2查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

select * from student

where s_id in (select s_id from score where c_id=01)

and s_id not in (select s_id from score where c_id=02);

题目七:查询没有学全所有课程的同学的信息

select * from student

where s_id not in (select s_id from score where c_id=01)

or s_id not in (select s_id from score where c_id=02)

or s_id not in (select s_id from score where c_id=03);

(也可以使用count查询学习课程数<总课程数的学生)

题目八:查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息

select distinct st.* from student st, score sc

where st.s_id=sc.s_id and sc.c_id in (select c_id from score where s_id=01);

***题目九:查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息

select st.* from student st where st.s_id in

(select distinct s_id from score where s_id!='01' and c_id in(select c_id from score where s_id='01')

 group by s_id

 having count(c_id)=(select count(c_id) from score where s_id='01'));

题目十:查询没学过"张三"老师讲授的任一门课程的学生姓名

select st.s_id, st.s_name

from student st

where st.s_id not in (select s_id

from score sc where sc.c_id in (select c_id from course co, teacher te where co.t_id=te.t_id and te.t_name='张三'));

(思路:先找出学过张三老师任一门课程的同学,将这些同学排除即可)

你可能感兴趣的:(SQL经典五十题个人实战与总结(1-10题))