I have the following table structure:
table: user
user_id | join_date
101 | '2012-4-13'
102 | '2012-6-4'
103 | NULL
104 | NULL
table: job
job_id | user_id
20 | 101
21 | 103
I want to return a single result set of user records that contains the following:
- The results of all users that have a match in the job table.
- The results of all users where the join_date is null, AND do not have a record in the job table.
Here is the result set of user_id's I would like to see:
user_id
101 <-- has a job
103 <-- has a job
104 <-- never joined, AND also does not have a job
Do I need to do this using Common Table Expressions or can I do this in a subquery?
Please note I've left out actual details for the sake of brevity. This is not a simple case of an inner join.
Here are each of my queries running separately:
THIS IS THE QUERY THAT RETURNS THE PROPER RESULT FOR #1
SELECT DISTINCT u.*
FROM [user] u, job uj
WHERE u.user_id = uj.user_id
THIS IS THE QUERY THAT RETURNS THE PROPER RESULT FOR #2
SELECT DISTINCT u.*
FROM [user] u
FULL JOIN job uj ON u.user_id = uj.user_id
WHERE u.join_date IS NULL AND uj.user_id IS NULL