MySQL
SQL Query
Database Management
Data Filtering
SQL Tutorial

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.

Practice system design

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:

  • 'NULL means 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:

sql
1SELECT id, name
2FROM users
3WHERE name IS NOT NULL
4  AND name <> '';

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.

sql
1SELECT id, name
2FROM users
3WHERE name IS NOT NULL
4  AND TRIM(name) <> '';

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 NULL plus ''
  • 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

sql
1CREATE TABLE contacts (
2    id INT PRIMARY KEY,
3    email VARCHAR(255)
4);
5
6INSERT INTO contacts (id, email) VALUES
7(1, '[email protected]'),
8(2, ''),
9(3, NULL),
10(4, '   '),
11(5, '[email protected]');
12
13SELECT id, email
14FROM contacts
15WHERE email IS NOT NULL
16  AND TRIM(email) <> '';

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 that NULL does 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 NULL and '' as interchangeable when the schema or application semantics distinguish them.

Summary

  • In MySQL, NULL and '' 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.