Basic SQL logic of NOT and NULL values when using =
select * from dual where not (1=2 );
One row returned.
select * from dual where not ( 5=null);
No rows returned.
select * from dual where not (1=2 and 5=null);
Unexpectedly, row returned.
Am I missing something basic how NOT works?
Or how AND works?
Tried in Oracle 11 and 12.
sql oracle
add a comment |
select * from dual where not (1=2 );
One row returned.
select * from dual where not ( 5=null);
No rows returned.
select * from dual where not (1=2 and 5=null);
Unexpectedly, row returned.
Am I missing something basic how NOT works?
Or how AND works?
Tried in Oracle 11 and 12.
sql oracle
add a comment |
select * from dual where not (1=2 );
One row returned.
select * from dual where not ( 5=null);
No rows returned.
select * from dual where not (1=2 and 5=null);
Unexpectedly, row returned.
Am I missing something basic how NOT works?
Or how AND works?
Tried in Oracle 11 and 12.
sql oracle
select * from dual where not (1=2 );
One row returned.
select * from dual where not ( 5=null);
No rows returned.
select * from dual where not (1=2 and 5=null);
Unexpectedly, row returned.
Am I missing something basic how NOT works?
Or how AND works?
Tried in Oracle 11 and 12.
sql oracle
sql oracle
asked Nov 13 '18 at 22:07
goo54321goo54321
63
63
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This is all fine. Almost any operation with NULL returns NULL, including =, <>, and NOT.
But, let's parse that last where:
where not (1 = 2 and 5 = null);
is the same as:
where not (FALSE and NULL)
Well, AND returns FALSE if any of the operands are FALSE -- even if others are NULL. Look at this another way, whether the NULL is "TRUE" or "FALSE", the expression is FALSE.
Hence, the result is the same as:
where TRUE
Thanks for the explanation. I guess my assumption was NULL and FALSE, would return NULL.
– goo54321
Nov 13 '18 at 22:23
@user2469062 . . .NULL OR FALSEreturnsNULL, butNULL AND FALSEis well-defined, because of theFALSE.
– Gordon Linoff
Nov 13 '18 at 22:34
I thought this was an interesting little topic so I made an SQLFiddle to show what's legitimate and what isn't. I thought others might find it informative.
– Bob Jarvis
Nov 13 '18 at 23:16
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290229%2fbasic-sql-logic-of-not-and-null-values-when-using%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is all fine. Almost any operation with NULL returns NULL, including =, <>, and NOT.
But, let's parse that last where:
where not (1 = 2 and 5 = null);
is the same as:
where not (FALSE and NULL)
Well, AND returns FALSE if any of the operands are FALSE -- even if others are NULL. Look at this another way, whether the NULL is "TRUE" or "FALSE", the expression is FALSE.
Hence, the result is the same as:
where TRUE
Thanks for the explanation. I guess my assumption was NULL and FALSE, would return NULL.
– goo54321
Nov 13 '18 at 22:23
@user2469062 . . .NULL OR FALSEreturnsNULL, butNULL AND FALSEis well-defined, because of theFALSE.
– Gordon Linoff
Nov 13 '18 at 22:34
I thought this was an interesting little topic so I made an SQLFiddle to show what's legitimate and what isn't. I thought others might find it informative.
– Bob Jarvis
Nov 13 '18 at 23:16
add a comment |
This is all fine. Almost any operation with NULL returns NULL, including =, <>, and NOT.
But, let's parse that last where:
where not (1 = 2 and 5 = null);
is the same as:
where not (FALSE and NULL)
Well, AND returns FALSE if any of the operands are FALSE -- even if others are NULL. Look at this another way, whether the NULL is "TRUE" or "FALSE", the expression is FALSE.
Hence, the result is the same as:
where TRUE
Thanks for the explanation. I guess my assumption was NULL and FALSE, would return NULL.
– goo54321
Nov 13 '18 at 22:23
@user2469062 . . .NULL OR FALSEreturnsNULL, butNULL AND FALSEis well-defined, because of theFALSE.
– Gordon Linoff
Nov 13 '18 at 22:34
I thought this was an interesting little topic so I made an SQLFiddle to show what's legitimate and what isn't. I thought others might find it informative.
– Bob Jarvis
Nov 13 '18 at 23:16
add a comment |
This is all fine. Almost any operation with NULL returns NULL, including =, <>, and NOT.
But, let's parse that last where:
where not (1 = 2 and 5 = null);
is the same as:
where not (FALSE and NULL)
Well, AND returns FALSE if any of the operands are FALSE -- even if others are NULL. Look at this another way, whether the NULL is "TRUE" or "FALSE", the expression is FALSE.
Hence, the result is the same as:
where TRUE
This is all fine. Almost any operation with NULL returns NULL, including =, <>, and NOT.
But, let's parse that last where:
where not (1 = 2 and 5 = null);
is the same as:
where not (FALSE and NULL)
Well, AND returns FALSE if any of the operands are FALSE -- even if others are NULL. Look at this another way, whether the NULL is "TRUE" or "FALSE", the expression is FALSE.
Hence, the result is the same as:
where TRUE
answered Nov 13 '18 at 22:10
Gordon LinoffGordon Linoff
766k35300402
766k35300402
Thanks for the explanation. I guess my assumption was NULL and FALSE, would return NULL.
– goo54321
Nov 13 '18 at 22:23
@user2469062 . . .NULL OR FALSEreturnsNULL, butNULL AND FALSEis well-defined, because of theFALSE.
– Gordon Linoff
Nov 13 '18 at 22:34
I thought this was an interesting little topic so I made an SQLFiddle to show what's legitimate and what isn't. I thought others might find it informative.
– Bob Jarvis
Nov 13 '18 at 23:16
add a comment |
Thanks for the explanation. I guess my assumption was NULL and FALSE, would return NULL.
– goo54321
Nov 13 '18 at 22:23
@user2469062 . . .NULL OR FALSEreturnsNULL, butNULL AND FALSEis well-defined, because of theFALSE.
– Gordon Linoff
Nov 13 '18 at 22:34
I thought this was an interesting little topic so I made an SQLFiddle to show what's legitimate and what isn't. I thought others might find it informative.
– Bob Jarvis
Nov 13 '18 at 23:16
Thanks for the explanation. I guess my assumption was NULL and FALSE, would return NULL.
– goo54321
Nov 13 '18 at 22:23
Thanks for the explanation. I guess my assumption was NULL and FALSE, would return NULL.
– goo54321
Nov 13 '18 at 22:23
@user2469062 . . .
NULL OR FALSE returns NULL, but NULL AND FALSE is well-defined, because of the FALSE.– Gordon Linoff
Nov 13 '18 at 22:34
@user2469062 . . .
NULL OR FALSE returns NULL, but NULL AND FALSE is well-defined, because of the FALSE.– Gordon Linoff
Nov 13 '18 at 22:34
I thought this was an interesting little topic so I made an SQLFiddle to show what's legitimate and what isn't. I thought others might find it informative.
– Bob Jarvis
Nov 13 '18 at 23:16
I thought this was an interesting little topic so I made an SQLFiddle to show what's legitimate and what isn't. I thought others might find it informative.
– Bob Jarvis
Nov 13 '18 at 23:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290229%2fbasic-sql-logic-of-not-and-null-values-when-using%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown