skip to Main Content

I have a query:

SELECT json_agg(t) FROM t

, which fails, when t table is empty. How to return no rows in this case?

2

Answers


  1. To get no row if the table is empty, you can try this :

    SELECT json_agg(t) 
    FROM t
    WHERE EXISTS (SELECT 1 FROM t)
    GROUP BY t.id
    HAVING COUNT(id) > 0;
    
    Login or Signup to reply.
  2. You might be looking for

    SELECT COALESCE(json_agg(t), '[]') FROM t
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search