0

Table A:

   id   Name
    1   a
    2   b
    3   c
    4   d
    5   e

Table B:

id  Name
3   c
4   d
5   e

Here, id is the primary key connected to Table B.

I need output like this:-

id 
1
2

That means, which ids in Table A are not present in Table B

5 Answers 5

3

Use EXCEPT operator:

select id from tableA
except
select id from tableB
Sign up to request clarification or add additional context in comments.

Comments

1

You can use a left join, which will preserve all records on the left side and associate them with null if no matching record is available on the right side. This way you can then filter on the right side columns to be null to get the desired outcome

select  t1.id
from    tableA t1
left join
        tableB t2
on      t1.id = t2.id
where   t2.id is null

Comments

1

Use NOT EXISTS in WHERE clause

  SELECT id FROM TableA A 
  WHERE NOT EXISTS(SELECT 1 FROM TableB B WHERE A.id = B.Id )

Comments

1

Using Not in statement.

Try this:-

Select id from TableA
where id not in (Select id from TableB);

Comments

0

You can use minus:

select * from tableA 
minus 
select * from tableB

1 Comment

MINUS is non-standard and only available in some DBMS (Oracle, Teradata, ...). SQL Server uses the standard EXCEPT instead.

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.