Find only id where in another column is only NULL

Viewed 47

Example table:

------------------------
| user_id | value|
------------------------
|   1     | NULL |
|   1     | NULL |
|   2     | NULL |
|   2     | 22   |

How to find user_id which has only value is NULL? (in the example, the expected result: user_id: 1)

SELECT * WHERE value is NULL of course doesn't work.

2 Answers

You can use aggregation with a having clause:

select user_id
from t
group by user_id
having max(value) is null

This should be working since its working. It seems sql server version issue :

enter image description here

select * from users where value IS NULL

Another way of checking the null values as below :

select * from users where value <> ''

enter image description here

Related