Urgent.News

What's breaking now, across thousands of outlets.

Tech

When SQL Has Nothing to Say: Understanding NULLs

NULL in SQL carry different meanings; missing value unknown value no value inapplicable data customer_id customer_name contact 101 Alice 0712345678 102 Brian NULL 103 Carol 0798765432 You shouldn't interpret NULL as; 0 (zero) '' (empty string) blank space Using IS NULL, IS NOT NULL to find NULLs Since NULL is not equal to zero or an empty string, you cannot use = to compare it; you must use IS…

In the world of SQL, NULL carries a unique meaning - it signifies a missing, unknown or not applicable value. This can be confusing as users often try to interpret it as zero, an empty string or blank space. However, it's crucial to remember that NULL is not equal to these values and should not be treated as such.

SQL uses three-valued logic which includes TRUE, FALSE and UNKNOWN. When you compare NULL with another NULL or any value using comparison operators like =, it results in UNKNOWN. Hence, a query like WHERE contact = NULL will not return any rows as UNKNOWN does not satisfy the condition.

To specifically find NULL values, SQL provides two functions: IS NULL and IS NOT NULL. Using these functions, you can retrieve rows with or without NULL values. For instance, SELECT * FROM customers WHERE contact IS NULL; will return all customers who don't have a contact number.

Moreover, COUNT() function is useful in counting NULL values. COUNT(*) ignoring NULL values while COUNT(column_name) does not. For instance, SELECT COUNT(*) AS null_contacts FROM customers WHERE contact IS NULL; will return the number of customers without a contact number.

Updating NULL values can be done using UPDATE command. If you want to replace NULL with a default value, you can use UPDATE customers SET home_address = work_address WHERE home_address IS NULL; to fill up missing home addresses with the available work addresses.

However, when dealing with NULL values, it's essential to think about the business context. Deleting rows based solely on NULL values can lead to loss of valuable data. It's always recommended to confirm the business requirement before doing so. The syntax for deleting rows with NULL values is DELETE FROM table_name WHERE column_name IS NULL;

In conclusion, NULL in SQL represents missing, unknown or not applicable data. Understanding how to use IS NULL and IS NOT NULL, functions like COUNT() and knowing when to update, retain or delete such data is crucial for accurate data handling in SQL. Remember, when SQL has nothing to say, NULL is saying something.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

The hashCode()-to-Array-Index Bug Almost Everyone Writes Once

"hash % capacity" looks trivial until you trace it by hand. Two separate bugs hide in that one line — and the fix for one of them has its own hole.

  • Modulo operator returns signed 32-bit integer, can be negative
  • Math.abs() fails for Integer.MINVALUE, no positive counterpart
  • Math.floorMod() ensures non-negative result for all integers

Devbox: Portable and Isolated Development Environments

Introduction I am one of those people who need to have everything in its place to be able to do something. This applies, of course, when I work with a local or remote repository.

  • Devbox simplifies isolated development environments
  • Devbox generates project-specific repositories
  • Devbox offers user-friendly package management

More from Sunday 6 September →