InterviewPrepKit

Home / SQL / Query Basics

Handling Missing Phone Numbers

easy
Solving tips
  • Test for missing values with IS NULL, never with = NULL, which is never true.
  • Use COALESCE to substitute a fallback value when a column is NULL.
  • COALESCE returns its first non-NULL argument, so put the preferred value first.

You are given a customers table where the phone number is optional. Produce a contact list that never shows a blank phone field.

Schema

CREATE TABLE customers (
  id    INTEGER PRIMARY KEY,
  name  TEXT,
  phone TEXT
);

Sample data:

idnamephone
1Alice555-0101
2BobNULL
3Carol555-0199
4DaveNULL
5EveNULL
6Frank555-0155

Task

Return the id, name, and a contact column for every customer, ordered by id ascending. The contact column should show the customer’s phone when it is present, and the literal text No phone on file when phone is NULL.

Expected output

idnamecontact
1Alice555-0101
2BobNo phone on file
3Carol555-0199
4DaveNo phone on file
5EveNo phone on file
6Frank555-0155
Your workspace Not runnable by design — this is your interview scratchpad. Saved on this device.