Skip to main content
Admissions guidance, study banks, and career tools — get started on Shalyam

The NOT IN Trap With NULL

Medium NULL Handling

Return the ids of customers who have never placed an order. The Orders table contains a row whose customer_id is NULL, so a naive NOT IN returns nothing. Write a query that returns the correct result. Order by id.

Table schema

Customers(id INT PRIMARY KEY, name VARCHAR(50))
Orders(id INT PRIMARY KEY, customer_id INT NULL)

Sample data

Customers ids: 1, 2, 3

Orders:
id | customer_id
1 | 1
2 | NULL

Discussion

No discussions yet.

Rewards · v2.1

Get Exclusive Insights with Every Issue

Join Shalyam Newsletter

Stay ahead in education, research, and innovation—straight to your inbox.