TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Null Values in SQL Queries

137 pointsby maynmanover 5 years ago

16 comments

jupedover 5 years ago
&gt;For example, Oracle database won’t allow you to have an empty string. Anytime Oracle database sees an empty string, it automatically converts the empty string into a NULL value.<p>Damn. This is how you do enterprise.<p>I might be the only person who likes SQL nulls. If you learn how they work up front, they&#x27;re useful and not really that confusing. But if I ran into weird behaviors like this, I might hate them too.
评论 #22224813 未加载
评论 #22224601 未加载
评论 #22225064 未加载
评论 #22224610 未加载
评论 #22232970 未加载
评论 #22226606 未加载
评论 #22224642 未加载
评论 #22224664 未加载
评论 #22224860 未加载
评论 #22224878 未加载
wefarrellover 5 years ago
Null values and inequality are extremely counterintuitive (in postgres at least). If you run the query:<p><pre><code> SELECT * FROM my_table WHERE my_column != 5 </code></pre> 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.
评论 #22224824 未加载
评论 #22225068 未加载
评论 #22230528 未加载
评论 #22225067 未加载
评论 #22225276 未加载
评论 #22224849 未加载
评论 #22224783 未加载
评论 #22224763 未加载
xivzgrevover 5 years ago
Not sure what big deal is. You learn somewhere along the way that you check for null values with “is” vs “=“. Done, write it on a sticky note if you need, and move on.<p>“Why isn’t it consistent??” - well a lot of systems have a lot of bat shit crazy inconsistencies, some times there for good reason. You learn to keep them straight and get your shit done.<p>If you want to learn the “why” every time you encounter a system design quirk, be my guest but you may be going down a time intensive rabbit hole with little pay off for yourself.
评论 #22233851 未加载
mwexlerover 5 years ago
Null values are so important in representing data. But they cause so much confusion in a) unexpected behaviors in queries and b) inconsistent handling across various engines... I sometimes wish &lt;whisper&gt; that they hadn&#x27;t been included in the spec at all &lt;&#x2F;whisper&gt;. But then I come to my senses again, and go fix yet another bugged query for an analyst who didn&#x27;t account for nulls in the data.
评论 #22232428 未加载
tabtabover 5 years ago
A pet peeve of mine is concatenating null strings. It&#x27;s like a poison pill that nulls the whole result. 99.99% of the time that&#x27;s NOT what one wants domain-wise. Maybe the standard should make another concatenation operator that treats null strings as zero length strings. Sure, one can de-null each string in the expression, but that&#x27;s ugly anti-DRY code. Please fix it, I haaate that.
评论 #22232445 未加载
sashavingardt2over 5 years ago
Now here&#x27;s a blast from the past! 20 years ago this was common knowledge. Now it&#x27;s making headline news on HN. SQL is back with the vengeance!
irrationalover 5 years ago
We recently moved from Oracle to Postgres. We had thousands of queries written based on the way Oracle handles NULLs and empty strings. It took us the better part of a year to rewrite all of them to the Postgres way. I am so glad to be off of Oracle.
altitudinousover 5 years ago
I miss my past Oracle career, I&#x27;ve diagnosed this &quot;= NULL&quot; rather than &quot;IS NULL&quot; in so many broken queries, slow queries because of the way Oracle indexing handles NULL.<p>There is a lot of discussion in this thread about whether this implementation of null checking in Oracle is appropriate, analysing it, but the current implementation is just fine, it has been tested by time.<p>The internet does tend to rehash the same arguments over and over!!! The internet forgets. I remember these arguments 20 years ago.
评论 #22232511 未加载
michannneover 5 years ago
Another one is MIN and MAX ignore NULL values, which make for some interesting rollback scenarios.<p>I also swear I have seen a gotcha involving UPDATE WHERE IN and not throwing an error where it should have, which is why I always quadruple check my update statements, but I wasn&#x27;t able to reproduce it and couldn&#x27;t find any information online. I haven&#x27;t seen the issue in so long I forgot what it was, but it would update all rows in your table even if your WHERE clause was proper.
评论 #22230957 未加载
评论 #22229926 未加载
pjdorrellover 5 years ago
Theoretically NULL means &quot;unknown&quot; value. As it happens, most business applications do not have any requirement to deal with &quot;unknown&quot; values. These applications are only interested in acting on requests where all the required data are provided by the person responsible for entering the data. For example, when I transfer money from one bank account to another, the amount of the transfer can&#x27;t be &quot;unknown&quot;, the sending account can&#x27;t be &quot;unknown&quot;, and the receiving account can&#x27;t be &quot;unknown&quot;.<p>These same applications do have requirements to deal with empty values. Sometimes an empty value means &quot;I haven&#x27;t yet entered this value in the to the UI&quot;. But in that case the UI won&#x27;t let you submit the form until you have supplied a valid value.<p>In other cases an empty value is a valid value. For example, &quot;who is your spouse?&quot; and the answer is &quot;I&#x27;m not married&quot;.<p>Sometimes NULL represents &quot;irrelevant&quot;, like for &quot;who is your spouse?&quot;, where some of the records in the table represent people who can have spouses, and some of the records represent other person-like entities that aren&#x27;t actually people and therefore they can&#x27;t have spouses.<p>Given that NULL is _not_ being used to represent &quot;unknown&quot; values, and there is a requirement to represent empty values, and you don&#x27;t want to have a whole extra column just to represent &quot;emptiness&quot;, the most straightforward way to implement empty values is to use NULL. So that is what happens.<p>And you have to remember to use &quot;is&quot; instead of &quot;=&quot; when you want to test your empty NULL values for equality with other empty NULL values - because your SQL database is pretending that NULL really means &quot;unknown&quot;, and it doesn&#x27;t want to say that one unknown value is equal to another unknown value, because that would be theoretically incorrect.
salzigover 5 years ago
there is something &quot;missing&quot;. The SQL spec specifies `null = null` to be &quot;unknown&quot;, where i sometimes expect &quot;true&quot;. For MSSQL this can be configured using `SET ANSI_NULLS { ON | OFF }`. AFAIK MySQL can&#x27;t be configured. Don&#x27;t know about Postgres.
评论 #22225189 未加载
评论 #22224541 未加载
评论 #22224764 未加载
hancheover 5 years ago
NULLs in subselects do bite me with distressing regularity: Writing<p><pre><code> SELECT ... FROM ... WHERE blah NOT IN (SELECT foo FROM bar); </code></pre> getting no hits until I slap my forehead and add WHERE foo IS NOT NULL to the subselect.
kordsover 5 years ago
DynamoDB, which is NoSql, also doesn&#x27;t accept empty strings. But at least, Oracle automatically converts the empty string into NULL, comparing with DynamoDB which would actually fail the query.
paulryanrogersover 5 years ago
With some columnar databases NULLs are &#x27;free&#x27; because they are a default, absent state or compressed away. Can be another reason to prefer them with very large datasets.
lesserknowndanover 5 years ago
In MySQL, NULL values are useful when using CONCAT_WS (concatenation with separator) or GROUP_CONCAT because NULL values will be ignored - so you don’t get e.g., “one,,two”.
Andromeda88over 5 years ago
I was dealing with NULLs whole day on MySQL workbench. It wasn&#x27;t considering int as NULL value. Needed to make all empty cells 0 to be able to import data properly.