Question: Alright, so I'm trying to compare two columns, in two tables, in separate databases. I am going to warn you, I quite new to SQL. I am trying to write a query to do something like this:

```
If a field in tableA column2 contains a field from tableB column1 at least once, increment a counter

```

I want to know the value of the counter. Also, when I same "contains" I mean in a `substr()` kind of way (fe. "marketplace" contains the word "market"). Can anyone help me out with this, or at least point me in the right direction?

Answer: 
I think this is what you are after. it will obviously not loop, but it accomplishes what you are trying to do.

```
select a.c2,
  count(case when a.c2 = b.c1 then 1 else 0 end) as c1count
from table1 as a 
left join
table2 as b
on a.key = b.key

group by a.c2

```

or something like that. that join will probably need some work, but it is hard to say without more information. good luck.
This will get you the number of times that the contents of tableB.col1 is found as part of the contents of tableA.col2:

```
SELECT COUNT(*)
FROM tableA
JOIN tableB ON tableA.col2 RLIKE tableB.col1 

```

In general, SQL engines are optimized to work better with set-based logic as opposed to procedural algorithms, such as looping.