Replace Missing Values

Create Table

-- Create table called adventurers
CREATE TABLE adventurers (
    -- string variable
    name varchar(255),
    -- integer variable
    age int,
    -- string variable
    race varchar(255),
    -- string variable
    weapon varchar(255)

Insert Rows With Missing Values

-- Insert into the table adventurers
INSERT INTO adventurers (name, age, race, weapon)
VALUES ('Fjoak Doom-Wife', 28, 'Human', 'Axe'),
       ('Alooneric Cortte', 29, 'Elf', 'Bow'),
       ('Piperel Ramsay', 35, 'Elf', NULL),
       ('Casimir Yardley', 14, 'Elf', NULL)

Retrieve Missing Values

In SQL, missing values are denoted as NULL.

-- Return values where value is the weapon if not missing, but "Unknown" if missing
SELECT name, COALESCE(weapon, 'Unknown') FROM adventurers
Fjoak Doom-WifeAxe
Alooneric CortteBow
Piperel RamsayUnknown
Casimir YardleyUnknown