Unreachable Dental Patients

A dental clinic is sending appointment reminders. Before the campaign, the front desk wants a list of patients they cannot reach at all: patients with no phone number and no email address on file. A missing value is stored as NULL.

Return two columns, patient_id and full_name, one row per unreachable patient, in any order. A patient who has at least one of the two contact details is reachable and must not appear. If every patient is reachable, return no rows.

Tables

patients — One row per registered patient of the clinic.

ColumnTypeDescription
patient_id (key)INTEGERUnique patient id
full_nameTEXTPatient name as written on the intake form
phoneTEXTContact phone number; NULL when the patient gave none
emailTEXTContact email address; NULL when the patient gave none
registered_onTEXTDate the patient registered, 'YYYY-MM-DD'

Example

patients

patient_idfull_namephoneemailregistered_on
1Amara Osei555-0142amara@mail.test2025-01-09
2Bilal HaddadNULLNULL2025-02-14
3Chiara RicciNULLchiara@mail.test2025-02-20
4Dmitri Volkov555-0177NULL2025-03-02
5Esi MensahNULLNULL2025-03-15
6Farid Rahimi555-0190farid@mail.test2025-04-01

Expected output

patient_idfull_name
2Bilal Haddad
5Esi Mensah

Rows may be returned in any order. Your query also runs against hidden datasets with tricky edge cases.

SQL Path · Select & Filter · problem 7 of 70 · SQLite

A patient is unreachable only when both contact columns are missing.
NULL is not a value you can compare with =. Any comparison with NULL yields NULL, which WHERE treats as false.
Use the IS NULL operator on each column and combine the two tests with AND.

The idea

SQL uses three-valued logic: a comparison is true, false or unknown. Any comparison involving NULL, including NULL = NULL, is unknown, and WHERE keeps only rows where the condition is true. That is why phone = NULL silently matches nothing.

The dedicated test is IS NULL, which returns a plain true or false. A patient is unreachable when phone IS NULL AND email IS NULL. AND is the right connective because one surviving contact detail is enough to reach someone.

The alternative uses COALESCE(phone, email), which returns the first non-NULL argument. It is NULL only when both arguments are NULL, so a single IS NULL test on it gives the same answer. It is compact and handy when you check three or four columns at once.

Solution

SELECT patient_id,
       full_name
FROM patients
WHERE phone IS NULL
  AND email IS NULL;

Another way

SELECT patient_id,
       full_name
FROM patients
WHERE COALESCE(phone, email) IS NULL;

Why common answers fail

Comparing with = NULL is never true (it evaluates to NULL), so the query always returns no rows.

SELECT patient_id, full_name FROM patients WHERE phone = NULL AND email = NULL;

Uses OR, so patients missing only one detail (and still reachable) are listed too.

SELECT patient_id, full_name FROM patients WHERE phone IS NULL OR email IS NULL;

Looks for empty strings, but a missing value here is NULL, which is not the same as an empty string.

SELECT patient_id, full_name FROM patients WHERE phone = '' AND email = '';

What the interviewer is checking

This checks whether you know that NULL means "unknown" and needs IS NULL, a gap that causes silent empty reports in production.

Mistakes to avoid

  • Writing = NULL or <> NULL, which never matches anything.
  • Using OR when the business rule needs both details to be missing.
  • Treating an empty string and NULL as the same thing.

Follow-ups to expect

  • Also treat blank strings as missing. (NULLIF(TRIM(phone), '') IS NULL)
  • Count reachable and unreachable patients in one query. (SUM(CASE WHEN ... THEN 1 ELSE 0 END))
  • List patients missing exactly one of the two details. (one IS NULL, the other IS NOT NULL, via OR of two ANDs)

In real work

Data-quality checks before email or SMS campaigns, CRM hygiene reports and onboarding audits all start by finding rows with missing contact fields.

Practice next

Next Post Previous Post
No Comment
Add Comment
comment url