sql语句查询没有被学生选修过的课程(即课程号没有在sc表中出现过)的名称.

sql语句查询没有被学生选修过的课程(即课程号没有在sc表中出现过)的名称.

1、创建学生及课程表,

create table test_student(stu_id number, class_id number);

create table test_class(class_id number, class_name varchar2(30));

2、插入测试数据,

insert into test_student values(1,1001);

insert into test_student values(2,1001);

insert into test_student values(3,1002);

insert into test_student values(4,1003);

insert into test_student values(5,1003);

insert into test_student values(6,1003);

insert into test_class values(1001,'美术');

insert into test_class values(1002,'音乐');

insert into test_class values(1003,'绘画');

insert into test_class values(1004,'跆拳道');

3、查询学生选修的课程,

select * from test_student t, test_class b where t.class_id = b.class_id

4、查询无学生选修的课程,可以发现1004 跆拳道课程,无人选择,


select b.*

  from test_student t, test_class b

 where t.class_id(+) = b.class_id

   and t.class_id is null

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-10-04
1)
select a.课程 from 课程表 a where  not exists(
select 1 from sc b where b.课程号=a.课程号);

2)
select distinct a.课程 from 课程表 a left join sc b
on a.课程号=b.课程号 where b.课程号 is null;

请按实际调整代码中的表名和字段名。

如果碰到大数据表时在有可利用的索引情况下推荐语句1其效率很高,否则语句1可能效率低下,这种情况下应采用语句2。本回答被提问者采纳
第2个回答  2018-05-03
SELECT A.CNO
FROM SC A
WHERE
NOT EXISTS
(SELECT B.CNO
FROM SC B
WHERE A.CNO=B.CNO);
相似回答