vivlumn
vivlumn

Reputation: 13

Use data from 2 tables but no need to join?

I have a table A with a column of some years and a table B with 2 columns of startyear & endyear. I need to check whether years in A are in periods of B and count the appearance frequency of each period. So, I wrote:

SELECT B.periodname,count(A.id)
FROM A, B
WHERE A.year >= B.startyear AND A.year < B.endyear
GROUP BY B.periodname

Is this a valid query? Do I need to join A on B?

Upvotes: 1

Views: 28

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270773

Although your method is valid, it is more commonly written using JOIN:

ELECT B.periodname, COUNT(A.id)
FROM A JOIN
     B
     ON A.year >= B.startyear AND A.year < B.endyear
GROUP BY B.periodname

Upvotes: 1

Related Questions