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='张三'));

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

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

推荐阅读更多精彩内容

  • ORACLE自学教程 --create tabletestone ( id number, --序号usernam...
    落叶寂聊阅读 1,072评论 0 0
  • Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 S...
    忘了呼吸的那只猫阅读 2,855评论 0 8
  • 说明:以下五十个语句都按照测试数据进行过测试,最好每次只单独运行一个语句。 问题及描述: --1.学生表 Stud...
    lijun_m阅读 1,295评论 0 1
  • 50个常用的sql语句Student(S#,Sname,Sage,Ssex) 学生表Course(C#,Cname...
    哈哈海阅读 1,228评论 0 7
  • SQL笔试题50道答案整理 CREATE TABLE Student ( s_id VARCHAR(20), s...
    程序媛_阅读 508评论 0 0