avatar
One Q for DB expert# Database - 数据库
s*g
1
We have three tables as follow;
STUDENT(SID,Name,Major,Grade,age)
ENROLLMENT(SID,CourseCode,Marks)
COURSE(CourseCode,Time,RoomNumber)
What are the SQL statements for finding the names of
students who are taking all courses?
Thanks!
avatar
s*g
2
No, it doesn't work, though u r pretty close to it.:)
avatar
s*g
3
The right statements should be:
select student.name
from student
where exists
(select *
from enrollment
where student.sid=enrollment.studentnumber
group by sid
having count(enrollment.classname)in
(select count(class.name)
from class))
Thanks! BTW, I figured it out after I posted the Q. So it is
not a hole definitely.:)))

【在 s*****g 的大作中提到】
: No, it doesn't work, though u r pretty close to it.:)
avatar
a*o
4
Well, if you are sure that courseCode is unique in
enrollment and course,then use count(*) and group by would
be efficient.
But sometimes we may have one student enrolled in different
section, so that is not always true.
Following query seems clumsy, but it works. Any better way?
select s.name from student inner join enrollment e
on e.sid = s.sid where
not exists (select * from course where coursecode not
in(select coursecode from enrollment where sid = e.sid))
avatar
s*g
5
oops! sorry about it, as I changed the Q a little bit. :P
But I think it is pretty similar. You can replace class with course
and ClassName with CourseCode.:)))
avatar
s*g
6

CourseCode is unique in COURSE, but not in ENROLLMENT.
Exactly!
Unfortunatley, it doesn't work at all on my machine.:( May I know
logic behind the query?
Thanks!

【在 a****o 的大作中提到】
: Well, if you are sure that courseCode is unique in
: enrollment and course,then use count(*) and group by would
: be efficient.
: But sometimes we may have one student enrolled in different
: section, so that is not always true.
: Following query seems clumsy, but it works. Any better way?
: select s.name from student inner join enrollment e
: on e.sid = s.sid where
: not exists (select * from course where coursecode not
: in(select coursecode from enrollment where sid = e.sid))

avatar
s*g
7
I change ur query a little bit and it works on my machines now.:)
select s.name
from student s,enrollment e
where s.sid=e.sid
and not exists
(select *
from course c
where c.coursecode not in
(select coursecode
from enrollment e
where s.sid=e.sid))

But still, I don't understnad the logic behind it.:( Would you
please explain it?

【在 a****o 的大作中提到】
: Well, if you are sure that courseCode is unique in
: enrollment and course,then use count(*) and group by would
: be efficient.
: But sometimes we may have one student enrolled in different
: section, so that is not always true.
: Following query seems clumsy, but it works. Any better way?
: select s.name from student inner join enrollment e
: on e.sid = s.sid where
: not exists (select * from course where coursecode not
: in(select coursecode from enrollment where sid = e.sid))

avatar
a*o
8
Are you using Oracle? My code is Ansi sql, Oracle does not
support inner join.
the logic is if for each sid, if there's a courseid in
course table that is not in enrollment for this sid, then we
don't select it. the join to student is trivial though.
Clear?

【在 s*****g 的大作中提到】
: I change ur query a little bit and it works on my machines now.:)
: select s.name
: from student s,enrollment e
: where s.sid=e.sid
: and not exists
: (select *
: from course c
: where c.coursecode not in
: (select coursecode
: from enrollment e

avatar
s*g
9
//nod & bow
Thanks a lot, aaajoo!:)


【在 a****o 的大作中提到】
: Are you using Oracle? My code is Ansi sql, Oracle does not
: support inner join.
: the logic is if for each sid, if there's a courseid in
: course table that is not in enrollment for this sid, then we
: don't select it. the join to student is trivial though.
: Clear?

avatar
p*s
10

double negation: (using pure relational algebra conforming operations)
For each student, he/she is qualified if
there exists no such a course that
the course is not in his/her enrollment list.

SELECT S.name
FROM students S
WHERE NOT EXISTS
(SELECT *
FROM course C
WHERE NOT EXISTS
(SELECT *
FROM

【在 s*****g 的大作中提到】
: We have three tables as follow;
: STUDENT(SID,Name,Major,Grade,age)
: ENROLLMENT(SID,CourseCode,Marks)
: COURSE(CourseCode,Time,RoomNumber)
: What are the SQL statements for finding the names of
: students who are taking all courses?
: Thanks!

avatar
p*s
11

enrollment is accessed double times. The first enrollment access
in the inner join is not necessary.

【在 a****o 的大作中提到】
: Well, if you are sure that courseCode is unique in
: enrollment and course,then use count(*) and group by would
: be efficient.
: But sometimes we may have one student enrolled in different
: section, so that is not always true.
: Following query seems clumsy, but it works. Any better way?
: select s.name from student inner join enrollment e
: on e.sid = s.sid where
: not exists (select * from course where coursecode not
: in(select coursecode from enrollment where sid = e.sid))

avatar
p*s
12

Oracle supports inner join, only not the syntactical name.
ANSI SQL is majorly defined by IBM guys, so it looks much like
DB2's SQL.

【在 a****o 的大作中提到】
: Are you using Oracle? My code is Ansi sql, Oracle does not
: support inner join.
: the logic is if for each sid, if there's a courseid in
: course table that is not in enrollment for this sid, then we
: don't select it. the join to student is trivial though.
: Clear?

avatar
s*g
13
Wow! //admire & bow

【在 p****s 的大作中提到】
:
: Oracle supports inner join, only not the syntactical name.
: ANSI SQL is majorly defined by IBM guys, so it looks much like
: DB2's SQL.

avatar
a*o
14
Yeah. that's stupid.
Should be:
select s.name from student s
where
not exists (select * from course where coursecode not
in(select coursecode from enrollment e where s.sid = e.sid))

【在 p****s 的大作中提到】
:
: Oracle supports inner join, only not the syntactical name.
: ANSI SQL is majorly defined by IBM guys, so it looks much like
: DB2's SQL.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。