In the world of data, deciding how to handle missing values is a common task. Is the absence of a value meaningful or meaningless? Will the absence of a value negatively affect our ability to make an accurate calculation resulting in a value that isn’t misleading? Do the consumers of our dashboards and/or reports understand the concept of NULL and if they do, is it desirable to populate a value instead of showing nothing or NULL? These are only a handful of the possible questions you may need to answer regarding NULLs or missing values. Let’s quickly look at a widely available function across relational database management system products and then dive into a few examples.
Step forward, COALESCE. The COALESCE function accepts two or more arguments and returns the first non-null argument. Easy right? Now for the examples. The schemas, corresponding tables, and data used in the examples can be found at livesql.oracle.com.
At times, it may be ideal to populate NULLs or missing values with a value that provides meaning to the consumers of your data. In the following two examples, missing values are populated with the value 999. In the first example, the value is populated with 999 to indicate the employee isn’t managed by another employee. In the second example, orders without an associated sales representative are populated with the value 999.
-- Replace missing manager ID values with 999.
SELECT
hr.employees.employee_id,
COALESCE(hr.employees.manager_id, 999) AS manager_id
FROM
hr.employees;
| employee_id | manager_id |
| 100 | 999 |
| 101 | 100 |
| 102 | 100 |
-- Replace missing sales representative ID values with 999.
SELECT
COALESCE(oe.orders.sales_rep_id, 999) AS sales_rep_id,
CASE
WHEN oe.orders.sales_rep_id IS NULL
THEN 'Online'
ELSE 'In-store'
END AS order_type,
SUM(oe.orders.order_total) AS order_total
FROM
oe.orders
GROUP BY
sales_rep_id,
CASE
WHEN oe.orders.sales_rep_id IS NULL
THEN 'Online'
ELSE 'In-store'
END
ORDER BY
sales_rep_id DESC;
| sales_rep_id | order_type | order_total |
| 999 | Online | 1859147.3 |
| 163 | In-store | 128249.5 |
| 161 | In-store | 661734.5 |
| 160 | In-store | 88238.4 |
| 159 | In-store | 151167.2 |
| 158 | In-store | 156296.2 |
| 156 | In-store | 202617.6 |
| 155 | In-store | 134415.2 |
| 154 | In-store | 171973.1 |
| 153 | In-store | 114215.7 |
The two examples above are relatively straightforward. The scenario and use of COALESCE simply replaced the missing values with a hard-coded default value. In the next example, product price values will be imputed based on the average price of those products in the same category. The strategy below allows us to step away from hard-coding missing values. Instead, the missing values will reflect the average price at run-time.
-- Solution utilizing a CTE and a correlated subquery in the SELECT clause.
WITH impute AS (
SELECT
product.category_id,
AVG(product.list_price) AS average_price
FROM
oe.product_information product
GROUP BY
product.category_id
)
SELECT
product.product_id,
product.category_id,
COALESCE(product.list_price,
(
SELECT
impute.average_price
FROM
impute
WHERE
impute.category_id = product.category_id
)
) AS list_price
FROM
oe.product_information product;
-- Solution utilizing a CTE and an INNER join.
WITH impute AS (
SELECT
product.category_id,
AVG(product.list_price) AS average_price
FROM
oe.product_information product
GROUP BY
product.category_id
)
SELECT
product.product_id,
product.category_id,
COALESCE(product.list_price, impute.average_price) AS list_price
FROM
oe.product_information product
INNER JOIN
impute
ON product.category_id = impute.category_id;
| product_id | category_id | list_price |
| 1797 | 12 | 349 |
| 2459 | 12 | 699 |
| 3127 | 12 | 498 |
| 2254 | 13 | 453 |
| 3353 | 13 | 489 |
| 3069 | 13 | 436 |
| 2253 | 13 | 399 |
| 3354 | 13 | 543 |
| 3072 | 13 | 567 |
| 3334 | 13 | 612 |
| 3071 | 13 | 633 |
| 2255 | 13 | 775 |
| 1743 | 13 | 800 |
| 2382 | 13 | 850 |
| 3399 | 13 | 815 |
| 3073 | 13 | 224 |
| 1768 | 13 | 345 |
| 2410 | 13 | 357 |
| 2257 | 13 | 399 |
| 3400 | 13 | 389 |
| 3355 | 13 | 517.75 |
| 1772 | 13 | 456 |
| 2243 | 11 | 350 |
| 3057 | 11 | 369 |
| 3061 | 11 | 499 |
| 2245 | 11 | 512 |
| 3065 | 11 | 999 |
| 3331 | 11 | 879 |
| 2252 | 11 | 889 |
| 3064 | 11 | 1023 |
| 3155 | 11 | 49 |
| 3234 | 11 | 39 |
| 3350 | 11 | 740 |
| 2236 | 11 | 964 |
| 3054 | 11 | 600 |
| 1782 | 12 | 125 |
| 2430 | 12 | 175 |
| 1792 | 12 | 225 |
| 1791 | 12 | 275 |
| 2302 | 12 | 150 |
| 2453 | 12 | 195 |
| 2810 | 32 | 6 |
| 2870 | 32 | 5 |
| 1781 | 17 | 233 |
| 2264 | 17 | 223 |
| 2260 | 17 | 67 |
| 2266 | 17 | 333 |
| 3077 | 17 | 274 |
| 2259 | 17 | 39 |
| 2261 | 17 | 42 |
| 3082 | 17 | 81 |
| 2270 | 17 | 66 |
| 2268 | 17 | 77 |
| 3083 | 17 | 67 |
| 2374 | 17 | 65 |
| 1740 | 17 | 134 |
| 2409 | 17 | 210 |
| 2262 | 17 | 98 |
| 2522 | 19 | 44 |
| 2278 | 19 | 55 |
| 2418 | 19 | 61 |
| 2419 | 19 | 72 |
| 3097 | 19 | 3 |
| 3099 | 19 | 4 |
| 2380 | 19 | 6 |
| 2408 | 19 | 4 |
| 2457 | 19 | 5 |
| 2373 | 19 | 6 |
| 1734 | 19 | 6 |
| 1737 | 19 | 8 |
| 1745 | 19 | 9 |
| 2982 | 19 | 44 |
| 3277 | 19 | 36 |
| 2976 | 19 | 52 |
| 3204 | 19 | 126 |
| 2638 | 19 | 137 |
| 3020 | 19 | 449 |
| 1948 | 19 | 498 |
| 3003 | 19 | 3219 |
| 2999 | 19 | 999 |
| 3000 | 19 | 1749 |
| 3001 | 19 | 2556 |
| 3004 | 19 | 2768 |
| 3391 | 19 | 85 |
| 3124 | 19 | 84 |
| 1738 | 19 | 86 |
| 2377 | 19 | 97 |
| 2299 | 19 | 76 |
| 2414 | 13 | 454 |
| 2415 | 13 | 359 |
| 2395 | 14 | 123 |
| 1755 | 14 | 121 |
| 2406 | 14 | 223 |
| 2404 | 14 | 221 |
| 1770 | 14 | 250.78 |
| 2412 | 14 | 98 |
| 2378 | 14 | 305 |
| 3087 | 14 | 124 |
| 2384 | 14 | 599 |
| 1749 | 14 | 337 |
| 1750 | 14 | 699 |
| 2394 | 14 | 128 |
| 2400 | 14 | 448 |
| 1763 | 14 | 247 |
| 2396 | 14 | 179 |
| 2272 | 14 | 135 |
| 2274 | 14 | 161 |
| 3090 | 14 | 193 |
| 1739 | 14 | 299 |
| 3359 | 14 | 111 |
| 3088 | 14 | 258 |
| 2276 | 14 | 269 |
| 3086 | 14 | 211 |
| 3091 | 14 | 279 |
| 1787 | 15 | 101 |
| 2439 | 15 | 123 |
| 1788 | 15 | 178 |
| 2375 | 15 | 78 |
| 2411 | 15 | 98 |
| 1769 | 15 | 48 |
| 2049 | 15 | 55 |
| 2751 | 15 | 66 |
| 3112 | 15 | 77 |
| 2752 | 15 | 88 |
| 2293 | 15 | 99 |
| 3114 | 15 | 101 |
| 3129 | 15 | 46 |
| 3133 | 15 | 48 |
| 2308 | 15 | 58 |
| 2496 | 15 | 299 |
| 2497 | 15 | 399 |
| 3106 | 16 | 48 |
| 2289 | 16 | 48 |
| 3110 | 16 | 48 |
| 3108 | 16 | 78 |
| 2058 | 16 | 23 |
| 2761 | 16 | 27 |
| 3117 | 16 | 41 |
| 2056 | 16 | 8 |
| 2211 | 16 | 4 |
| 2944 | 16 | 3 |
| 1742 | 17 | 101 |
| 2402 | 17 | 127 |
| 2403 | 17 | 117 |
| 1761 | 17 | 134 |
| 2381 | 17 | 99 |
| 2424 | 17 | 221 |
| 1726 | 11 | 259 |
| 2359 | 11 | 249 |
| 3060 | 11 | 299 |
| 3051 | 32 | 12 |
| 3150 | 32 | 18 |
| 3208 | 32 | 2 |
| 3209 | 32 | 13 |
| 3224 | 32 | 32 |
| 3225 | 32 | 47 |
| 3511 | 32 | 9 |
| 3515 | 32 | 2 |
| 2986 | 33 | 125 |
| 3163 | 33 | 35 |
| 3165 | 33 | 40 |
| 3167 | 33 | 55 |
| 3216 | 33 | 30 |
| 3220 | 33 | 45 |
| 1729 | 39 | 80 |
| 1910 | 39 | 14 |
| 1912 | 39 | 15 |
| 1940 | 39 | 18 |
| 2030 | 39 | 12 |
| 2326 | 39 | 2 |
| 2330 | 39 | 2 |
| 2334 | 39 | 4 |
| 2340 | 39 | 72 |
| 2365 | 39 | 78 |
| 2594 | 39 | 9 |
| 2596 | 39 | 12 |
| 2631 | 39 | 15 |
| 2721 | 39 | 87 |
| 2722 | 39 | 112 |
| 2725 | 39 | 4 |
| 2782 | 39 | 62 |
| 3187 | 39 | 3 |
| 3189 | 39 | 2 |
| 3191 | 39 | 2 |
| 3193 | 39 | 3 |
| 3123 | 19 | 81 |
| 1748 | 19 | 83 |
| 2387 | 19 | 83 |
| 2370 | 19 | 91 |
| 2311 | 19 | 95 |
| 1733 | 19 | 89 |
| 2878 | 19 | 345 |
| 2879 | 19 | 456 |
| 2152 | 19 | 231 |
| 3301 | 19 | 15 |
| 3143 | 19 | 16 |
| 2323 | 19 | 18 |
| 3134 | 19 | 18 |
| 3139 | 19 | 21 |
| 3300 | 19 | 23 |
| 2316 | 19 | 22 |
| 3140 | 19 | 24 |
| 2319 | 19 | 25 |
| 2322 | 19 | 23 |
| 3178 | 21 | 45 |
| 3179 | 21 | 50 |
| 3182 | 22 | 65 |
| 3183 | 22 | 50 |
| 3197 | 21 | 45 |
| 3255 | 21 | 35 |
| 3256 | 21 | 40 |
| 3260 | 22 | 50 |
| 3262 | 21 | 50 |
| 3361 | 21 | 40 |
| 1799 | 24 | 1000 |
| 1801 | 24 | 100 |
| 1803 | 24 | 60 |
| 1804 | 24 | 65 |
| 1805 | 24 | 50 |
| 1806 | 24 | 50 |
| 1808 | 24 | 55 |
| 1820 | 24 | 55 |
| 1822 | 24 | 1500 |
| 2422 | 24 | 150 |
| 2452 | 24 | 100 |
| 2462 | 24 | 80 |
| 2464 | 24 | 70 |
| 2467 | 24 | 80 |
| 2468 | 24 | 75 |
| 2470 | 24 | 80 |
| 2471 | 24 | 500 |
| 2492 | 24 | 45 |
| 2493 | 24 | 25 |
| 2494 | 24 | 25 |
| 2995 | 24 | 70 |
| 3290 | 24 | 65 |
| 1778 | 25 | 62 |
| 1779 | 25 | 128 |
| 1780 | 25 | 450 |
| 2371 | 25 | 146 |
| 2423 | 25 | 84 |
| 3501 | 25 | 555 |
| 3502 | 25 | 105 |
| 3503 | 25 | 22 |
| 1774 | 29 | 110 |
| 1775 | 29 | 27 |
| 1794 | 29 | 128 |
| 1825 | 29 | 25 |
| 2004 | 29 | 90 |
| 2005 | 29 | 115 |
| 2416 | 29 | 41 |
| 2417 | 29 | 33 |
| 2449 | 29 | 83 |
| 3101 | 29 | 75 |
| 3170 | 29 | 161 |
| 3171 | 29 | 148 |
| 3172 | 29 | 42 |
| 3173 | 29 | 86 |
| 3175 | 29 | 37 |
| 3176 | 29 | 120 |
| 3177 | 29 | 120 |
| 3245 | 29 | 222 |
| 3246 | 29 | 222 |
| 3247 | 29 | 120 |
| 3248 | 29 | 222 |
| 3250 | 29 | 28 |
| 3251 | 29 | 31 |
| 3252 | 29 | 26 |
| 3253 | 29 | 222 |
| 3257 | 29 | 66 |
| 3258 | 29 | 80 |
| 3362 | 29 | 99 |
| 2231 | 31 | 2510 |
| 2335 | 31 | 100 |
| 2350 | 31 | 2500 |
| 2351 | 31 | 2900 |
| 2779 | 31 | 3980 |
| 3337 | 31 | 800 |
| 2091 | 32 | 1 |
| 2093 | 32 | 8 |
| 2144 | 32 | 18 |
| 2336 | 32 | 55 |
| 2337 | 32 | 300 |
| 2339 | 32 | 25 |
| 2536 | 32 | 80 |
| 2537 | 32 | 200 |
| 2783 | 32 | 10 |
| 2808 | 32 | 1 |