How can i return '0' from query in pgsql, if the row doesn't exist?

postgresql

Solution

I don't completely understand what result you want to get, but if you want to get value from a field from some row in a table and 0 if there's no rows, try:

select coalesce((select field from table limit 1), 0)

if you have some filter condition for table which could return 1 row or nothing, try this query:

select coalesce((select field from table where <your condition>), 0)

Problem

How can i return '0' from query in pgsql, if the row doesn't exist? Like SELECT IF EXISTS(field) THEN field ELSE 0 FROM table

Original source