Interview Questions/SQL/Find Customer Referee

Find Customer Referee

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Description

You have a Customer table where each customer may have been referred by another customer (stored as referee_id). Write a SQL query to find the names of customers who were NOT referred by the customer with id = 2. Return the result in any order.

Database Schema

Customer

Column NameTypeDescription
idINTPrimary key
nameVARCHARCustomer name
referee_idINTID of customer who referred them (nullable)

Example

Customer

idnamereferee_id
1WillNULL
2JaneNULL
3Alex2
4BillNULL
5Zack1
6Mark2

Output

name
Will
Jane
Bill
Zack

Explanation:

Alex and Mark were referred by customer 2. All others (including those with NULL referee_id) are returned.

Approach hint

Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

SQL Editor
Loading...

Customer

idnamereferee_id
1WillNULL
2JaneNULL
3Alex2
4BillNULL
5Zack1
6Mark2

Output

name
Will
Jane
Bill
Zack