日期:2014-05-16  浏览次数:20385 次

四、数据库及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 (select count(*) from sc where sno=stu.sno and cno =

??????? ???? (select cno from Course where cname='计算机原理')) != 0;

??? 2SQL语句如下

??? select cname from Course

??? where cno in ( select cno from sc where sno =

????????? ????????(select sno from Student where sname='周星驰'));

??? 3SQL语句如下

??? select stu.sno, stu.sname from student stu

??? where (select count(*) 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, 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)

??? ?);

??? 2SQL语句如下