0

I have the following data -

Code1   Code2
G01621  E04621

I want to show all codes that are in code1 but not in code2.

I wrote the following SQL, but it is returning incorrect data -

select * from codes c1
left join codes c2 
on lower(c1.code1) = lower(c2.code2)
where c2.code2 is null
2
  • How are the results incorrect? Commented Aug 2, 2019 at 2:34
  • Please expand your meaning for "in code1 but not in code2". Provide additional data as a sample set of 1 is not very useful. Data should include data rows that should be selected and some that should not. Commented Aug 2, 2019 at 4:13

3 Answers 3

1

You can use not exists:

select c.code1
from codes c
where not exists (select 1
                  from codes c2
                  where lower(c2.code2) = lower(c.code1)
                 );
Sign up to request clarification or add additional context in comments.

2 Comments

This is returning some records that exist in both. I have even added a tolower before comparing the two values.
No, there must be some difference between the codes. It's most likely white space or unprintable characters.
0

use trim() function

select * from codes c1
left join codes c2 
on trim(lower(c1.code1)) = trim(lower(c2.code2))
where c2.code1 is null;

Demo

Comments

0

Use except :

select c1.code1 from codes c1
except
select c2.code2 from codes c2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.