Inserts, NULLs and Cleanup

How to Insert Values Into an Empty Column in SQL

Learn how to fill an empty column in an existing SQL table with UPDATE, copy values from another table, and avoid common NULL mistakes in your queries.

On this page
  1. The short answer
  2. What “empty” can mean
  3. Why INSERT doesn’t fill an existing row
  4. Fill an empty column with UPDATE
  5. Copy values from another table
  6. Fill from another column with COALESCE
  7. Use CASE when the value depends on other data
  8. Add the column first if it doesn’t exist
  9. Add a new row with values
  10. Check your work and protect your data
  11. Common mistakes

The short answer

To put values into a column that is empty in rows that already exist, use an UPDATE statement with a WHERE clause that finds the empty rows:

UPDATE orders
SET shipping_region = 'Unknown'
WHERE shipping_region IS NULL;

INSERT is the wrong tool for this job. It adds new rows, so it doesn’t change rows already in the table. New to UPDATE? Start with how do i write an update statement in sql, then come back here for the empty-column details.

What “empty” can mean

A column can look empty in a few different ways:

  • NULL means no value was stored. Test for it with IS NULL.
  • An empty string ('') is a value with zero characters. In most databases it is not NULL, so IS NULL won’t match it. Oracle is the notable exception, because it treats an empty string as NULL.
  • Spaces only look blank in a results grid, but they are still characters. Neither check above will find them. Trimming them first helps, and the details are in what does trim() do in sql.

Check which one you have before you write the update. A SELECT with the same condition shows you the rows first.

Why INSERT doesn’t fill an existing row

An INSERT creates a new row from the values you give it, so it doesn’t change a row that is already there. If the table has a unique key and you insert a key that already exists, the database rejects the statement. Without a key, you end up with a duplicate row. Either way, an UPDATE is the statement that changes the existing row.

Fill an empty column with UPDATE

Start with a SELECT that uses the same condition you plan to use in the update:

SELECT order_id, customer_id, shipping_region
FROM orders
WHERE shipping_region IS NULL;

When the rows look right, run the update:

UPDATE orders
SET shipping_region = 'Unknown'
WHERE shipping_region IS NULL;

The WHERE clause limits the change to the empty rows. Without it, the UPDATE sets the column in every row of the table.

Copy values from another table

When the right value is stored in another table, look it up for each row with a subquery:

UPDATE orders
SET shipping_region = (
    SELECT c.region
    FROM customers AS c
    WHERE c.customer_id = orders.customer_id
)
WHERE shipping_region IS NULL;

If a customer has no match, the column is set to NULL again, which is what it was before. If the subquery returns more than one row, the statement fails, so the matching key in customers should be unique.

Many databases also accept an UPDATE that joins two tables directly. The join syntax differs between MySQL, SQL Server and PostgreSQL, so check your database’s manual. The matching idea is the same one used in how to write an inner join query in sql.

Fill from another column with COALESCE

If a blank column should take its value from other columns in the same row, COALESCE() returns the first value in its list that isn’t NULL:

UPDATE orders
SET shipping_region = COALESCE(shipping_region, billing_region, 'Unknown')
WHERE shipping_region IS NULL;

For each blank row, the update uses billing_region when it has a value, and 'Unknown' when both are empty. For a closer look at how the function works, read what is coalesce() in sql.

Use CASE when the value depends on other data

When the right value depends on other columns, a CASE expression in the SET clause can pick a different value for each row:

UPDATE orders
SET order_size = CASE
    WHEN total_amount >= 100 THEN 'Large'
    ELSE 'Small'
END
WHERE order_size IS NULL;

The same pattern appears in case statement in update query in sql server, which goes into more detail.

Add the column first if it doesn’t exist

If the table has no column for the values yet, add one. A new column holds NULL in every existing row unless you give it a DEFAULT:

ALTER TABLE orders
ADD shipping_region VARCHAR(50);

The exact form varies a little by database. The COLUMN keyword is optional in MySQL and PostgreSQL, and SQL Server doesn’t use it, so this form works in all three. Check your manual for other databases. Once the column exists, fill it with an UPDATE as shown above.

Add a new row with values

If the values belong in a new row, use INSERT instead:

INSERT INTO orders (order_id, customer_id, shipping_region)
VALUES (1005, 42, 'North');

Use an UPDATE for rows that already exist, and an INSERT for rows that don’t. For the full syntax, start with what is an insert statement.

Check your work and protect your data

  • Run the SELECT first and note how many rows it returns. After the UPDATE, most SQL clients report how many rows changed. The two numbers should match.
  • Where your database supports transactions, start one before the change, check the results, then COMMIT to keep them or ROLLBACK to undo them. The start command differs by database, such as BEGIN or START TRANSACTION.
  • For a large table, take a backup or make a copy before you change it.

Common mistakes

  • Using = NULL. WHERE shipping_region = NULL matches no rows, because NULL isn’t equal to anything. Use IS NULL.
  • Leaving out WHERE. The update then changes every row in the table, including rows that already have values.
  • Ignoring data types. The new value must fit the column’s type. Text in a number column fails, and a value too long for its column may be rejected or cut short, depending on the database and its settings. For the common types, see different data types of sql.
  • Expecting a NOT NULL column to stay empty. A column that doesn’t allow NULL can’t hold a blank value, so every row needs a value when it is written.
  • Filling with 0 or placeholder text without thinking. A zero, or a word like “Unknown,” looks like real data later. Before you store 0 in place of NULL, read how to get 0 instead of null in sql.

Keep exploring

Guides from other topics

All topics →