Predict the output
What does the SELECT print?
CREATE TABLE users (id int);
CREATE TABLE banned (user_id int);
INSERT INTO users VALUES (1), (2), (3);
INSERT INTO banned VALUES (2), (NULL);
SELECT count(*) FROM users WHERE id NOT IN (SELECT user_id FROM banned);Hint 1 of 1
What is 1 <> NULL?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 0
Correct. For ids 1 and 3, id <> NULL is unknown, so NOT IN is never true. NOT EXISTS would return 2.
x NOT IN (2, NULL) means x <> 2 AND x <> NULL, and comparing with NULL gives unknown, never true. No row passes the WHERE clause, so the count is 0.
Why not the others?
- B
2. That's what NOT EXISTS returns; NOT IN with a NULL in the list returns nothing. - C
3. Id 2 is banned for certain, so 3 is impossible either way. - D An error. NULLs in a subquery are legal; they just make NOT IN unknown.
| count |
|---|
| 0 |
(1 row)