经典地45道MySQL例题—前10题

做了些修改,水平一般,正在学习中,如有错误请指正。

CREATE DATABASE IF NOT EXISTS school DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
use school;
# 学生表 Student
create table if not exists Student(SId varchar(10),Sname varchar(10),Sage datetime,Ssex varchar(10));
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('09' , '张三' , '2017-12-20' , '女');
insert into Student values('10' , '李四' , '2017-12-25' , '女');
insert into Student values('11' , '李四' , '2017-12-30' , '女');
insert into Student values('12' , '赵六' , '2017-01-01' , '女');
insert into Student values('13' , '孙七' , '2018-01-01' , '女');

# 科目表 Course
create table if not exists Course(CId varchar(10),Cname nvarchar(10),TId varchar(10));
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');

# 教师表 Teacher
create table if not exists Teacher(TId varchar(10),Tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');

# 成绩表 SC
create table if not exists SC(SId varchar(10),CId varchar(10),score decimal(18,1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98);

# 1. 查询" 01 "课程比" 02 "课程成绩高的学生的ID及课程分数
select t1.SId, t1.score as 01_score, t2.score as 02_score, t3.Sname, t3.Sage, t3.Ssex from 
(select SId, score from sc where CId='01') as t1 join
(select SId, score from sc where CId='02') as t2 join
Student t3
where t1.score>t2.score and t1.SId=t2.SId and t1.SId=t3.SId;
# 1.1 查询同时存在学习"01"课程和"02"课程的学生ID
select t1.SId from 
(select SId,CId from SC where CId = "01") as t1 join 
(select SId,CId from SC where CId = "02") as t2 
where t1.SId=t2.SId;
# 1.2 查询学习"01"课程但可能不学习"02"课程的学生ID和课程ID(不学习的课程ID显示为 null)
select t1.SId, t1.CId, t2.CId from 
(select SId,CId from SC where CId = "01") as t1 left join 
(select SId,CId from SC where CId = "02") as t2 
on t1.SId=t2.SId;
# 1.3 查询不学习" 01 "课程但学习" 02 "课程的学生ID和课程ID(不学习的课程ID显示为 null)
select t2.SId, t1.CId, t2.CId from 
(select SId,CId from SC where CId = "01") as t1 right join 
(select SId,CId from SC where CId = "02") as t2 
on t1.SId=t2.SId;

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

select t1.SId, t2.Sname, convert(t1.score_sum/t1.course_num, decimal(10, 1)) as score_avg from 
(select SId, sum(score) as score_sum, count(SId) as course_num from sc group by SId) as t1 join
student t2
where t1.SId=t2.SId and t1.score_sum/t1.course_num>60;

# 3. 查询在 SC 表存在成绩的学生信息
select t2.* from 
(select distinct(SId) from sc) as t1 left join
student as t2
on t1.SId=t2.SId;

# 4. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null)
select t1.SId, t1.Sname, t2.course_num, t2.score_sum from 
student as t1 left join 
(select SId, sum(score) as score_sum, count(SId) as course_num from sc group by SId) as t2
on t1.SId=t2.SId;

# 4.1 查有成绩的学生信息
select t2.* from 
(select distinct(SId) from sc) as t1 left join
student as t2
on t1.SId=t2.SId;

# 5. 查询「李」姓老师的数量 
select count(*) from teacher where Tname like "李%";

# 6. 查询学过「张三」老师授课的同学的信息
select distinct(t1.SId) from 
sc as t1 join
course t2 join
(select TId from teacher where Tname="张三") as t3 
where t1.CId=t2.CId and t3.TId=t2.TId;

# 7. 查询没有学全所有课程的同学的信息
select * from
student t1 left join 
(select distinct(SId), count(CId) as course_num from sc group by SId) as t2
on t1.SId=t2.SId and t2.course_num<3;

# 8. 查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
select distinct(SId) from sc where CId in(
select CId from sc where SId="01") and SId!="01";

# 9. 查询和" 01 "号的同学学习的课程完全相同的其他同学的信息
select distinct student.* from
student join
(select count(t2.SId) as course_num, t2.SId from (select SId, CId from sc) as t2 left join
(select SId,CId from sc where SId="01") as t3
on t2.CId=t3.CId group by t2.SId) as t1
where student.SId!="01" and t1.SId=student.SId and t1.course_num=3;

# 10. 查询没学过"张三"老师讲授的任一门课程的学生姓名
select *
from student 
where student.SId not in 
(
select student.SId
from student left join sc on student.SId=sc.SId 
where EXISTS 
(select *
from teacher ,course
where teacher.Tname="张三"
and   teacher.TId=course.TId
and     course.CId=sc.CId))
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,830评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,992评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,875评论 0 331
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,837评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,734评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,091评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,550评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,217评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,368评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,298评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,350评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,027评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,623评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,706评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,940评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,349评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,936评论 2 341

推荐阅读更多精彩内容

  • 周二的段子 片段选自高琳的职场实用指南《职得》,高琳是一个有20多年世界500强公司工作经验的一个女士。她致力于跨...
    小小酥_b903阅读 207评论 6 0
  • 一到两个小时一直看书或者一直做题,不被看手机,聊天幻想等娱乐性质的打断。这样的学习才叫真正的学习。 当然知道这个道...
    12_05阅读 500评论 0 0
  • 春晓晚游湖 吁微凉 繁花不知处 香如故 斜影霓虹 彩幻斑斓无数 琐事萧沉 暗自解眉舒
    周大柱阅读 206评论 4 12