Description

Table: Customer

Column NameType
idint
namevarchar
referee_idint
  • id is the primary key column for this table.
  • Each row of this table indicates the id of a customer, their name, and the id of the customer who referred them.

Problem Statement

Write an SQL query to report the IDs of the customer that are not referred by the customer with id = 2.

Return the result table in any order.

The query result format is in the following example.

Example 1:

Input:

  • Customer table:
idnamereferee_id
1Willnull
2Janenull
3Alex2
4Billnull
5Zack1
6Mark2

Output:

name
Will
Jane
Bill
Zack

Solution

This is relatively simple problem where you need to filter the customers who are not referred by the customer with id = 2. You can get the required result by using WHERE clause. You will also have to filter where the referee_id is NULL.

1SELECT name 
2    FROM Customer
3    WHERE referee_id IS NOT NULL OR referee_id != 2;