Skip to content
Common mistakes/Why a SQL Query Returns No Rows
Common mistake

Why a SQL Query Returns No Rows

An empty result is almost always a filter that does not match the real data: case, ranges, or AND/OR.

The symptom

The query runs without error but comes back empty, even though you're sure matching rows exist.

The usual causes

CauseExample
Case mismatch (text is case-sensitive)status = 'Shipped' when the data is 'shipped'
A NULL comparisoncoupon_code = null instead of is null
An impossible ANDwhere state = 'PA' and state = 'CA'
A date range that excludes everythingEnd date before the start date

How to debug it

Strip the query back and inspect the real values, then add conditions one at a time until the rows disappear. That last condition is your culprit.

Case is the classic culprit: a text comparison is exact, so your filter has to match the stored value letter for letter.

Wrong: capital S matches nothing
select order_id, status
from orders
where status = 'Shipped'
order by order_id
Right: the value as stored
select order_id, status
from orders
where status = 'shipped'
order by order_id

When a SQL view returns no rows

Debug a view the same way you debug a query: start with the base table, then add each join and filter back one at a time. For an exact text filter such as where coupon_code = 'spring10', first prove that value exists in the real data with the same casing and spacing:select distinct coupon_code from orders shows that Garden Shop stores those codes uppercase, so that filter finds nothing.

Tip

To compare text without worrying about case, lowercase both sides:lower(status) = 'shipped'.

Learn more