by wefarrell · 6y ago · 0 comments
SELECT * FROM my_table WHERE my_column != 5
You would expect it to return rows that have a null value for my_column, since null is not 5. However that is not the case. ... WHERE my_column IS DISTINCT FROM 5 SELECT * FROM my_table WHERE my_column NOT IN (5, 6) SELECT * FROM my_table WHERE my_column != 5 OR my_column IS NULL;
This is because what you are selecting for is two conditions: when the value is != 5 and when the value is NULL so the result of != 5 is unknown.FWIW I agree with you that NULL's are counter-intuitive. While I am more or less aware of all the various ways to account for them, I still gravitate towards SQL schemas without NULL's since I prefer the intuitiveness of 2VL when writing or reading SQL.
COALESCE(my_column, -1)
might work for you in this case. See [1] for documentation on it in Postgres (it is in ANSI SQL though).[1]: https://www.postgresql.org/docs/current/functions-conditiona...
A NULL value technically means it's unknown. An unknown value might be 5, hence why it's not in the result set. Some abuse NULL to mean "value doesn't exist". But a value that doesn't exist can't be 3, or 42, or any other value that's different from 5, so in that regard shouldn't be part of the result set either.
Others again abuse NULL to mean "doesn't apply". And in that case I think it makes sense to include the row in the result set. For example, if I write a query to get all people who's middle name is not "William", I'd most likely want people without middle names included.
Maybe we should have introduced NEX (non-existing) and NAP (non-applicable) as possible values in addition to NULL?
Codd (the inventor of relational algebra) actually suggested this. I think the primary source is a book that may not be on the web. There's some discussion here (mostly saying why they think it didn't happen and wouldn't work out): https://arxiv.org/html/1606.00740v1/
No, that would muddy things in my opinion, like it did to JavaScript. Instead, have more operations/functions for dealing with them in a more "normal" way, so that we can say "WHERE x <> 5" and get results one expects. I'm not sure the syntax, and my drafts would take a lot of time to explain. To give a taste, maybe have something like "WHERE ~x <> 5" in which the tilde converts x's value to the type's default, such as a blank in the case of strings.
If the different reasons for "emptiness" matter, then usually it suggests the need for a "status" column of some kind so that queries can be done on the reasons. I'd need to study domain specifics to recommend something specific.
Continuing with my middle name example. Say I and everyone I knew had middle names, so I write a database including a required middle name column. Later I discover not everyone has middle names, and so I need to relax the restriction.
In your case, I would change the column to accept NULLs, and I'd have to remember to go over my query to add the ~ operator.
In my case, I'd change the column to accept NAPs (or whatever) and since a NAP value would behave differently to a NULL for <> (and other operators), I wouldn't need to change my query.
I'd almost always use it no matter what. In fact if starting SQL over via a DeLorean, I'd reverse it to require "~" to make it null-sensitive.
It's analogous to case-sensitive comparing. The vast majority of the time you don't want case-sensitive comparisons such that case-insensitive should be the DEFAULT, and you only add extra specifiers/functions if and when you need to do case-based comparing.
If not, then you either end up with SQL full of "toUpperCase(x)", or problems keep popping up if somebody typed stuff wrong or the UI coders messed up.
Similarly, if your SQL is full of NVL() or DENULL() or whatnot, it means the language was poorly factored for your domain, or even most domains. It flunked D.R.Y. in design, such as comparisons having the wrong default behavior.
If a value is unknown, you don't know if it is different from 5, so it would be incorrect to return in the query.
Also a null is no equal to anything.. not even another null
This will print false in SQL Server
if null = null print 'true' else print 'false'
Wrong. It is equal to UNKNOWN:
https://docs.microsoft.com/en-us/sql/t-sql/queries/is-null-t...
If I'm reading it right, (null = null) is unknown, which is falsy (except with ansi_nulls off, then it'll be true). (null is null) is true.
I don't think you can test null = (null = null), i.e. null = unknown. Let me know if that's possible somehow, I can't get it working.
if null = null print 'true' else print 'false'
https://docs.microsoft.com/en-us/sql/t-sql/language-elements...
shortcuts "OR is null", works within functions.