'SQL Query for accessing information from two separate tables

I have two tables named Articles and Publishers

In Article table, there are multiple columns but there are 3 of note: ArticleId, PublishDate, and AuthorName.

In the Publishers table, there are also multiple columns but 2 of note: ArticleId and HasPublisher.

So for example if the Article table contains:

ArticleID   PublishDate  AuthorName
1              9/27/21     John
2              12/13/21   Smith
3               1/3/22.    Bob

and the Publisher table contains

ArticleID   HasPublisher
 1             Yes
 2             No
 3              Yes

What query will I have to run to check how many rows in the Publisher table has column HasPublisher = yes in which the same Id in the Article table has a publishDate column value of past date X.

So in this example if we want to fit criteria of date being past 9/26/21, the result would be 2 (Id = 1 and Id = 3 are value HasPublisher = Yes in the second table) but if we wanted to fit critera of date being past 1/1/22, the result would only be 1 entry (Id = 3).



Solution 1:[1]

SELECT COUNT(1) FROM Article art
INNER JOIN Publisher pub
ON  art.ArticleID   = pub.ArticleID 
WHERE PublishDate  > '2021-07-26'
AND HasPublisher = 'Yes'

You can inner join both the table on the ArticleId column and then filter on the criteria of HasPublisher and PublishDate.

Edit: Corrected the date format

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1