首页 > 试题广场 >

现在查询所有及格学生的学生姓名,下面正确的是?

[单选题]
分数表scores设计如下:
courseID(课程编号)
studentID(学生编号)
score(分数)
另有一个学生信息表student,包含studentID,sname(学生姓名)。
已知并非所有学生都参加了考试,现在查询所有及格学生的学生姓名,下面正确的是?
  • select sname from student where studentID in (select studentID from score where score>60)
  • select sname from student where studentID = (select studentID from score where score>60)
  • select sname from student where studentID not in (select studentID from score where score<=60)
  • select sname from student where exists (select studentID from score where score>60)
A正确
B =不属于数据库关系代数的操作。只有并交差积投影选择和连接
C会查询出考试合格和没有去考试的学生名字
D少连接条件。这样确实在分数表里找出所有及格的人,但是没有用studentID把两个表连接起来。
编辑于 2015-11-11 16:23:36 回复(0)
A,D的区别在于效率,由于in操作是进行数据的对比,id in (1,2)等价于id=1 or id=2,所以当结果集较大时,in不适合,应该选用exists。
此处A也可以执行,但是考虑效率,应该选D(虽然这个表明显也不是那么大。。。)
发表于 2019-08-08 15:28:29 回复(1)
A,B用=不对,C要注意并不是所有学生都参加了考试,D的exists只返回True 或False
发表于 2015-07-15 08:53:12 回复(0)
drop table if exists student111;
create table student111 (
    student_ID varchar(10),
    sname varchar(10)
);
drop table if exists scores;
create table scores (
    course_ID varchar(10),
    student_ID varchar(10),
    score INT
);
insert into student111 values ('123', 'a'),('124', 'b'),('125', 'c'),('126','d');
insert into scores(course_ID,student_ID,score) values('math', '123', 95),('math', '124', 55),('math','125', 60);

select sname from student111 where exists (select student_ID from scores where score>60);

select sname from student111 where student_ID in (select student_ID from scores where score>60);

我的代码哪里写错了吗。感觉ABCD都是错的。A错就错在没有等号,这样60分的就不算及格了!但D也没有等号啊!而且我运行D,D会把所有数据都列举出来,根本没有筛选!
发表于 2019-07-27 23:43:53 回复(2)
select sname from student where studentID in (select studentID from score where score>60)
这个错在哪里了啊,为什么不选它啊。我套你猴子~~~~~
发表于 2019-07-25 21:56:38 回复(0)
选择A 子查询查出来的是满足分数及格的所有学生的ID 之后的查询是查询这些ID所对应的姓名
发表于 2015-11-10 18:07:18 回复(0)
比如in(1,2) 就是 = 1 or = 2的一种简单写法,所以一般在元素少的时候使用IN,如果多的话就用exists指定一个子查询。
发表于 2014-11-22 10:54:20 回复(0)