Skip to content
Advertisement

PostgreSQL: exclude complete jsonb array if one element fails the WHERE clause

Assume a table json_table with columns id (int), data (jsonb). A sample jsonb value would be

When I use an SQL statement like the following:

… the two array elements are unnested and the one that qualifies the WHERE clause is still returned. This makes sense since each array element is considered individually. In this example I will get back the complete row

I’m looking for a way to exclude the complete json_table row when any jsonb array element fails the condition

Advertisement

Answer

You can move the condition to the WHERE clause and use NOT EXISTS:

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