Common Forum Questions: Why Does My Query Return Nothing?

"My query runs without errors but returns nothing!" is one of the most common questions beginners post online. The good news: the cause is nearly always one of a few simple things. Work through this list before you post.

Is there any data at all?

Check the table has rows:

SELECT COUNT(*) FROM students;

If it says 0, the problem is not your query. Perhaps the INSERT was never saved. In Python's sqlite3 module, for example, changes are not saved until you call conn.commit().

Are you connected to the right database?

With SQLite, opening a file name that does not exist creates a new, empty file. If your script runs from a different folder, it may be looking at a brand-new empty database instead of yours. Print the full path you are opening, or use an absolute path.

Is the text an exact match?

SELECT * FROM students WHERE name = 'sam';

If the stored name is Sam or Sam (with a trailing space), this may find nothing. Whether comparisons ignore upper and lower case depends on the database and its settings. Try:

SELECT * FROM students WHERE LOWER(TRIM(name)) = 'sam';

If that works, the stored data has different case or extra spaces.

Are you comparing with NULL using =?

SELECT * FROM students WHERE email = NULL;   -- always finds nothing
SELECT * FROM students WHERE email IS NULL;  -- correct

NULL means "unknown", so it is never equal to anything, not even another NULL.

Is your WHERE clause too strict?

Combining conditions with AND means all must be true. Try removing conditions one at a time to see which one removes all the rows.

Did a JOIN drop the rows?

An INNER JOIN only keeps rows that have a match in both tables. If a student has no enrolments, they disappear. Use LEFT JOIN to keep them:

SELECT s.name, e.course_id
FROM students s
LEFT JOIN enrolments e ON e.student_id = s.id;

Are the types different?

If a number is stored as text, comparing it with a number may not match in some databases. Check how the column was created and what the data looks like.

Quick checklist

CheckCommand or idea
Any rows?SELECT COUNT(*) FROM table
Right file or database?Print the path or connection details
Saved?Did you commit after inserting?
Exact text?Try LOWER(TRIM(column))
NULL?Use IS NULL
Too many conditions?Remove them one by one
JOIN dropping rows?Try LEFT JOIN

If you still need to ask

Include the table definition, a few sample rows, your query, what you expected and what you got. That gets you a fast, friendly answer. Our page on asking your first SQL question online shows a good example.

A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.

This is a static page of sample discussion topics. There is no live forum, membership or user community on this site.

Related pages on this site