Menu

How to return results only if value exists in SQL?

You have a table with a column where some rows contain NULL values or empty strings. You want to retrieve rows where this column has a value (i.e., not NULL and not an empty string).

Written by Selva Prabhakaran | 2 min read

Problem

You have a table with a column where some rows contain NULL values or empty strings. You want to retrieve rows where this column has a value (i.e., not NULL and not an empty string).

Input

product_idproduct_nameproduct_description
1LaptopHigh performance gaming laptop
2Mouse
3Keyboard
4Monitor27 inch 4K display

Create Input Table: Gist

Desired Output

product_idproduct_nameproduct_description
1LaptopHigh performance gaming laptop
4Monitor27 inch 4K display

Solution 1:

Using WHERE

sql
SELECT * 
FROM Products 
WHERE product_description IS NOT NULL AND product_description != '';

Explanation:

  • product_description IS NOT NULL filters out the rows where the product_description is NULL.

  • product_description != ” filters out the rows where the product_description is an empty string.

Combining these conditions with an AND operator ensures that you retrieve rows where product_description has a value.

Solution 2:

Using COALESCE

sql
SELECT * 
FROM Products 
WHERE COALESCE(product_description, '') <> ''
 DESC;

Explanation:

The COALESCE function returns the first non-NULL value in its list of arguments. By using COALESCE(product_description, ”), if product_description is NULL, it will be replaced by an empty string.

The condition <> ” then filters out both NULLs (which have been converted to empty strings by COALESCE) and actual empty strings

.
This method uses a single condition instead of two, which can be considered more concise in some scenario
s.

  1. SQL for Data Science – Level 1
  2. SQL for Data Science – Level 2
  3. SQL for Data Science – Level 3
  1. Introduction to SQL
  2. SQL Window Functons – Made Simple and Easy
  3. SQL Subquery

More SQL Questions

  1. How to concatenate multiple rows into one field in MySQL?
  2. How to efficiently convert rows to columns in SQL?
  3. How to transpose columns to rows in SQL?
  4. How to select first row in each GROUP BY group?
Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Related Course
Master SQL — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Machine Learning Plus
MachineLearningPlus Expert AI/ML Training

Get a Free 30-Min
Guidance Call

Let our ML expert call you back and guide you for free

You're all set!
Tired of tutorials that go nowhere? This video shows you why and what to do instead.
Watch the Free Training
Redirecting in 5 seconds...
No thanks, I'll figure it out myself

Get your Start in AI/ML with the Foundations for Data Science (AI/ML) Course

Enroll for Free
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science