Skip to content
>_sqlbuddy
easySELECTWHERENULL handling

Find Customer Referee

Find customers who are not referred by customer id 2.

Start practice

Prompt

Find Customer Referee

Prompt

Given the customer table below, write a query that returns the name of every customer who was not referred by customer with `referee_id` = 2.

  • Output a single column: name.
  • A customer with no referee (referee_id IS NULL) is not referred by 2 and must be included.
  • The order of the result does not matter.

Example

idnamereferee_id
1AliceNULL
2Bob1
3Carol2
4Dave3

Expected result:

name
Alice
Bob
Dave

Constraints

  • customer.id is the primary key.
  • customer.referee_id is nullable.

Tips

  • referee_id <> 2 excludes rows where referee_id IS NULL (a NULL comparison is never true) — you need referee_id <> 2 OR referee_id IS NULL.
  • COALESCE(referee_id, -1) <> 2 is the classic one-liner alternative.

Expected concepts

  • WHERE
  • NULL comparison
  • COALESCE / IS NULL

Schema

CREATE TABLE customer (
    id         INTEGER PRIMARY KEY,
    name       TEXT NOT NULL,
    referee_id INTEGER  -- NULL allowed
);

Sample data

INSERT INTO customer (id, name, referee_id) VALUES
  (1, 'Alice', NULL),
  (2, 'Bob',   1),
  (3, 'Carol', 2),
  (4, 'Dave',  3),
  (5, 'Eve',   2);

Additional hidden fixtures are applied during validation to test edge cases.