I’m trying to create a query that includes records from tables on the following condition:
Includes:
- The user has a record in the job table.
- The user has a NULL join_date OR a record in the job table
Excludes
- The user has no record in the job table AND a NOT NULL join_date
Here’s my schema:
user --> user_id, join_date
job --> job_id, user_id
user rows
user_id: 1, join_date: 1/24/13
user_id: 2, join_date: 1/24/13
user_id: 3, join_date: NULL
user_id: 4, join_date: NULL
job rows
job_id: 101, user_id: 1
job_id: 102, user_id: 3
I want to write a query that returns users #1, #3, and #4. I have the following query which does not return user #4:
SELECT DISTINCT u.[user_id], u.join_date, uj.job_id
FROM [user] u
LEFT JOIN job uj ON (u.user_id = uj.user_id OR u.join_date is null)
WHERE uj.user_id = u.user_id
I think this is best expressed using
inin thewhereclause:If you want job information too, you can join in the job information as:
This will return multiple rows per user, one for each job. You only need the
distinctif a user can be listed multiple times for a single job.Or, if you prefer:
This goes by your exclude logic.
By the way,
inandnot inare really just special cases of outer joins.