Members Not Invited by Founder

A coworking space is running a welcome event for people who were not brought in by its founder, whose member_id is 2. Some members were invited by another member; others walked in on their own and have invited_by set to NULL.

Return member_id and full_name of every member whose invited_by is anything other than 2, including members with no inviter at all. Rows may be in any order. If every member was invited by the founder, return no rows.

Tables

members — One row per coworking member.

ColumnTypeDescription
member_id (key)INTEGERUnique member id
full_nameTEXTMember name
invited_byINTEGERmember_id of the member who invited them; NULL if nobody did
joined_onTEXTDate the membership started, 'YYYY-MM-DD'

Example

members

member_idfull_nameinvited_byjoined_on
1Rosa DelgadoNULL2024-01-10
2Sven AaltoNULL2024-01-10
3Tariq Nasser22024-02-01
4Uma Reddy12024-02-15
5Vera KowalskiNULL2024-03-03
6Wes Okafor22024-03-20
7Yara Haddad32024-04-02

Expected output

member_idfull_name
1Rosa Delgado
2Sven Aalto
4Uma Reddy
5Vera Kowalski
7Yara Haddad

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

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

Run the obvious query and check whether members with no inviter appear.
Comparing NULL with anything, even with <>, gives unknown, and WHERE drops unknown rows.
Add the NULL rows explicitly with IS NULL, or use SQLite's null-safe IS NOT operator.

The idea

The natural first attempt, WHERE invited_by <> 2, misses members with no inviter. For them the comparison is NULL <> 2, which SQL evaluates to unknown rather than true, and WHERE keeps only true rows. In plain English those members clearly were not invited by the founder, so the query must bring them back.

The standard fix is to say so explicitly: invited_by IS NULL OR invited_by <> 2. Each row now passes if it has no inviter, or a different inviter.

SQLite also offers null-safe comparison operators. a IS NOT b behaves like <> except that NULL is treated as an ordinary value, so NULL IS NOT 2 is true. The standard SQL spelling is IS DISTINCT FROM, which other databases support. Another common form is COALESCE(invited_by, 0) <> 2, which works as long as the placeholder 0 can never be a real id.

Solution

SELECT member_id,
       full_name
FROM members
WHERE invited_by IS NULL
   OR invited_by <> 2;

Another way

SELECT member_id,
       full_name
FROM members
WHERE invited_by IS NOT 2;

Why common answers fail

NULL <> 2 is unknown, not true, so members with no inviter are silently dropped.

SELECT member_id, full_name FROM members WHERE invited_by <> 2;

Tries to add the NULL rows back with = NULL, which never matches anything.

SELECT member_id, full_name FROM members WHERE invited_by <> 2 OR invited_by = NULL;

Explicitly removes members with no inviter, although they were not invited by the founder either.

SELECT member_id, full_name FROM members WHERE invited_by NOT IN (2) AND invited_by IS NOT NULL;

What the interviewer is checking

The interviewer wants to see whether you notice that "not equal" silently drops NULL rows, one of the most common real-world SQL bugs.

Mistakes to avoid

  • Assuming <> also matches NULLs.
  • Trying to catch NULLs with = NULL.
  • Using a COALESCE placeholder that could collide with a real id.

Follow-ups to expect

  • Count members per inviter, with walk-ins (NULL) as their own group. (GROUP BY invited_by keeps NULL as one group)
  • Show each member next to their inviter's name. (LEFT JOIN members AS inv ON inv.member_id = m.invited_by)
  • Members not invited by anyone in a given list of ids. (invited_by IS NULL OR invited_by NOT IN (...))

In real work

"Everyone except segment X" queries over nullable fields, such as referral source, account manager or region, are a classic source of under-counted reports.

Practice next

Next Post Previous Post
No Comment
Add Comment
comment url