Postgres not returning data on array_agg query as below
A LEFT JOIN
can solve it, like Laurenz provided.
But I suggest an ARRAY constructor in a LATERAL
subquery instead:
SELECT l.name, b.book_list
FROM library l
CROSS JOIN LATERAL (
SELECT ARRAY(
SELECT title
FROM books
WHERE library = l.id
)
) b(book_list)
WHERE l.id = :library_no;
This way, you don't need to aggregate in the outer query level and don't need to GROUP BY
there.
You also don't need COALESCE
, since the ARRAY constructor over an empty result already produces an empty array ({}
).
And it should be faster for a small selection in library
- obviously the query gets the result for a single given library.
Aside, you only need the variable :library_no
in a single place like demonstrated.
About LATERAL
joins:
- What is the difference between LATERAL and a subquery in PostgreSQL?
About the ARRAY constructor:
- Why is array_agg() slower than the non-aggregate ARRAY() constructor?
Basic about joining tables in the manual.
You need a left join:
... FROM library
LEFT JOIN books ON library.id = books.library
Then you will also get libraries with no books.