知乎50题目

SQL笔试题50道答案整理

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 ,

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

select * from[dbo].[Course]

select * from[dbo].[Score]

select * from[dbo].[Student]

select * from[dbo].[Teacher]

--11、查询至少有一门课与学号为“01”的学生所学课程相同的学生的学号和姓名

select distinct a.s_id, a.s_name

from Student a join Score b on a.s_id= b.s_id

where c_id in

(select c_id from Score where s_id = '01')

and a.s_id<>'01';

--※10、查询没有学全所有课的学生的学号、姓名

--select[s_id], [s_name]from [dbo].[Student] where not in(select [s_id] from [dbo].[Score] where c_id)

--写法一:

select Student.s_id, Student.s_name

from Student join Score on Score.s_id = Student.s_id

group by Student.s_id, s_name

having count(Score.c_id) < (select count(c_id) from Course);

--写法二:

select s_id, s_name

from Student

where s_id IN (SELECT s_id FROM Score group by s_id

having count(c_id)<(select count(c_id) from Course))

--小于还可以是不等于

--9、查询所有课程成绩小于60分的学生的学号、姓名

select s1.s_id,s1.s_name from  [dbo].[Student] s1 join [dbo].[Score] s2

on s1.s_id=s2.s_id where s_score<60--mistake

select[s_id], [s_name]from [dbo].[Student] where [s_id]

not in(select [s_id]from [dbo].[Score] where [s_score]>=60)--√

select s_id, s_name

from Student

where s_id NOT IN (SELECT s_id FROM Score where s_score >=60)--answer

--8、查询课程编号为“02”的总成绩

select sum(s_score) from [dbo].[Score] where [c_id]='02'

select sum(s_score)

from Score

where c_id = '02';--answer

--※7、查询学过编号为“01”的课程并且也学过编号为“02”的课程的学生的学号、姓名

select s1.s_id,s1.s_name  from [dbo].[Student] s1 join [dbo].[Score] s2

on s1.s_id=s2.s_id where s2.c_id in('01','02')--mistake


  --写法一:

select s_id, s_name from Student

where s_id in

(select a.s_id from

(select s_id from Score where c_id = '01') as a

join (select s_id from Score where c_id ='02') as b

on a.s_id= b.s_id);

--写法二:

select s_id,s_name 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')

--写法三:

select a.s_id,a.s_name

from Student a JOIN Score b ON a.s_id=b.s_id

JOIN Score c ON a.s_id=c.s_id

where b.c_id='01' and c.c_id='02'

and b.s_id=c.s_id ;

--6、查询学过“张三”老师所教的所有课的同学的学号、姓名

select s1.s_id,s1.s_name from [dbo].[Student] s1 join [dbo].[Score] s2 on s1.s_id=s2.s_id join [dbo].[Course] c

on s2.c_id=c.c_id join [dbo].[Teacher] t on c.t_id=t.t_id where t.t_name='张三' --√

select s.s_id,s_name from [dbo].[Student] s where s.s_id in(select s.s_id from

[dbo].[Course] c join [dbo].[Teacher] t on c.t_id=t.t_id where t.t_name='张三')--mistake

--写法一:

select s_id, s_name

from Student

where s_id in

(select s_id from Score join Course on Score.c_id = Course.c_id

join Teacher on Course.t_id = Teacher.t_id

where t_name = '张三');

--写法二:

select Student.s_id, s_name

from Student JOIN Score on Student.s_id = Score.s_id

JOIN Course on Score.c_id = Course.c_id

JOIN Teacher on Teacher.t_id = Course.t_id

where t_name='张三';---连接4个表√

--※5.查询没学过“张三”老师课的学生的学号、姓名(重点)

select s1.s_id,s1.s_name from [dbo].[Student] s1 join [dbo].[Score] s2 on s1.s_id=s2.s_id join [dbo].[Course] c

on s2.c_id=c.c_id join [dbo].[Teacher] t on c.t_id=t.t_id where t.t_name!='张三'--mistake

select s.s_id,s_name from [dbo].[Student] s,[dbo].[Course] c ,[dbo].[Teacher] t where s.s_id=c.c_id and

c.t_id=t.t_id and t.t_name !='张三'--mistake

select s.s_id,s_name from [dbo].[Student] s join [dbo].[Course] c on s.s_id=c.c_id join [dbo].[Teacher] t on

c.t_id=t.t_id where t.t_name!='张三'--mistake

select s_id, s_name

from Student

where s_id not in

(select s_id from Score join Course on Score.c_id = Course.c_id

join Teacher on Course.t_id = Teacher.t_id

where t_name = '张三');--answer

--4、查询姓“张”的老师的个数

select count(*) from [dbo].[Teacher] where t_name like '张%'

select count(t_id)

from Teacher

where t_name like '张%';---answer

--3、查询所有学生的学号、姓名、选课数、总成绩

select s1.[s_id] ,[s_name],count([c_id]),sum([s_score]) from [dbo].[Score] s1

join [dbo].[Student]  s2 on s1.s_id=s2.s_id group by s1.[s_id],[s_name]

select Score.s_id AS '学号', s_name AS '姓名',count(c_id)AS '选课数',sum(s_score)AS '总成绩'

from Score join Student on Score.s_id = Student.s_id

group by Score.s_id,s_name;--answer

--2、查询平均成绩大于60分的学生的学号和平均成绩

select [s_id],AVG([s_score]) from [dbo].[Score] where [s_score]>60 group by [s_id]

select s_id, avg(s_score)

from Score

group by s_id

having avg(s_score)>60;--标准答案

--※1、查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号。

--select * from [dbo].[Course] c,[dbo].[Score] s1,[dbo].[Student] s2 where c.c_id=s1.c_id and  s1.s_id=s2.s_id and

-- 写法一:

select a.s_id from [dbo].[Score] a join [dbo].[Score] b on a.s_id=b.s_id and a.s_score>b.s_score where

a.c_id='01' and b.c_id='02'

select a.s_id

from Score a join Score b on a.s_id = b.s_id and a.s_score >b.s_score

where a.c_id = '01' and b.c_id= '02';

-- 写法二:

select a.s_id

from (select * from Score where c_id = '01') as a

join (select * from Score where c_id='02') as b

on a.s_id = b.s_id

where a.s_score > b.s_score;

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

推荐阅读更多精彩内容

  • 50个常用的sql语句Student(S#,Sname,Sage,Ssex) 学生表Course(C#,Cname...
    哈哈海阅读 1,224评论 0 7
  • Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 S...
    忘了呼吸的那只猫阅读 2,832评论 0 8
  • ORACLE自学教程 --create tabletestone ( id number, --序号usernam...
    落叶寂聊阅读 1,064评论 0 0
  • 说明:以下五十个语句都按照测试数据进行过测试,最好每次只单独运行一个语句。 问题及描述: --1.学生表 Stud...
    lijun_m阅读 1,288评论 0 1
  • MSSQL 跨库查询(臭要饭的!黑夜) 榨干MS SQL最后一滴血 SQL语句参考及记录集对象详解 关于SQL S...
    碧海生曲阅读 5,542评论 0 1