Explain Codes LogoExplain Codes Logo

How do I check if a column is empty or null in MySQL?

sql
prompt-engineering
best-practices
performance
Alex KataevbyAlex Kataev·Feb 11, 2025
TLDR

To find rows with a blank or NULL your_column in your_table, use:

SELECT * FROM `your_table` WHERE `your_column` IS NULL OR `your_column` = '';

This efficiently obtains all entries missing data in your_column.

Need to treat spaces as empty too? Fear not! The TRIM function comes to the rescue:

SELECT * FROM `your_table` WHERE TRIM(`your_column`) = '' OR `your_column` IS NULL;

And voila! Spaces are no longer considered as data.

The art of checking for emptiness

Handling NULL and empty values is a practical and sometimes tricky part of data integrity. Let's look at various nifty methods and queries.

Using NULLIF for variable conditions

The handy function NULLIF treats specific values as NULL, ideal for checking against spaces:

SELECT * FROM `your_table` WHERE NULLIF(`your_column`, ' ') IS NULL;

That query counts space as NULL, giving you a more flexible condition check.

Employing COALESCE for fallback values

Use the COALESCE function to provide a fallback if your_column is NULL:

SELECT * FROM `your_table` WHERE COALESCE(`your_column`, 'default_value') = 'default_value';

This way, you're prepared even if your_column disappoints with NULL.

Robust checks with function combos

To virtually shake your data and see if anything falls out (NULL or empty), try:

SELECT * FROM `your_column` WHERE NULLIF(TRIM(`your_column`), '') IS NULL;

You're practically yelling, "Show yourself, empty data!"

Trimming the fat

Beware of pesky leading or trailing spaces, always use TRIM:

SELECT * FROM `your_table` WHERE TRIM(`your_column`) IS NULL OR TRIM(`your_column`) = '';

This is your detective work for those hide-and-seek playing spaces.

Index-friendly efficient data retrieval

Large datasets call for efficient queries and index-friendly conditions:

SELECT * FROM `your_table` WHERE `your_column` IS NULL OR `your_column` = '';

Think of it as speed dating for your data—you want to weed out the duds fast.

Visualization

To filter EMPTY or NULL values in our data garden (🌾), Consider a watering can (🚿) that only waters plants that exist: - A cell with data: 🌱✅ (no need for water, it's growing) - A cell that's empty: 🌱❓ (water me please) - A cell that's null: ⛔️ (no plant, no watering)
SELECT * FROM data_garden WHERE plant IS NULL OR plant = ''; -- The watering can 🚿 attends to ❓ (empty) or ⛔️ (null) cells.

In SQL, IS NULL or = '' is like telling the watering can where to go!

Going beyond: edge cases and traps

Let's dive head first into certain edge cases and common pitfalls:

Watch out for whitespace

If your_column could be full of spaces, give TRIM the power:

SELECT * FROM `your_table` WHERE TRIM(`your_column`) = '';

A thorough cleanup that could make Marie Kondo proud.

Empty isn't always applicable

Non-string columns won't be "empty", but still, check for NULL:

SELECT * FROM `your_table` WHERE `numeric_column` IS NULL;

Because even numbers can ghost you and turn NULL.

Beware of data conversion

Implicit type conversions in MySQL can bite:

SELECT * FROM `your_table` WHERE `your_column` = 0; -- '0', 'false', and '' may be friends in MySQL world. Don't fall for it.

Performance matters

Indexes are super handy but not fond of functions. Keep conditions simple and you'll speed up your queries:

SELECT * FROM `your_table` WHERE `your_column` IS NULL OR `your_column` = '';

Remember: SQL is not a snail. Optimize for speed.