四、数据库及SQL部分--《面试题集》
四、数据库及SQL部分:(共4题:基础3道,中等难度1道)106、有3个表(15分钟):【基础】
Student 学生表 (学号,姓名,性别,年龄,组织部门)
Course 课程表 (编号,课程名称)
Sc 选课表 (学号,课程编号,成绩)
表结构如下:
1)写一个SQL语句,查询选修了’计算机原理’的学生学号和姓名(3分钟)
2)写一个SQL语句,查询’周星驰’同学选修了的课程名字(3分钟)
3)写一个SQL语句,查询选修了5门课程的学生学号和姓名(9分钟)
答:1)SQL语句如下:
select stu.sno, stu.sname from Student stu
where (selectcount(*) from sc where sno=stu.sno and cno =
(select cno from Course where cname='计算机原理')) != 0;
2)SQL语句如下:
select cnamefrom Course
where cno in (select cno from sc where sno =
(select sno from Student where sname='周星驰'));
3)SQL语句如下:
selectstu.sno, stu.sname from student stu
where (selectcount(*) from sc where sno=stu.sno) = 5;
107、有三张表,学生表S,课程C,学生课程表SC,学生可以选修多门课程,一门课程可以被多个学生选修,通过SC表关联。【基础】
1)写出建表语句;
2)写出SQL语句,查询选修了所有选修课程的学生;
3)写出SQL语句,查询选修了至少5门以上的课程的学生。
答:1)建表语句如下(mysql数据库):
create table s(id integer primary key, namevarchar(20));
create tablec(id integer primary key, name varchar(20));
create tablesc(
sid integer references s(id),
cid integer references c(id),
primary key(sid,cid)
);
2)SQL语句如下:
select stu.id,stu.name from s stu
where(select count(*) from sc where sid=stu.id)
=(select count(*) from c);
3)SQL语句如下:
select stu.id,stu.name from s stu
where(select count(*) from sc where sid=stu.id)>=5;
108、数据库表(Test)结构如下:【基础】
ID NAME AGE MANAGER(所属主管人ID)
106 A 30 104
109 B 19 104
104 C 20 111
107 D 35 109
112 E 25 120
119 F 45 NULL
要求:列出所有年龄比所属主管年龄大的人的ID和名字?
答:SQL语句如下:
select employee.name from test employee
whereemployee.age > (select manager.age from test manager
where manager.id=employee.manager);
109、有如下两张表:【中等难度】
表city: 表state:
CityNo
CityName
StateNo
BJ
北京
(Null)
SH
上海
(Null)
GZ
广州
GD
DL
大连
LN
StateNo
State Name
GD
广东
LN
辽宁
SD
山东
NMG
内蒙古
欲得到如下结果:
City No CityName State No State Name
BJ 北京 (Null) (Null)
DL 大连 LN 辽宁
GZ 广州 GD 广东
SH 上海 (Null) (Null)
写相应的SQL语句。
答:SQL语句为:
SELECT C.CITYNO, C.CITYNAME, C.STATENO,S.STATENAME
FROM CITY C,STATE S
WHEREC.STATENO=S.STATENO(+)
ORDERBY(C.CITYNO);
页:
[1]