Ibtesam Hussain Sharif
Ibtesam Hussain Sharif

Reputation: 189

SQL query to get count of records in Table A and also in Table B

I have two tables name Table 1 and Table 2. Both of these table contain a column named address. Table 1 contains about 1200 records while Table 2 has another 1 million records on store.

Now, what I'd like to do is to find the count of records in Table 1 where a row with a matching address also exists in Table 2.

I am new to SQL - could anybody please tell me how to get the aforementioned row count?

Upvotes: 0

Views: 1822

Answers (4)

luksch
luksch

Reputation: 11712

You need a JOIN. Something like

SELECT COUNT(*) FROM table2 INNER JOIN table1 ON table2.address = table1.address;

Note: if this is a frequent query you should put an index on the address field in both tables.

Upvotes: 0

Codesen
Codesen

Reputation: 7822

Try this

SELECT COIUNT(*) FRO Table1 WHERE address IN(SELECT address from table2)

Upvotes: 0

Mudassir Hasan
Mudassir Hasan

Reputation: 28741

Select Count( * ) from Table1 Where address in ( select address from Table2 )

Upvotes: 1

TheEwook
TheEwook

Reputation: 11117

select count(*) from Table1
INNER JOIN Table2 on Table1.address = Table2.address

Upvotes: 3

Related Questions