I want to query names from table1 and also find if a name exists in table2. I have the following query but it doesn't seem to work. Any suggestions what I did wrong?
select A.name,
CASE WHEN A.name in (select B.name in table2 B)
THEN 'common'
ELSE 'not common'
END
from table1 A
Please note that I have to get "common" / "uncommon" from the select clause itself. I'm using postgres.
How do you check if a table contains any data in SQL? Using EXISTS clause in the IF statement to check the existence of a record. Using EXISTS clause in the CASE statement to check the existence of a record. Using EXISTS clause in the WHERE clause to check the existence of a record.
Example using VLOOKUP You can check if the values in column A exist in column B using VLOOKUP. Select cell C2 by clicking on it. Insert the formula in “=IF(ISERROR(VLOOKUP(A2,$B$2:$B$1001,1,FALSE)),FALSE,TRUE)” the formula bar.
To test whether a row exists in a MySQL table or not, use exists condition. The exists condition can be used with subquery. It returns true when row exists in the table, otherwise false is returned. True is represented in the form of 1 and false is represented as 0.
The exists operator can be used with either of these statements- SELECT, UPDATE, INSERT or DELETE.
I would use EXIST instead of IN:
select
A.name,
CASE
WHEN EXISTS (select *
from table2 B
where B.name = A.name)
THEN 'common'
ELSE 'not common'
END
from
table1 A
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With