Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Basic Select Question

Author  Topic 

jtrumbul
Starting Member

11 Posts

Posted - 2008-05-26 : 22:46:40
Hello All,

I have what i believe to be a pretty basic SQL question. I have 2 tables, One called participant, and another called cnAvailableParticipant_List they are related with a field called userid I would like to select the firstname, lastname, userid, fields from the participant table and the dnnuserid field from the cnAvailableParticipant_List table where the users have a record in the participant table but do NOT have a record in the other table. Basicially i want to select all the users who are essentially not checked in. So far i have:

SELECT p.ParticipantID as ID, p.FName as FName, p.LName as LName, ap.DNNID FROM Participant p, cnAvailableParticipant_List ap
WHERE
ORDER BY LName, FName

can someone explain to me what i need in the where clause and why. I don't just want the answer, i would like to know why it works the way it does as well. Sorry for the basic question but im new to SQL

Thanks all help is appreciated,
John

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2008-05-26 : 22:49:43
Then you are better to read about join in books online.
Go to Top of Page

jtrumbul
Starting Member

11 Posts

Posted - 2008-05-26 : 22:52:03
quote:
Originally posted by rmiao

Then you are better to read about join in books online.



I will do, however it is very time critical i need this done by 9am. I have looked up joining in select statements online, and its still giving me trouble. I don't get why when i imagine this where should be relitively simple no?
Go to Top of Page

sodeep
Master Smack Fu Yak Hacker

7174 Posts

Posted - 2008-05-26 : 23:27:23
Like this:

select column1, column2
from tableA
where not exists (select * from tableB
where tableA.column1 = tableB.column1
and tableA.column2=tableB.column2)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-27 : 00:39:29
quote:
Originally posted by jtrumbul

Hello All,

I have what i believe to be a pretty basic SQL question. I have 2 tables, One called participant, and another called cnAvailableParticipant_List they are related with a field called userid I would like to select the firstname, lastname, userid, fields from the participant table and the dnnuserid field from the cnAvailableParticipant_List table where the users have a record in the participant table but do NOT have a record in the other table. Basicially i want to select all the users who are essentially not checked in. So far i have:

SELECT p.ParticipantID as ID, p.FName as FName, p.LName as LName, ap.DNNID FROM Participant p, cnAvailableParticipant_List ap
WHERE
ORDER BY LName, FName

can someone explain to me what i need in the where clause and why. I don't just want the answer, i would like to know why it works the way it does as well. Sorry for the basic question but im new to SQL

Thanks all help is appreciated,
John


SELECT p.ParticipantID as ID, p.FName as FName, p.LName as LName, ap.DNNID 
FROM Participant p
LEFT JOIN cnAvailableParticipant_List ap
ON ap.LinkingColumn=p.LinkingColumn
WHERE ap.LinkingColumn IS NULL
ORDER BY p.LName, p.FName

However i didnt understand the purpose of second part. What is the purpose of returning the value of dnnuserid field from the cnAvailableParticipant_List table when you're looking at records having no matching value in cnAvailableParticipant_List table?the value returned for that field will always be NULL
Go to Top of Page

jtrumbul
Starting Member

11 Posts

Posted - 2008-05-27 : 08:02:23
quote:
Originally posted by visakh16

quote:
Originally posted by jtrumbul

Hello All,

I have what i believe to be a pretty basic SQL question. I have 2 tables, One called participant, and another called cnAvailableParticipant_List they are related with a field called userid I would like to select the firstname, lastname, userid, fields from the participant table and the dnnuserid field from the cnAvailableParticipant_List table where the users have a record in the participant table but do NOT have a record in the other table. Basicially i want to select all the users who are essentially not checked in. So far i have:

SELECT p.ParticipantID as ID, p.FName as FName, p.LName as LName, ap.DNNID FROM Participant p, cnAvailableParticipant_List ap
WHERE
ORDER BY LName, FName

can someone explain to me what i need in the where clause and why. I don't just want the answer, i would like to know why it works the way it does as well. Sorry for the basic question but im new to SQL

Thanks all help is appreciated,
John


SELECT p.ParticipantID as ID, p.FName as FName, p.LName as LName, ap.DNNID 
FROM Participant p
LEFT JOIN cnAvailableParticipant_List ap
ON ap.LinkingColumn=p.LinkingColumn
WHERE ap.LinkingColumn IS NULL
ORDER BY p.LName, p.FName

However i didnt understand the purpose of second part. What is the purpose of returning the value of dnnuserid field from the cnAvailableParticipant_List table when you're looking at records having no matching value in cnAvailableParticipant_List table?the value returned for that field will always be NULL



It worked like a charm. You are correct in your question, what i meant to write was p.UserID as DNNID not select it from the ap.table. Sorry it was late and i was tired as all hell. Now with this query reading it still confuses me a bit. when you say where ap.linking column is null makes me think it would select records that exists in both tables only where that column from the ap table is null? i see it is working properly but if you get a second do you think you can break down the query for me as to why? im sure its annoying but as i stated above im as interested in why the answer works as opposed to just getting it working.

thanks for all the help i appreciate it much.
-John
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-27 : 08:23:02
quote:
Originally posted by jtrumbul

quote:
Originally posted by visakh16

quote:
Originally posted by jtrumbul

Hello All,

I have what i believe to be a pretty basic SQL question. I have 2 tables, One called participant, and another called cnAvailableParticipant_List they are related with a field called userid I would like to select the firstname, lastname, userid, fields from the participant table and the dnnuserid field from the cnAvailableParticipant_List table where the users have a record in the participant table but do NOT have a record in the other table. Basicially i want to select all the users who are essentially not checked in. So far i have:

SELECT p.ParticipantID as ID, p.FName as FName, p.LName as LName, ap.DNNID FROM Participant p, cnAvailableParticipant_List ap
WHERE
ORDER BY LName, FName

can someone explain to me what i need in the where clause and why. I don't just want the answer, i would like to know why it works the way it does as well. Sorry for the basic question but im new to SQL

Thanks all help is appreciated,
John


SELECT p.ParticipantID as ID, p.FName as FName, p.LName as LName, ap.DNNID 
FROM Participant p
LEFT JOIN cnAvailableParticipant_List ap
ON ap.LinkingColumn=p.LinkingColumn
WHERE ap.LinkingColumn IS NULL
ORDER BY p.LName, p.FName

However i didnt understand the purpose of second part. What is the purpose of returning the value of dnnuserid field from the cnAvailableParticipant_List table when you're looking at records having no matching value in cnAvailableParticipant_List table?the value returned for that field will always be NULL



It worked like a charm. You are correct in your question, what i meant to write was p.UserID as DNNID not select it from the ap.table. Sorry it was late and i was tired as all hell. Now with this query reading it still confuses me a bit. when you say where ap.linking column is null makes me think it would select records that exists in both tables only where that column from the ap table is null? i see it is working properly but if you get a second do you think you can break down the query for me as to why? im sure its annoying but as i stated above im as interested in why the answer works as opposed to just getting it working.

thanks for all the help i appreciate it much.
-John


Sure i'm happy to explain you. What we are looking here is to left join the two tables based on matching column. What this does is reagrdless of whether it has a match or not in cnAvailableParticipant_List table, all records of Participant will be returned.Those which has a match will have matching values from cnAvailableParticipant_List table while others will have NULL. Our requirement here is to get records in Participant but not in cnAvailableParticipant_List table. So it is enough we filter results on condition that linking field from cnAvailableParticipant_List table is NULL as these were records from Participant table that couldnt find the match (does not exist) in cnAvailableParticipant_List table. Hope this makes sense to you now.

I really appreciate you asking explanation on soln provided. Most people posting here requires only a soln but seldom tries to understand what it really means.
Go to Top of Page

jtrumbul
Starting Member

11 Posts

Posted - 2008-05-27 : 13:52:08
thanks for your time, i understand the solution now. I will try for more complex questions in the future. hahaha. :)
Go to Top of Page
   

- Advertisement -