LEFT JOIN and handling NULL Values I have two tables. 

Table 1:

A   B 
------
1   'A'
2   'B'
3   'C'


Table 2:

   A   C
----------
   2   15
   3   20


I'm using the following query:

SELECT table1.A, table1.B, table2.C
FROM table1
LEFT JOIN table2
ON table1.A = table2.A


The problem arises in the output which is:

    A   B   C
---------------
    1   'A'  null
    2   'B'  15
    3   'C'  20


Is there a way to get the null as 0, as I have to do an order by on table2.C. Any help is appreciated.

My expected output is:

    A   B   C
---------------
    3  'C'  20
    2  'B'  15
    1  'A'  0 


ps: I tried with IFNULL(table2.C, 0). Didn't work.
This may be the most efficient solution:

SELECT A, t1.B, t2.C
FROM   table1 t1
LEFT   JOIN table2 t2 USING (A)
ORDER  BY t2.C NULLS FIRST;


NULL values are sorted last by default. You can reverse that with NULLS FIRST. See:


PostgreSQL sort by datetime asc, null first?