Find neighborhoods with no registered users

Quick Overview

Write a SQL query that returns every neighborhood with no registered users, given a Neighborhoods table and a Users table that references it. The problem tests anti-join patterns, outer joins and careful handling of a nullable foreign key.

Find neighborhoods with no registered users

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Given a table of neighborhoods and a table of users, find every neighborhood that has zero registered users. The `Neighborhoods` table lists all known neighborhoods, and the `Users` table records each user along with the `neighborhood_id` they belong to. A neighborhood is considered empty if no row in `Users` references its `id`. Return the `id` and `name` of each empty neighborhood, ordered by `id` in ascending order. ```hint Watch the NULLs Some users are not assigned to any neighborhood, so `Users.neighborhood_id` can be NULL. Pick a way of testing "no matching user" that still behaves correctly when the other table contains NULL values. ``` ### Input Table(s) `Neighborhoods` (primary key `id`) | Column | Type | Meaning | |---|---|---| | id | INT | Neighborhood id, primary key | | name | VARCHAR(100) | Neighborhood name, never NULL | | city_id | INT | City the neighborhood belongs to | `Users` (primary key `id`; `neighborhood_id` is a foreign key to `Neighborhoods(id)`) | Column | Type | Meaning | |---|---|---| | id | INT | User id, primary key | | name | VARCHAR(100) | User name, never NULL | | neighborhood_id | INT | Neighborhood the user belongs to, or NULL when the user is not assigned to one | ### Output Contract - Columns, in this order: `id`, `name`, both taken from `Neighborhoods`. - One row per empty neighborhood. - Rows ordered by `id` ascending. `id` is unique, so the order is fully determined. - No rounding applies. ### Example `Neighborhoods` | id | name | city_id | |---|---|---| | 1 | Downtown | 10 | | 2 | Uptown | 10 | | 3 | Midtown | 10 | | 4 | Riverside | 11 | `Users` | id | name | neighborhood_id | |---|---|---| | 1 | Alice | 1 | | 2 | Bob | 1 | | 3 | Charlie | 3 | Expected result | id | name | |---|---| | 2 | Uptown | | 4 | Riverside | Uptown (id 2) and Riverside (id 4) have no registered users, so they are returned in id order. Downtown has two users and Midtown has one, so both are left out. If every neighborhood has at least one user, the result is empty. For example, a single neighborhood `Solo` (id 1) with one user `Eve` whose `neighborhood_id` is 1 returns no rows. ### Constraints and Clarifications - `Neighborhoods` has 1 to 10,000 rows. `Users` has 0 to 100,000 rows. - `Neighborhoods.id` and `Users.id` are unique primary keys. `Neighborhoods.name` is never NULL. - `Users.neighborhood_id` may be NULL, meaning the user is not assigned to any neighborhood. Such a user does not count toward any neighborhood. - A neighborhood with several users is not empty and must not appear in the output. - When `Users` has no rows, every neighborhood is empty and all of them are returned. - `city_id` plays no part in the result.

Overview: Write a SQL query that returns every neighborhood with no registered users, given a Neighborhoods table and a Users table that references it. The problem tests anti-join patterns, outer joins and careful handling of a nullable foreign key.

Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Given a table of neighborhoods and a table of users, find every neighborhood that has zero registered users.

The Neighborhoods table lists all known neighborhoods, and the Users table records each user along with the neighborhood_id they belong to. A neighborhood is considered empty if no row in Users references its id.

Return the id and name of each empty neighborhood, ordered by id in ascending order.

Input Table(s)

Neighborhoods (primary key id)

ColumnTypeMeaning
idINTNeighborhood id, primary key
nameVARCHAR(100)Neighborhood name, never NULL
city_idINTCity the neighborhood belongs to

Users (primary key id; neighborhood_id is a foreign key to Neighborhoods(id))

ColumnTypeMeaning
idINTUser id, primary key
nameVARCHAR(100)User name, never NULL
neighborhood_idINTNeighborhood the user belongs to, or NULL when the user is not assigned to one

Output Contract

  • Columns, in this order: id , name , both taken from Neighborhoods .
  • One row per empty neighborhood.
  • Rows ordered by id ascending. id is unique, so the order is fully determined.
  • No rounding applies.

Example

Neighborhoods

idnamecity_id
1Downtown10
2Uptown10
3Midtown10
4Riverside11

Users

idnameneighborhood_id
1Alice1
2Bob1
3Charlie3

Expected result

idname
2Uptown
4Riverside

Uptown (id 2) and Riverside (id 4) have no registered users, so they are returned in id order. Downtown has two users and Midtown has one, so both are left out.

If every neighborhood has at least one user, the result is empty. For example, a single neighborhood Solo (id 1) with one user Eve whose neighborhood_id is 1 returns no rows.

Constraints and Clarifications

  • Neighborhoods has 1 to 10,000 rows. Users has 0 to 100,000 rows.
  • Neighborhoods.id and Users.id are unique primary keys. Neighborhoods.name is never NULL.
  • Users.neighborhood_id may be NULL, meaning the user is not assigned to any neighborhood. Such a user does not count toward any neighborhood.
  • A neighborhood with several users is not empty and must not appear in the output.
  • When Users has no rows, every neighborhood is empty and all of them are returned.
  • city_id plays no part in the result.