55

I would like to use a default value for a column that should be used if no rows is returned. Is that possible in PostgreSQL? How can I do it? Or is there any other way I can solve this?

E.g. something like this:

SELECT MAX(post_id) AS max_id DEFAULT 0 FROM my_table WHERE org_id = 3

And if there is no rows with org_id = 3 in the table I want to return 0.

asked May 18, 2011 at 14:50

5 Answers 5

68
SELECT coalesce(MAX(post_id),0) AS max_id FROM my_table WHERE org_id = 3

or

SELECT case count(*) when 0 then 0 else MAX(post_id) end AS max_id
FROM my_table 
WHERE org_id = 3;

if you want max(post_id) to be null when there is 1 row but post_id is null

dbfiddle

answered May 18, 2011 at 14:58
0
20

If you want to show 0 (alas 1 row) when your query returns 0 rows, then you could use:

SELECT COALESCE( 
 ( SELECT MAX(post_id) FROM my_table WHERE org_id = 3 )
 , 0) AS max_id
answered Apr 2, 2012 at 11:56
1
  • How can this be done for more than one column? Commented Mar 24, 2020 at 10:40
9
SELECT 
 coalesce(MAX(post_id),0) AS max_id 
FROM 
 my_table 
WHERE 
 org_id = 3 

The above do not work if you want to use default name for name field and it works only if you use number field . The below query works for all type of fields..

SELECT 
 COALESCE(
 (SELECT column_name FROM my_table WHERE id = -1), 
 'default_value'
 ) AS column_name;
mustaccio
28.7k24 gold badges60 silver badges77 bronze badges
answered Apr 2, 2012 at 11:08
3

I cannot get either of the above to work.

Here is what I found to work for this:

SELECT COALESCE(A.max_id, B.dflt) FROM (
SELECT MAX(post_id) AS max_id FROM my_table WHERE org_id = 3) A
 RIGHT OUTER JOIN (SELECT 0 AS dflt) B
 ON 1 = 1

I realize not an elegant solution but does the job.

jcolebrand
6,3764 gold badges44 silver badges67 bronze badges
answered Oct 21, 2011 at 18:01
2
  • 1
    SELECT coalesce(MAX(post_id),0) AS max_id FROM my_table WHERE org_id = 3 works fine for me. Commented Oct 21, 2011 at 18:27
  • 2
    @mmandk9 can you elaborate on "doesn't work" - what version of postgres are you on and what error message (if any) do you get? Commented Oct 21, 2011 at 19:37
-3

Just return the default value if no rows are found:

SELECT IFNULL(s_stock, 5)
 FROM stock_info 
 WHERE s_product_id = '43';
Yasir Arsanukayev
3,1653 gold badges23 silver badges30 bronze badges
answered Apr 20, 2013 at 8:41
2
  • IFNULL is not valid syntax in Postgres (or standard SQL). It's used in MySQL. Commented Apr 20, 2013 at 13:00
  • If no row is found, nothing will be returned. Commented May 5, 2020 at 19:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.