几道数据库的面试题或笔试题 难度适中

数据库

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;

同类其他面试题 点击新一篇或旧一篇可浏览全部同类面试题

新一篇:
旧一篇:

你有答案? 你对以上面试题有意见? 你想发表你的见解? 写下来吧!你的分享将会让很多人受益!

相关面试题

·数据库选择题
·sql查询数据库最后10条记录并按降序排列
·数据库笔试题
·SQL面试题
·大量数据并发的情况下 ,不用自增列 怎样设置主键

版权声明:本站大部分内容为原创! 另有少部分内容整理于网络,如需转载本站内容或关切版权事宜请联系站长。未经允许,严禁复制转载本站内容,否则将追究法律责任。 本站欢迎与同类网站建立友情链接,请联系QQ:176687814