SELECT Table1.Task, COUNT(Table2.Task)
FROM Table1
LEFT JOIN Table2
ON Table1.Task = Table2.Task
GROUP BY Table1.Task ;
Table1 is your base set, so join to it. You want to retain all values, so you use a LEFT JOIN and you join on the matching task values. The group and count operations happen on different columns - the GROUP BY is based on your tasks from Table1, and you COUNT the of tasks from Table2.
Here's a demo (using CTEs as implemented by SQL Server, PostgreSQL, Oracle, etc.):
WITH Table1 AS
(
SELECT 100 AS Task
UNION ALL
SELECT 101
UNION ALL
SELECT 103
)
,Table2 AS
(
SELECT 113 AS Task
UNION ALL
SELECT 113
UNION ALL
SELECT 103
)
SELECT Table1.Task, COUNT(Table2.Task)
FROM Table1
LEFT JOIN Table2
ON Table1.Task = Table2.Task
GROUP BY Table1.Task ;
You have to aggregate using GROUP BYSELECT statement, which is part of the SQL standard (and quite popular to be honest).
Although You haven't stated which database engine You are using, the query below should work in most, if not all products, because it uses very basic syntax:
SELECT b.country, count(a.id) AS user_count
FROM countries b
LEFT JOIN users a ON b.id = a.country_id
GROUP BY b.country;
The query could use an inner join instead of the left outer join. In that case it would exclude countries for which there are no matches in the users table.
If You are thinking about writing more queries, it might be better to try some online SQL course. Code Academy one is rather good. Other noteworthy example is from SQLCourse, but other ones found on the Internet might be OK too. Checking the SELECT statement's online documentation on Your database's webpage might be a good idea as well (as implementations do vary in some details).
Best Answer
As
count()
counts only the non-NULL values, this is a simpleGROUP BY
query: