Question: I have 2 tables with no primary-foreign key relationship. I want to delete a record(row to be more specific) in Table1 when count of a row in Table2 reaches a certain value.

```
Table1
ID      Name
1       name1
2       name2

Table2
ID     Name     Country
1      name1     US
2      name2     China
3      name2     Germany
4      name1     KSA

```

If name1 count in Table2 reaches 40 then delete name1 from Table1

Hope i have explained it all.
Thanks in advance

Answer: 
From what I understood I came up with this solution. I'm not sure how efficient this is but I guess does the trick.

```
DELETE FROM Table1 where Name IN (select Name from Table2 group by [Name] having count(Name)>=40)

```

Edit: Here you SQL Fiddle that you can try with small set of data.
<http://sqlfiddle.com/#!6/9bf70/21>
Use stored procedure. 

```
Create Procecedure DeleteRecords
(
@recCount int,
@nameField varchar(20)
)
Begin
 If((select count(id) from Table2 where [name]=@nameField) >= @recCount)
begin
 Delete from Table1 where [name]=@nameField
end
End

```