Find Customer Referee

EasySQL

Description

Write a SQL query to find customers not referred by customer with id = 2. Return customer names.

Table:customer
ColumnType
idINT
nameTEXT
referee_idINT

Examples

Input:CREATE TABLE customer (id INT, name TEXT, referee_id INT); INSERT INTO customer VALUES (1, 'Alice', NULL), (2, 'Bob', NULL), (3, 'Charlie', 1), (4, 'Dave', 2);
Output:Alice Bob Charlie
Explanation:

Alice and Bob have NULL referee_id values, so both are included. Charlie is referred by customer 1, so Charlie is also included. Dave is referred by customer 2, so Dave is the only row excluded.

Input:CREATE TABLE customer (id INT, name TEXT, referee_id INT); INSERT INTO customer VALUES (1, 'Emma', 3), (2, 'John', NULL), (3, 'Sarah', NULL), (4, 'Mike', 2), (5, 'Lisa', 2);
Output:Emma John Sarah
Explanation:

Emma is referred by customer 3 (not 2), John has NULL referee_id (not referred by 2), and Sarah has NULL referee_id (not referred by 2). Mike and Lisa are both referred by customer 2, so they are excluded from the results.

Input:CREATE TABLE customer (id INT, name TEXT, referee_id INT); INSERT INTO customer VALUES (1, 'Tom', NULL), (2, 'Anna', 1), (3, 'Ben', NULL), (4, 'Kate', NULL);
Output:Tom Anna Ben Kate
Explanation:

All customers are returned because none are referred by customer with id = 2. Tom, Ben, and Kate all have NULL referee_id values, while Anna is referred by customer 1. Since no one is referred by customer 2, all customers meet the criteria.

Constraints

  • Handle NULL referee_id

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.