If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Query in SQL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-29-03, 03:54
SAl SAl is offline
Registered User
 
Join Date: Sep 2003
Posts: 15
Cool Query in SQL

Greetings

I'm brand new in using SQL , so I wish to get help here .

how to find all students names who had taken a course from each professor outside the CS Department ???

Last edited by SAl; 09-29-03 at 14:08.
Reply With Quote
  #2 (permalink)  
Old 09-30-03, 05:58
SAl SAl is offline
Registered User
 
Join Date: Sep 2003
Posts: 15
.. Any Answer ???
Reply With Quote
  #3 (permalink)  
Old 09-30-03, 06:22
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
sounds like homework

what's your best attempt so far?

rudy
Reply With Quote
  #4 (permalink)  
Old 09-30-03, 12:46
mhozey mhozey is offline
Registered User
 
Join Date: Apr 2003
Location: Columbia, SC
Posts: 7
What have you got so far? What is your train of thought?
Reply With Quote
  #5 (permalink)  
Old 10-05-03, 06:08
SAl SAl is offline
Registered User
 
Join Date: Sep 2003
Posts: 15
This what I got ..

SELECT studid

FROM Transcript, Teaching
WHERE Transcript.crscode = Teaching.crscode And ( SELECT id FROM professor, teaching WHERE professor.id = Teaching.profid AND professor.dept <> 'CS')


???

Last edited by SAl; 10-06-03 at 02:39.
Reply With Quote
  #6 (permalink)  
Old 10-06-03, 02:42
SAl SAl is offline
Registered User
 
Join Date: Sep 2003
Posts: 15
Here is another attempt :

Select T.Crscode,Trans.stdid.P.Id
From professor p , Teaching T, Transcript Tans
Where p.Iddept <> 'CS'
Reply With Quote
  #7 (permalink)  
Old 10-06-03, 07:48
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
you need to be a bit more careful in your typing -- sql, like other languages, is very unforgiving about typos

you have a period between Trans.stdid and P.Id in the SELECT clause, and it should be a comma

also, you alias the Transcript table to Tans but then use Trans in the SELECT list

you said the problem called for student names, so you still need to join to the student table, and then use only SELECT DISTINCT student.name because the same student will take multiple classes from professors that aren't 'CS" but you want each student name to show up only once
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 10-08-03, 07:09
SAl SAl is offline
Registered User
 
Join Date: Sep 2003
Posts: 15
Hi Rudy ,

Thanx a lot for your notes
Here is My correction :


SELECT DISTINCT s.name
From Student S,
Where
( Select T.Crscode, Trans.stdid, P.Id
From professor p , Teaching T, Transcript Trans
Where p.Iddept <> 'CS'
)

Am I right now ???
Reply With Quote
  #9 (permalink)  
Old 10-08-03, 07:23
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
nope, even more diligence is required

there's a comma in front of WHERE

the subquery inside the parentheses will return an entire result set of rows, and this cannot be a condition of WHERE

you need to join the student table to the other tables


rudy
Reply With Quote
  #10 (permalink)  
Old 10-08-03, 07:38
SAl SAl is offline
Registered User
 
Join Date: Sep 2003
Posts: 15
OK


SELECT DISTINCT s.name
From Student S
Where
( Select T.Crscode, Trans.stdid, P.Id
From professor p , Teaching T, Transcript Trans
Where p.Iddept <> 'CS'
)

What about the Join ?? How can I use it here ??
Reply With Quote
  #11 (permalink)  
Old 10-08-03, 07:43
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
you still have that syntax problem in the WHERE clause

the WHERE clause needs to have one or more conditions that evealuate true or false

a subselect consisting of a result set of rows is not a condition

see Getting the Right Data with SQL Joins
Reply With Quote
  #12 (permalink)  
Old 10-08-03, 07:51
SAl SAl is offline
Registered User
 
Join Date: Sep 2003
Posts: 15
AHA


SELECT DISTINCT s.name
From Student S, Teaching T, professor p

Where T.profid = P.Id AND
( Select T.Crscode, Trans.stdid, P.Id
From professor p , Teaching T, Transcript Trans
Where p.Iddept <> 'CS'
)
Reply With Quote
  #13 (permalink)  
Old 10-08-03, 07:54
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
you have a cross-join there -- in order to make it an inner join, you will have to add join conditions, e.g. S.teachid=T.id

and you still have an invalid WHERE condition

i'm through for this morning, i have to go to work now
Reply With Quote
  #14 (permalink)  
Old 10-08-03, 07:57
SAl SAl is offline
Registered User
 
Join Date: Sep 2003
Posts: 15
Thanx for the link Rudy , This is my last try and I think it will be more accurate:

SELECT DISTINCT s.name
From Student S, Teaching T, professor p

Where (T.profid = P.Id) AND (T.studid = S.id) And
( Select T.Crscode, Trans.stdid, P.Id
From professor p , Teaching T, Transcript Trans
Where p.Iddept <> 'CS'
)
Reply With Quote
  #15 (permalink)  
Old 10-08-03, 07:59
SAl SAl is offline
Registered User
 
Join Date: Sep 2003
Posts: 15
Thanx alot Rudy And have a nice time
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On