MySQL select where column is not empty
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In MySQL, "not empty" can mean several different things depending on the column and the data quality you expect. The usual case is that you want values that are neither NULL nor the empty string, and sometimes you also want to exclude strings that contain only spaces.
Distinguish NULL, empty strings, and whitespace
These cases are not equivalent:
- '
NULLmeans no value' - '
''is a real string value with length zero' - '
' 'is a non-empty string that happens to contain only spaces'
If you do not decide which of these should count as empty, your query logic will stay fuzzy.
The common query pattern
If a text column should contain a meaningful value, the basic filter is:
That query removes rows where name is missing and rows where name is an empty string. It does not remove rows such as ' '. If those should also be excluded, trim the value first.
This version is semantically stricter. It treats strings made only of spaces as empty for filtering purposes.
Choose the condition that matches the data
If your column is numeric, date-based, or already constrained by schema, the notion of empty may not apply at all. The title pattern is mainly a string-column problem. For string columns, the rule usually becomes one of these:
- allow whitespace and reject only
NULLplus'' - reject
NULL,'', and whitespace-only strings - normalize data on write so the query stays simpler later
That last option is often the best long-term choice. If the application never writes blank-but-not-meaningful values, the query becomes easier and indexes are easier to use effectively.
Performance implications
name IS NOT NULL AND name <> '' is generally simpler for the optimizer than wrapping the column in a function. When you use TRIM(name), MySQL may have a harder time using an index efficiently because the predicate applies a transformation to the column value.
That does not mean you should never use TRIM. It means you should understand the tradeoff. If whitespace-only junk exists in the table, the strict query may be the correct one. If performance matters heavily, cleaning the data during ingestion is often a better fix than calling string functions in every filter.
A practical example
The result contains rows 1 and 5 only. That is usually what people mean when they say they want values that are not empty.
Common Pitfalls
- Checking only
<> ''and forgetting thatNULLdoes not compare like a normal string. - Assuming whitespace-only strings count as empty when the query never trims them.
- Using function-wrapped predicates everywhere instead of fixing the data model or input validation.
- Applying the same logic to non-string columns where "empty" is not the right concept.
- Treating
NULLand''as interchangeable when the schema or application semantics distinguish them.
Summary
- In MySQL,
NULLand''are different and must be filtered separately. - Use
column IS NOT NULL AND column <> ''for the common string case. - Add
TRIM(column) <> ''if whitespace-only values should also be rejected. - Decide what "empty" means for your application before writing the query.
- If performance matters, data cleanup on write is often better than function-heavy filtering on read.
Related reading
- MySQL SELECT WHERE datetime matches day and not necessarily time
- MySQL selecting rows where a column is null
- MySQL selecting yesterday's date
- MySQL Server has gone away when importing large sql file
- MySQL server startup error 'The server quit without updating PID file
- MySQL sharding and partition in distributed system
- MySQL show current connection info
- MySQL show status - active or total connections?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.