Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Should these queries return the same count?

$
0
0

I have Table1 that has 1000 records and Table2 that has 157 records. Table1 has all the records from Table2.

Shouldn't these queries return the same amount of records (157)?

Thanks.

select count(*) from [Table1] where ID in (select ID from [Table2]) --returns 104 records
select count(*) from [Table2] where ID in (select ID from [Table1]) --returns 157 records

select count(*) from [Table2] b
WHERE  EXISTS
(
   SELECT * FROM [Table1] as a
   WHERE b.ID = a.ID
) --returns 157 records

select count(*) from [Table1] b
WHERE  EXISTS
(
   SELECT * FROM [Table2] as a
   WHERE b.ID = a.ID
) --returns 104 records


VM


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>