设为首页收藏本站

嘻皮客娱乐学习网

 找回密码
 中文注册
搜索
打印 上一主题 下一主题
开启左侧

[面试题库] 几道数据库的面试题或笔试题

[复制链接]
跳转到指定楼层
楼主
发表于 2014-11-30 13:16:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.张表,学生表S,课程C,学生课程表SC,学生可以选修多门课程,一门课程可以被多个学生选修,通过SC表关联;(SQL)
1)写出建表语句;
答:建表语句如下(mysql数据库):
create table s(id integer primary key, name varchar(20));
create table c(id integer primary key, name varchar(20));
create table sc(
sid integer references s(id),
cid integer references c(id),
primary key(sid,cid)
);
2)写出SQL语句,查询选修了所有选修课程的学生;
答:SQL语句如下:
select stu.id, stu.name from s stu
where (select count(*) from sc where sid=stu.id) =
(select count(*) from c);
3)写出SQL语句,查询选修了至少5门以上的课程的学生。
答:SQL语句如下:
select stu.id, stu.name from s stu
where (select count(*) from sc where sid=stu.id)>=5;

2.数据库表(Test)结构如下:(SQL)
IDNAMEAGEMANAGER(所属主管人ID)
106A30104
109B19104
104C20111
107D35109
112E25120
119F45NULL
要求:列出所有年龄比所属主管年龄大的人的ID和名字?
答:SQL语句如下:
select employee.name from test employee where employee.age>
(select manager.age from test manager where manager.id=employee.manager);

3.有3个表(15分钟):(SQL)
Student 学生表 (学号,姓名,性别,年龄,组织部门)
Course 课程表 (编号,课程名称)
Sc 选课表 (学号,课程编号,成绩)
表结构如下:

1)写一个SQL语句,查询选修了’计算机原理’的学生学号和姓名(3分钟)
答:SQL语句如下:
select stu.sno, stu.sname from Student stu
where (select count(*) from sc where sno=stu.sno and cno =
(select cno from Course where cname=’计算机原理’)) != 0;
2)写一个SQL语句,查询’周星驰’同学选修了的课程名字(3分钟)
答:SQL语句如下:
select cname from Course where cno in (select cno from sc where sno=(select sno from Student where sname=’周星驰’));
3)写一个SQL语句,查询选修了5门课程的学生学号和姓名(9分钟)
答:SQL语句如下:
select stu.sno, stu.sname from student stu
where (select count(*) from sc where sno=stu.sno) = 5;
回复

使用道具 举报

小黑屋|手机版|嘻皮客网 ( 京ICP备10218169号|京公网安备11010802013797  

GMT+8, 2024-5-13 16:56 , Processed in 0.171435 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表