Query with LEFT JOIN not returning rows for count of 0
Fix the LEFT JOIN
This should work:
SELECT o.name AS organisation_name, count(e.id) AS total_used
FROM organisations o
LEFT JOIN exam_items e ON e.organisation_id = o.id
AND e.item_template_id = #{sanitize(item_template_id)}
AND e.used
GROUP BY o.name
ORDER BY o.name;
You had a LEFT [OUTER] JOIN
but the later WHERE
conditions made it act like a plain [INNER] JOIN
.
Move the condition(s) to the JOIN
clause to make it work as intended. This way, only rows that fulfill all these conditions are joined in the first place (or columns from the right table are filled with NULL). Like you had it, joined rows are tested for additional conditions virtually after the LEFT JOIN
and removed if they don't pass, just like with a plain JOIN
.
count()
never returns NULL to begin with. It's an exception among aggregate functions in this respect. Therefore, never makes sense, even with additional parameters. The manual:COALESCE(COUNT(col))
It should be noted that except for
count
, these functions return a null value when no rows are selected.
Bold emphasis mine. See:
- Count the number of attributes that are NULL for a row
count()
must be on a column defined NOT NULL
(like e.id
), or where the join condition guarantees NOT NULL
(e.organisation_id
, e.item_template_id
, or e.used
) in the example.
Since used
is type boolean
, the expression e.used = true
is noise that burns down to just e.used
.
Since o.name
is not defined UNIQUE NOT NULL
, you may want to GROUP BY o.id
instead (id
being the PK) - unless you intend to fold rows with the same name (including NULL).
Aggregate first, join later
If most or all rows of exam_items
are counted in the process, this equivalent query is typically considerably faster / cheaper:
SELECT o.id, o.name AS organisation_name, e.total_used
FROM organisations o
LEFT JOIN (
SELECT organisation_id AS id -- alias to simplify join syntax
, count(*) AS total_used -- count(*) = fastest to count all
FROM exam_items
WHERE item_template_id = #{sanitize(item_template_id)}
AND used
GROUP BY 1
) e USING (id)
ORDER BY o.name, o.id;
(This is assuming that you don't want to fold rows with the same name like mentioned above - the typical case.)
Now we can use the faster / simpler count(*)
in the subquery, and we need no GROUP BY
in the outer SELECT
.
See:
- Multiple array_agg() calls in a single query
To make it clear,
the important line is GROUP BY MAIN_TABLE
that will handle NULL value from SOME_TABLE
SELECT COUNT(ST.ID)
FROM MAIN_TABLE MT
LEFT JOIN SOME_TABLE ST ON MT.ID = ST.MT_ID
GROUP BY MT.ID -- this line is a must