Let me explain why your query fails first, then show how to fix it.
This is the query that doesn't work as expected:
SELECT *
FROM Inventory
WHERE NOT EXISTS(SELECT * FROM Inventory WHERE AccNum LIKE '100%');
The reason it doesn't return any rows is because of the where
clause. The subquery returns 50 rows. Hence, it exists. In other words, this version of the query will return either all rows in the table or none of the rows. Nothing changes from row to row.
One way to fix this is by using in
:
SELECT *
FROM Inventory i
WHERE AccNum NOT IN (SELECT AccNum FROM Inventory i2 WHERE AccNum LIKE '100%');
This will work so long as AccNum
is never NULL
.
Of course, an easier way is just to use a simple where
clause:
SELECT *
FROM inventory
WHERE AccNum NOT LIKE '100%' OR AccNum IS NULL;