Skip to content
Advertisement

Postgres not returning data on array_agg query as below

The problem arise when there are no data for books in specific library. Consider a following working scenario.

Table name: library

Table name: books

Now when I do query like below:

The query generates output for library 1 & 2, but not for library 3. Why and how to solve this issue? (Generate an empty list on no library books)

Required Output:

PS: I’ve even tried coalesce as below:

Postgres version: 12

Advertisement

Answer

You should use left join for this kind of scenarios like below:

DEMO

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement