有三张表,学生表S,课程C,学生课程表SC,学生可以选修多门课程,一门课程可以被多个学生选修,通过SC表关联。

  1. 写出建表语句;
  2. 写出SQL语句,查询选修了所有选修课程的学生;
  3. 写出SQL语句,查询选修了至少5门以上的课程的学生。

参考解答

1. 建表语句

create table s(
    id number primary key, 
    name varchar2(20)
);
create table c(
    id number primary key, 
    name varchar2(20)
);
create table sc(
    sid number references s(id), 
    cid number references c(id), 
);

2. 选修了所有选修课程的学生

select stu.id, stu.name from s stu where 
    (select count(*) from sc where sid=stu.id) 
    = (select count(*) from c);

3. 选修了至少5门以上的课程的学生

 select stu.id, stu.name from s stu where 
    (select count(*) from sc where sid=stu.id)>=5;

results matching ""

    No results matching ""