由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - One Q for DB expert
相关主题
[转载] Database Interface in ANSI C++?如何判断一行是否存在的问题。
A sql questionquestion: copy first N rows from table B to table A (DB2)
请问如何实现这样一个db2的query, 谢谢包子请教query
请教各位大侠呀.[求助] 问一个Kettle从DB2抽取到SQL2012的问题
再问not exist和not inT-SQL Row Concatenate with a Twist??
有人给大包子了SQL2005 如何獲取所有 user login database mapping
讨论:在SELECT中限制TOP N条纪录SQL 请教
SQL question请教一个问题
相关话题的讨论汇总
话题: select话题: coursecode话题: enrollment话题: where话题: course
进入Database版参与讨论
1 (共1页)
s*****g
发帖数: 17
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!
s*****g
发帖数: 17
2
No, it doesn't work, though u r pretty close to it.:)
s*****g
发帖数: 17
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.:)
a****o
发帖数: 37
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))
s*****g
发帖数: 17
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.:)))
s*****g
发帖数: 17
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))

s*****g
发帖数: 17
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))

a****o
发帖数: 37
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

s*****g
发帖数: 17
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?

p****s
发帖数: 3184
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!

p****s
发帖数: 3184
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))

p****s
发帖数: 3184
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?

s*****g
发帖数: 17
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.

a****o
发帖数: 37
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.

1 (共1页)
进入Database版参与讨论
相关主题
请教一个问题再问not exist和not in
existence dependence 和 relationship strength 的关系有人给大包子了
Help讨论:在SELECT中限制TOP N条纪录
How to use the SQL key word EXISTS?SQL question
[转载] Database Interface in ANSI C++?如何判断一行是否存在的问题。
A sql questionquestion: copy first N rows from table B to table A (DB2)
请问如何实现这样一个db2的query, 谢谢包子请教query
请教各位大侠呀.[求助] 问一个Kettle从DB2抽取到SQL2012的问题
相关话题的讨论汇总
话题: select话题: coursecode话题: enrollment话题: where话题: course