Basuki Tjahaja Basuki Tjahaja - 6 months ago 16
SQL Question

How do I get the count of records that do not have any record in the second table?

I have Two Table

First_Table:

ID (PK), Name, Address

Second_Table:

ID (PK), InfoID (FK for ID from First_Table), BookName

I want to get the count of records that do not have any record in the second table?

vkp vkp
Answer

Use not exists.

select count(*) 
from first_table f
where not exists (select 1 from second_table s where s.infoid = f.id)
Comments