1

I have a database table: Employee with fields id, name, sex, age.

I run the following queries, and they gave different results. So what's the difference between them?

SELECT name, sex
FROM Employee
WHERE age = null

or

SELECT name, sex
FROM Employee
WHERE age is null
3
  • Any comparison to NULL returns NULL (which is treated as false) except for IS NOT NULL and IS NULL. Commented Jun 28, 2015 at 20:53
  • The diff is the 2nd one works Commented Jun 28, 2015 at 20:54
  • I think the question may be database specific. Commented Jun 28, 2015 at 21:28

1 Answer 1

1

In SQL there's three-valued-logic, TRUE/FALSE/UNKNOWN. Any comparison to NULL results in UNKNOWN, both following queries will always return an empty result set regardless if there are NULLs in age:

select name,sex
from employee 
where age=null;

select name,sex
from employee 
where age<>null;

To get the correct result you need either where age IS NULL; for employees with unknown age or where age IS NOT NULL; for all employees with known age.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.