赢在面试之数据库篇

  作为一枚Java后端开发者,数据库知识必不可少,对数据库的掌握熟悉度的考察也是对这个人是否有扎实基本功的考察。特别对于初级开发者,面试可能不会去问框架相关知识,但是绝对不会不去考察数据库知识,这里收集一些常见类型的SQL语句,无论对于平常开发还是准备面试,都会有助益。

基本表结构:

student(sno,sname,sage,ssex)学生表

course(cno,cname,tno) 课程表

sc(sno,cno,score) 成绩表

 teacher(tno,tname) 教师表

1、查询课程1的成绩比课程2的成绩高的所有学生的学号

select a.sno from

(select sno,score from sc where cno=1) a,

(select sno,score from sc where cno=2) b

where a.score>b.score and a.sno=b.sno


2、查询平均成绩大于60分的同学的学号和平均成绩select a.sno as "学号", avg(a.score) as "平均成绩"

from

(select sno,score from sc) a

group by sno having avg(a.score)>60


3、查询所有同学的学号、姓名、选课数、总成绩select a.sno as 学号, b.sname as 姓名,

count(a.cno) as 

选课数, sum(a.score) as 总成绩

from sc a, student b

where a.sno = b.sno

group by a.sno, b.sname

或者:

selectstudent.sno as 学号, student.sname as 姓名,

count(sc.cno) as 

选课数, sum(score) as 总成绩

from student left Outer join sc on student.sno = sc.sno

group by student.sno, sname


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

selectcount(distinct(tname)) from teacher where tname like '张%'

或者:

select tname as "姓名", count(distinct(tname)) as "人数"

from teacher

where tname like'张%'

group by tname


5、查询没学过“张三”老师课的同学的学号、姓名

select student.sno,student.sname from student

where sno not in (select distinct(sc.sno) from sc,course,teacher

where sc.cno=course.cno and teacher.tno=course.tno and teacher.tname='张三')


6、查询同时学过课程1和课程2的同学的学号、姓名

select sno, sname from student

where sno in (select sno from sc where sc.cno = 1)

and sno in (select sno from sc where sc.cno = 2)

或者:

selectc.sno, c.sname from

(select sno from sc where sc.cno = 1) a,

(select sno from sc where sc.cno = 2) b,

student c

where a.sno = b.sno and a.sno = c.sno

或者:

select student.sno,student.sname from student,sc where student.sno=sc.sno and sc.cno=1

and exists( select * from sc as sc_2 where sc_2.sno=sc.sno and sc_2.cno=2)


7、查询学过“李四”老师所教所有课程的所有同学的学号、姓名select a.sno, a.sname from student a, sc b

where a.sno = b.sno and b.cno in

(select c.cno from course c, teacher d where c.tno = d.tno and d.tname = '李四')

或者:

select a.sno, a.sname from student a, sc b,

(select c.cno from course c, teacher d where c.tno = d.tno and d.tname = '李四') e

where a.sno = b.sno and b.cno = e.cno


8、查询课程编号1的成绩比课程编号2的成绩高的所有同学的学号、姓名

select a.sno, a.sname from student a,

(select sno, score from sc where cno = 1) b,

(select sno, score from sc where cno = 2) c

where b.score > c.score and b.sno = c.sno and a.sno = b.sno


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

select sno,sname from student

where sno not in (select distinct sno from sc where score > 60)


10、查询至少有一门课程与学号为1的同学所学课程相同的同学的学号和姓名

select distinct a.sno, a.sname

from student a, sc b

where a.sno <> 1 and a.sno=b.sno and

b.cno in (select cno from sc where sno = 1)

或者:

select s.sno,s.sname

from student s,

(select sc.sno

from sc

where sc.cno in (select sc1.cno from sc sc1 where sc1.sno=1)and sc.sno<>1

group by sc.sno)r1

where r1.sno=s.sno


11、把“sc”表中“王五”所教课的成绩都更改为此课程的平均成绩

update sc set score = (select avg(sc_2.score) from sc sc_2 wheresc_2.cno=sc.cno)

from course,teacher where course.cno=sc.cno and course.tno=teacher.tno andteacher.tname='王五'


12、查询和编号为2的同学学习的课程完全相同的其他同学学号和姓名

这一题分两步查:

1,

select sno

from sc

where sno <> 2

group by sno

having sum(cno) = (select sum(cno) from sc where sno = 2)

2,

select b.sno, b.sname

from sc a, student b

where b.sno <> 2 and a.sno = b.sno

group by b.sno, b.sname

having sum(cno) = (select sum(cno) from sc where sno = 2)


13、删除学习“王五”老师课的sc表记录

delete sc from course, teacher

where course.cno = sc.cno and course.tno = teacher.tno and tname = '王五'


14、向sc表中插入一些记录,这些记录要求符合以下条件:

将没有课程3成绩同学的该成绩补齐, 其成绩取所有学生的课程2的平均成绩

insert sc select sno, 3, (select avg(score) from sc where cno = 2)

from student

where sno not in (select sno from sc where cno = 3)


15、按平平均分从高到低显示所有学生的如下统计报表:

-- 学号,企业管理,马克思,UML,数据库,物理,课程数,平均分

select sno as 学号

,max(case when cno = 1 then score end) AS 企业管理

,max(case when cno = 2 then score end) AS 马克思

,max(case when cno = 3 then score end) AS UML

,max(case when cno = 4 then score end) AS 数据库

,max(case when cno = 5 then score end) AS 物理

,count(cno) AS 课程数

,avg(score) AS 平均分

FROM sc

GROUP by sno

ORDER by avg(score) DESC


16、查询各科成绩最高分和最低分:

以如下形式显示:课程号,最高分,最低分

select cno as 课程号, max(score) as 最高分, min(score) 最低分

from sc group by cno

select  course.cno as '课程号'

,MAX(score) as '最高分'

,MIN(score) as '最低分'

from sc,course

where sc.cno=course.cno

group by course.cno


17、按各科平均成绩从低到高和及格率的百分数从高到低顺序

SELECT t.cno AS 课程号,

max(course.cname)AS 课程名,

isnull(AVG(score),0) AS 平均成绩,

100 * SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0 END)/count(1) AS 及格率

FROM sc t, course

where t.cno = course.cno

GROUP BY t.cno

ORDER BY 及格率 desc


18、查询如下课程平均成绩和及格率的百分数(用"1行"显示):

企业管理(001),马克思(002),UML (003),数据库(004)

select

avg(case when cno = 1 then score end) as 平均分1,

avg(case when cno = 2 then score end) as 平均分2,

avg(case when cno = 3 then score end) as 平均分3,

avg(case when cno = 4 then score end) as 平均分4,

100 * sum(case when cno = 1 and score > 60 then 1 else 0 end) / sum(casewhen cno = 1 then 1 else 0 end) as 及格率1,

100 * sum(case when cno = 2 and score > 60 then 1 else 0 end) / sum(casewhen cno = 2 then 1 else 0 end) as 及格率2,

100 * sum(case when cno = 3 and score > 60 then 1 else 0 end) / sum(casewhen cno = 3 then 1 else 0 end) as 及格率3,

100 * sum(case when cno = 4 and score > 60 then 1 else 0 end) / sum(casewhen cno = 4 then 1 else 0 end) as 及格率4

from sc


19、查询不同老师所教不同课程平均分, 从高到低显示

select max(c.tname) as 教师, max(b.cname) 课程, avg(a.score) 平均分

from sc a, course b, teacher c

where a.cno = b.cno and b.tno = c.tno

group by a.cno

order by 平均分 desc

或者:

select r.tname as '教师',r.rname as '课程' , AVG(score) as '平均分'

from sc,

(select  t.tname,c.cno as rcso,c.cname as rname

from teacher t ,course c

where t.tno=c.tno)r

where sc.cno=r.rcso

group by sc.cno,r.tname,r.rname

order by AVG(score) desc


20、查询如下课程成绩均在第3名到第6名之间的学生的成绩:

-- [学生ID],[学生姓名],企业管理,马克思,UML,数据库,平均成绩

select top 6 max(a.sno) 学号, max(b.sname) 姓名,

max(case when cno = 1 then score end) as 企业管理,

max(case when cno = 2 then score end) as 马克思,

max(case when cno = 3 then score end) as UML,

max(case when cno = 4 then score end) as 数据库,

avg(score) as 平均分

from sc a, student b

where a.sno not in

(select top 2 sno from sc where cno = 1 order by score desc)

  and a.sno not in (select top 2 sno from sc where cno = 2 order by scoredesc)

  and a.sno not in (select top 2 sno from sc where cno = 3 order by scoredesc)

  and a.sno not in (select top 2 sno from sc where cno = 4 order by scoredesc)

  and a.sno = b.sno

group by a.sno

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

推荐阅读更多精彩内容

  • 1).创建数据库 create database学生选课数据库 2).创建四张表 Create table Stu...
    blvftigd阅读 1,514评论 0 0
  • 最近打算采用关系型数据库来理一下公司的运营数据,先拿点东西练手找感觉。下面是几个关于学生课业的表,需要建立一个数据...
    九天朱雀阅读 930评论 0 3
  • 软件测试笔试——数据库题型 1.一个简单的学生成绩表,表名Student,有字符类型的Name项和整型的score...
    天天向上的小M阅读 4,598评论 0 11
  • 一。数据库基本概念:数据、数据库。数据模型/DBMS(数据库管理系统)/DBS(数据库系统)二。数据库内部组成二维...
    S_s_s_a53f阅读 490评论 0 0
  • 50个常用的sql语句Student(S#,Sname,Sage,Ssex) 学生表Course(C#,Cname...
    哈哈海阅读 1,207评论 0 7