HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How to find values from one table missing in another?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
howmissingoneanotherfindvaluesfromtable

Problem

I am a Junior Developer. I have been asked to create a subquery to solve this problem. If the question is not clear then please let me know so I can provide more details.

I have two tables,Article and Author. I have been tasked with doing the following:


Select distinct Author values that don't exist in the Author table. The result should not contain any Authors that are NULL or Empty String. The purpose is to find any Author names that ARE in the Article table but NOT in the Author table.

I have Author column in both Tables. Can this problem be solved by joining these two tables?

I have tried

select distinct name 
from author 
where name NOT IN ( select author 
                    from article 
                    where author IS NOT NULL)


It doesn't give me the result that I want :(

Solution

select author from article where author IS NOT NULL and author <> ''
except 
select name   from author


since any comparison the null is false I think below works

select author from article where author <> ''
except 
select name   from author

Code Snippets

select author from article where author IS NOT NULL and author <> ''
except 
select name   from author
select author from article where author <> ''
except 
select name   from author

Context

StackExchange Database Administrators Q#137444, answer score: 5

Revisions (0)

No revisions yet.