SQL Subqueries

Associate
Joined
9 Dec 2008
Posts
2,341
Location
Somewhere!
Hi all,

I'm trying to run a subquery between 2 tables and I just can't get it to work

The 2 tables are below:

Users -- With columns of Name, Email, and StaffNumber
Users_Desc -- With columns of StaffNumber, Gender, and Job

My query is

Select Name
from Users
Where Job In (Select Job from Users_Desc Where Job = 'NULL')

I'm new to this and just have no idea what it's going wrong.

The error I'm getting is "Invalid column name 'Job'." which I assume is because it's looking in Users for it rather than User_Desc?

Any Ideas appreciated!
 
I literally created a load of random information just for the purposes of learning / testing ect..

Using -

SELECT Users.Staffnumber, Users.Name, Users_Desc.Job FROM Users
OUTER JOIN Users_Desc ON Users_Desc.StaffNumber=Users.StaffNumber
WHERE Users_Desc.Job IS NULL

Gave me the error Incorrect syntax near the keyword 'JOIN'.
 
Worked well with the left added on :) Thanks very much... Like I say - I'm still pretty new to SQL / databases.. teaching myself and just created a few tables to do help me learn ect..
 
Just having a look at the results and it doesnt seem to have returned all of the results where Job = Null? Any idea's why that may be?
 
I changed

WHERE Users_Desc.Job IS NULL

to

WHERE Users_Desc.Job = 'NULL'

And it is now returning everything
 
Heh, I take a break from work (currently writing import scripts to import hundreds of thousands of lines from a CSV file using SQL) to write yet more SQL :o.

:eek: I aspire to be you I suppose :p That worked - thanks very much! I'll study it and see where I was going wrong!
 
Back
Top Bottom