45道SQL面试题(网页版)

create database school;

#学生表

create table `Student`

(

  `Sno` varchar(20) not null COMMENT '人名',

  `Sname` varchar(20) not null COMMENT '姓名',

  `Ssex` varchar(20) not null COMMENT '性别',

  `Sbirthday` datetime COMMENT '出生日期',

  `Class` varchar(20) COMMENT '班级'

);

#课程表

create table `Course`(

  `Cno` varchar(20) not null COMMENT '课程号',

  `Cname` varchar(20) not null COMMENT '课程名称',

  `Tno` varchar(20) not null COMMENT '教工编号'

);

#成绩表

Create table `Score` (

  `Sno` varchar(20) not null COMMENT '学号',

  `Cno` varchar(20) not NULL comment '课程号',

  `Degree` DECIMAL(4,1) null COMMENT '成绩'

);

#教师表

create table `Teacher` (

  `Tno` varchar(20) not null COMMENT '教工编号',

  `Tname` varchar(20) not null COMMENT '教工姓名',

  `Tsex` varchar(20) not null COMMENT '教工性别',

  `Tbirthday` datetime null COMMENT '教工出生日期',

  `Prof` varchar(20) null COMMENT '职称',

  `Depart` varchar(20) null COMMENT '教工所在部门'

);

insert into `Student` value

(108,'曾华','男','1977-09-01',95033),

(105,'匡明','男','1975-10-02',95031),

(107,'王丽','女','1976-01-23',95033),

(101,'李军','男','1976-02-20',95033),

(109,'王芳','女','1975-02-10',95031),

(103,'陆军','男','1974-06-03',95031);

insert into `Course` value

("3-105","计算机导论",825),

("3-245","操作系统",804),

("6-166","数字电路",856),

("9-888","高等数学",831);

insert into `Score` value

(103,"3-245",86),

(105,"3-245",75),

(109,"3-245",68),

(103,"3-105",92),

(105,"3-105",88),

(109,"3-105",76),

(101,"2-105",64),

(107,"3-105",91),

(108,"3-105",78),

(101,"6-166",85),

(107,"6-166",79),

(108,"6-166",81);

insert into `Teacher` value

(804,'李城','男','1958-12-02','副教授','计算机系'),

(856,'张旭','男','1969-03-12','讲师','电子工程系'),

(825,'王萍','女','1972-05-05','助教','计算机系'),

(831,'刘冰','女','1977-08-14','助教','电子工程系');

1查询Student表中的所有记录的Sname、Ssex和Class列。

select sname,ssex,class from student;

2查询教师所有的单位即不重复的Depart列

select distinct depart from teacher;

3查询Student表的所有记录

select * from student;

4查询Score表中成绩在60到80之间的所有记录。

select * from score where degree>=60 and degree<=80;

或者:select * from score where degree between 60 and 80;

5查询Score表中成绩为85,86或88的记录。

select * from score where degree=85 or degree=86 or degree=88;

或者:select * from score where degree in (85,86,88);

6查询Student表中“95031”班或性别为“女”的同学记录。

select * from student where class='95031' or ssex="女";

7以Class降序查询Student表的所有记录

select * from student order by class desc;

8以Cno升序、Degree降序查询Score表的所有记录。

select * from score order by degree desc,cno asc;

9查询"95031"班的学生人数.

select count(1) from student where class='95031';

10查询Score表中的最高分的学生学号和课程号。(子查询或者排序)

select sno,cno from score where degree=(select max(degree) from score);

或者:select sno,cno from score order by degree desc limit 1;

11查询每门课的平均成绩。(group by:根据Cno进行分组)

select cno,avg(degree) as 平均分 from score group by cno;

12查询Score表中至少有5名学生选修的并以3开头的课程的平均分数.(单表查询)

select cno,avg(degree) from score where cno like “3-%” group by cno having count(cno)>=5;

或者:select avg(degree) from score where cno like '3-%' and cno in (select cno from score group by cno having count(*)>=5);

13查询分数大于70,小于90的Sno列。(单表查询)

select sno from score where degree >70 and degree < 90;

或者:select sno from score where degree between 70 and 90;

14查询所有学生的Sname、Cno和Degree列.(多表查询)

select b.sname,a.cno,a.degree from score a inner join student b on a.sno=b.sno;

或者:select sname,cno,degree from student,score where student.sno=score.sno;

15查询所有学生的Sno、Cname和Degree列。(3表连接查询)

select a.sno,b.degree,c.cname from student a inner join score b on a.sno=b.sno

left join course c on b.cno=c.cno;

或者:select sname,cname,degree from student,course,score where student.sno=score.sno and course.cno=score.cno;

16查询所有学生的Sname、Cname和Degree列.(3表连接查询)

select a.sname,c.cname,b.degree from student a,score b,course c where a.sno=b.sno and b.cno=c.cno;

或者:select a.sname,c.cname,b.degree from student a inner join score b on a.sno=b.sno left join course c on b.cno=c.cno;

17查询“95033”班学生的平均分.(2表连接查询)

select avg(degree) from score,student where student.sno=score.sno and slass='95033';

或者:select avg(degree) from student a inner join score b on a.sno=b.sno and a.class='95033';

或者:select avg(degree) from student a inner join score b on a.sno=b.sno where class='95033';

或者:select avg(degree) from score where sno in (select sno from student where class='95033');

18假设使用如下命令建立了一个grade表:

create table grade(low  int(3),upp  int(3),rank  char(1));

insert into grade values(90,100,"A");

insert into grade values(80,89,"B");

insert into grade values(70,79,"C");

insert into grade values(60,69,"D");

insert into grade values(0,59,"E");

查询所有同学的Sno、Cno和rank列(成绩排名).

select Sno,Cno,rank from Score,grade where Degree between low and upp;

这道题等同于[0,60)为E,[60,70)为D,[70,80)为C,(80,90]为B,(90,100]为A;用case when

select

   sno,cno,

   case when degree<60 and degree>=0 then "E"

        when degree>=60 and degree<70 then "D"

        when degree>=70 and degree<80 then "C"

        when degree>=80 and degree<90 then "B"

        when degree>=90 and degree<100 then "A" end as rank

from score;

19查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。

select * from score where cno='3-105' and degree>(select degree from score where cno='3-105' and sno='109');

20查询score表中选学多门课程的同学中分数为非最高分成绩的记录

select * from score a where sno in (select sno from score group by sno having count(*)>1) and degree<( select max(degree ) from score b where b.cno = a.cno);

或者:select * from score a where Sno in (select Sno from score group by Sno having count(*)>1) and degree not in (select max(degree ) from Score b where b.cno = a.cno);

21查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。

select * from score where degree >(select degree from score where sno="109" and cno="3-105");

22查询和学号为108、101的同学同年出生的所有学生的Sno、Sname和Sbirthday列

select sno,sname,sbirthday from student where year(sbirthday) in (select year(sbirthday) from student where sno="108" or sno="101");

23查询“张旭“教师任课的学生成绩。

第一种方法:select sno,degree from Score where cno in (select sno,degree from score where tno in (select tno from teacher where tname = '张旭'));

第二种方法:select sno,degree from score a,course b,teacher c where a.cno=b.cno and b.tno=c.tno and tname = '张旭';

第三种方法:select a.sno,b.degree from student a left join score b on a.sno=b.sno

left join course c on b.cno=c.cno left join teacher d on c.tno=d.tno where d.tname="张旭";

24查询选修某课程的同学人数多于5人的教师姓名。

第一种方法:select tname from teacher where tno in (select tno from course where cno in (select cno from score group by cno having count(1)>=5));

第二种方法:select tname from teacher a,course b,score c where a.tno=b.tno and b.cno=c.cno group by c.cno having count(c.sno)>=5;

25查询95033班和95031班全体学生的记录。

第一种方法(复杂):select * from student a,score b,course c,teacher d where a.sno=b.sno and b.cno=c.cno and c.tno=d.tno and class = '95033' or class= '95031' group by a.sno;

第二种方法(简单):select * from student where class="95033" or class="95031";

26查询存在有85分以上成绩的课程Cno.

第一种方法:select cno from score where degree>=85 group by cno;

第二种方法:select distinct cno from score where degree>=85;

27查询出“计算机系“教师所教课程的成绩表.

第一种方法:select * from score where cno in (select cno from course where tno in (select tno from teacher where depart="计算机系" ));

第二种方法:select a.* from score a,course b,teacher c where a.cno=b.cno and b.tno=c.tno and depart="计算机系";

28查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。

select Tname,Prof from Teacher where Prof not in (

select Prof from Teacher where Prof in (select Prof from Teacher where Depart = '计算机系') and Depart = '电子工程系');

select Tname,Prof from Teacher where Prof not in (select Prof from Teacher where Depart = '计算机系' or Depart ='电子工程系' group by Prof having count(*)>1);

select Tname,Prof from Teacher where Depart ='计算机系' and Prof  not in( select Prof from Teacher where Depart ='电子工程系')

union

select Tname,Prof from Teacher where Depart ='电子工程系' and Prof  not in( select Prof from Teacher where Depart ='计算机系');

29查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。

any:代表括号中任意一个成绩就可以

第一种方法:select cno,sno,degree from score where cno="3-105" and degree >any(select degree from score where cno="3-245") order by degree desc; 

第二种方法:select cno,sno,degree from score where cno = '3-105' and degree >

(select min(degree) from score where cno = '3-245') order by degree desc;

30查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree。

all:代表括号中的所有成绩

第一种方法:select cno,sno,degree from score where cno="3-105" and degree >(select max(degree) from score where cno="3-245") order by degree desc;

第二种方法:select cno,sno,degree from score where cno="3-105" and degree >all(select degree from score where cno="3-245") order by degree desc;

31查询所有教师和同学的name、sex和birthday.

第一种union:select Sname as ”姓名“,Ssex as "性别",Sbirthday as "出生日期" from Student

union

select Tname,Tsex,Tbirthday from Teacher;

第二种要以teacher表为主表连接

select d.tname,a.sname,d.tsex,a.ssex,d.tbirthday,a.sbirthday

from teacher d left join course c on c.tno=d.tno left join score b on b.cno=c.cno

left join student a on a.sno=b.sno;

32查询所有“女”教师和“女”同学的name、sex和birthday。

select sname as name,ssex as sex,sbirthday as birthday from student where ssex="女"

union

select tname,tsex,tbirthday from teacher where tsex="女";

33查询成绩比该课程平均成绩低的同学的成绩表。

第一种方法:select * from score a where degree <(select avg(degree) as avg_score from score b where a.cno=b.cno);

第二种方法:select * from score a left join

(select cno,sno,avg(degree) as avg_score from score b group by cno) b  on a.cno=b.cno

where a.degree

34查询所有任课教师的Tname和Depart.

第一种方法:select tname,depart from teacher; 

第二种方法:select tname,depart from teacher where tno in (select tno from course);

35查询所有未讲课的教师的Tname和Depart.

select tname,depart from teacher where tno not in (select tno from course where cno in (select cno from score));

36查询至少有2名男生的班号.

select class from student where ssex="男" group by class having count(ssex)>=2;

select class from student where ssex="男" group by class having count(1)>1;

37查询Student表中不姓“王”的同学记录.

select * from student where sname not like "王%%";

38查询Student表中每个学生的姓名和年龄。

第一种方法:select sname,timestampdiff(year,sbirthday,now()) as age from student;

第二种方法:select sname,year(now())-year(sbirthday) as age from student;

39查询Student表中最大和最小的Sbirthday日期值。

select Max(sbirthday),Min(sbirthday) from student;

40以班号和年龄从大到小的顺序查询Student表中的全部记录。

第一种方法:select *,timestampdiff(year,sbirthday,now()) as age from student order by age,class desc;

第二种方法:select * from student order by class desc,sbirthday;

41查询“男”教师及其所上的课程

第一种方法:select * from course where tno in (select tno from teacher where tsex="男");

第二种方法:select tname,cname from course a,teacher b where a.tno=b.tno and b.tsex="男";

第三种:select * from course a left join teacher b on a.tno=b.tno where b.tsex="男";

42查询最高分同学的Sno、Cno和Degree列。

第一种方法:select sno,cno,degree from score where degree=(select max(degree) from score);

第二种方法:select  Sno,Cno,Degree from score order by degree desc limit 0,1;

扩展:查询各科最高分的同学的sno,cno和degree.

select sno,cno,degree from score a where degree=(select max(degree) from score b where a.cno=b.cno);

43查询和“李军”同性别的所有同学的Sname.

select sname from student where ssex=(select ssex from student where sname="李军");

44查询和“李军”同性别并同班的同学Sname.

select sname from student where ssex=(select ssex from student where sname="李军") and class=(select class from student where sname="李军");

45查询所有选修“计算机导论”课程的“男”同学的成绩表。

第一种方法:select * from score where cno in (select cno from course where cname="计算机导论" and sno in (select sno from student where ssex="男"));

第二种方法:select * from course a left join score b on a.cno=b.cno left join student c

on b.sno=c.sno where a.cname="计算机导论" and c.ssex="男";

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

推荐阅读更多精彩内容