Why a SQL Query Returns No Rows
An empty result is often caused by a filter or join that does not match the real data: case, ranges, NULLs, or boolean logic.
The symptom
The query runs without error but comes back empty, even though you're sure matching rows exist.
The usual causes
| Cause | Example |
|---|---|
| Case mismatch (text is case-sensitive) | status = 'Shipped' when the data is
'shipped' |
| A NULL comparison | coupon_code = null instead of is null |
| An impossible AND | where state = 'PA' and state = 'CA' |
| A date range that excludes everything | End 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.
Letter case is the usual cause: a text comparison is exact, so your filter has to match the stored value letter for letter.
select order_id, status
from orders
where status = 'Shipped'
order by order_idselect order_id, status
from orders
where status = 'shipped'
order by order_idWhen 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.
To compare text without worrying about case, lowercase both sides:
lower(status) = 'shipped'.
Learn more
Related
Use WHERE to keep only matching rows.
Comparisons, AND/OR, IN, BETWEEN, and LIKE.
SELECT, WHERE, ORDER BY, and LIMIT at a glance.
Practice safe date filters, buckets, and lead-time reports.
Paste an error message and find the fix, from GROUP BY to constraint failures.
Combine filters without changing the intended logic.