<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Baldwin Apps</title>
    <description>The latest articles on DEV Community by Baldwin Apps (@baldwin_apps).</description>
    <link>https://dev.to/baldwin_apps</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3751190%2Fa30b53d9-42a4-4079-8f84-c3b777beada3.png</url>
      <title>DEV Community: Baldwin Apps</title>
      <link>https://dev.to/baldwin_apps</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/baldwin_apps"/>
    <language>en</language>
    <item>
      <title>COALESCE in SQL: the “Don’t Let NULL Win” Function You’ll Use Everywhere</title>
      <dc:creator>Baldwin Apps</dc:creator>
      <pubDate>Wed, 18 Feb 2026 11:07:46 +0000</pubDate>
      <link>https://dev.to/baldwin_apps/coalesce-in-sql-the-dont-let-null-win-function-youll-use-everywhere-n7o</link>
      <guid>https://dev.to/baldwin_apps/coalesce-in-sql-the-dont-let-null-win-function-youll-use-everywhere-n7o</guid>
      <description>&lt;p&gt;&lt;em&gt;Cross-posted from Medium. If NULL has ever “blanked out” a result you know should exist, COALESCE is the fix you’ll use forever.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you’ve ever looked at a query result and thought:&lt;/p&gt;

&lt;p&gt;“Why is this blank? I know there’s data here…”&lt;/p&gt;

&lt;p&gt;…it was probably NULL doing what NULL does.&lt;/p&gt;

&lt;p&gt;Some databases will happily let a missing value sneak through your calculations, concatenations, and reports — then you’re left debugging a spreadsheet like it’s a crime scene.&lt;/p&gt;

&lt;p&gt;Enter &lt;strong&gt;COALESCE&lt;/strong&gt;: one of the simplest functions in SQL, and also one of the most useful.&lt;/p&gt;

&lt;h2&gt;
  
  
  What COALESCE does (in plain English)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;COALESCE&lt;/strong&gt; returns the first non-NULL value from a list of expressions.&lt;/p&gt;

&lt;p&gt;Think of it like a fallback chain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If this value is NULL, try the next one&lt;/li&gt;
&lt;li&gt;If that’s NULL too, try the next one&lt;/li&gt;
&lt;li&gt;Keep going…&lt;/li&gt;
&lt;li&gt;If they’re all NULL, you get NULL&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The basic pattern
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;COALESCE(value1, value2, value3, ...)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Returns the first non-NULL value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick example: “Give me a name… any name”
&lt;/h2&gt;

&lt;p&gt;Let’s say you have a users table and people can have a display name, a full name, or (worst case) nothing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT
  COALESCE(display_name, full_name, 'Anonymous') AS shown_name
FROM users;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This reads like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use display_name if it exists&lt;/li&gt;
&lt;li&gt;Otherwise use full_name&lt;/li&gt;
&lt;li&gt;Otherwise label them 'Anonymous'&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you’re building apps, dashboards, or any user-facing output, this is basically required.&lt;/p&gt;

&lt;h2&gt;
  
  
  COALESCE vs NULL: why it matters
&lt;/h2&gt;

&lt;p&gt;NULL isn’t “empty” or “zero” or “blank”.&lt;/p&gt;

&lt;p&gt;NULL means: unknown / missing / not provided.&lt;/p&gt;

&lt;p&gt;And SQL treats NULL like a wildcard ghost.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NULL breaks math&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT 10 + NULL;   -- result: NULL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So if you’re doing totals, averages, costs, scoring, etc., one NULL can nuke your result.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix it with COALESCE&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT 10 + COALESCE(NULL, 0);  -- result: 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Real-world use cases you’ll hit constantly
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1) Safe totals (avoid NULL turning everything into NULL)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT
  order_id,
  COALESCE(subtotal, 0)
  + COALESCE(tax, 0)
  + COALESCE(shipping, 0) AS total
FROM orders;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is reporting-grade SQL: stable, predictable, and less likely to make you question reality at 1 a.m.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2) Text concatenation without “NULL poisoning”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Many SQL dialects will produce NULL if you concatenate text with NULL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT
  COALESCE(first_name, '')
  || ' '
  || COALESCE(last_name, '') AS full_name
FROM customers;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Depending on your database you may use CONCAT() instead of ||, but the COALESCE idea stays the same.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3) “Prefer this column, fallback to that one”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Classic: billing address might be missing so you fall back to shipping:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT
  customer_id,
  COALESCE(billing_state, shipping_state) AS state
FROM customer_addresses;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Common gotcha: type mismatches
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;COALESCE&lt;/strong&gt; returns a single value, and SQL tries to make all the arguments compatible.&lt;/p&gt;

&lt;p&gt;So avoid mixing types like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;COALESCE(age, 'unknown')   -- might error or auto-cast in weird ways
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Better:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;keep it numeric:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;COALESCE(age, 0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;or keep it text:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;COALESCE(CAST(age AS VARCHAR), 'unknown')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Casting syntax varies by database.)&lt;/p&gt;

&lt;h2&gt;
  
  
  Your turn
&lt;/h2&gt;

&lt;p&gt;Where are NULLs messing with your life right now?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;totals?&lt;/li&gt;
&lt;li&gt;string output?&lt;/li&gt;
&lt;li&gt;missing user fields?&lt;/li&gt;
&lt;li&gt;inconsistent imports?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Drop your scenario in the comments — if it’s a common pain point, I’ll write the next article as a fix.&lt;/p&gt;

&lt;p&gt;🫧 SQL Bubble Pop — a quick, game-style SQL trainer designed to help you build instinct for joins, views, CTEs, and query logic in short daily sessions.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>programming</category>
      <category>dataengineering</category>
    </item>
  </channel>
</rss>
