Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

COALESCE or CASE more efficient and/or standard

In terms of x compared to y.

  • Does x comply with sql standards better? [apologies if subjective]
  • Is x more efficient than y?
  • Or are these scripts completely different and to be used in different contexts?

x

    SELECT * 
    FROM   a 
           INNER JOIN b ON
           COALESCE(b.columntojoin, b.alternatecolumn) = a.columntojoin

y

    SELECT * 
    FROM   a 
           INNER JOIN b ON
           (case when b.columntojoin is null then b.alternatecolumn else b.columntojoin end) = a.columntojoin
like image 755
whytheq Avatar asked Mar 20 '13 12:03

whytheq


1 Answers

COALESCE is essentially a shorthanded CASE statement.

Both are exactly the same.

There is also ISNULL in SQL Server (differs in other DBMSs), but that's actually a non-standard feature and is actually more limited that COALESCE.

like image 68
mattytommo Avatar answered Sep 28 '22 06:09

mattytommo