日期:2014-05-17  浏览次数:20628 次

请教一道课程和选课的SQL面试题
题目:
数据库中有三张表:学生表Student(studentId、studentName、studentNo),课程表course(courseId、courseName、courseNo),关系表relation(id,studentId、courseId)。
问题:
1.查出所有姓名相同的学生信息;
2.查出所有未选课学生的信息;
3.查出选所有课程的选上的信息;
4.因系统原因,现在课程表中存在重复的课程记录,请写一条sql使课程表中每门课程只保留一条记录,不考虑外键约束。

题目大致就是这个意思,请高人帮忙写一下SQL语句


    
sql 数据库 面试

------解决方案--------------------
第一题

select * from Student where studentName in (select studentName from Student  group by studentName     having count(*)>1);

第二题

select * from Student where studentid not in(select studentid from relation);

第三题

select * from Student a ,
(select studentid from relation where courseid in (select courseid from course)) b
where a.studentid = b.studentid;