1

if I have this sql:

SELECT A FROM B WHERE C IN 
(
   SELECT D FROM E
)

If my internal select don't return any result (0 rows) my where statement will be true or false?

I'm using SQL Server

7
  • @zerkms I see... "my where statement will" so the OP is asking specifically about the WHERE condition (which is falsy), not the outer statement. Commented Feb 24, 2014 at 20:12
  • @Zane: this particular statement is, since it's not a correlated subquery. Commented Feb 24, 2014 at 20:51
  • Still results in search of a table for the results. Not equivalent to false. Commented Feb 24, 2014 at 21:02
  • @Zane: it's not about results of the search (?) it's about the value of C IN (...) expression, which is either true or false for the whole result set. Commented Feb 24, 2014 at 21:03
  • @Zane: IN operator doesn't return not found it returns either false or true Commented Feb 24, 2014 at 21:06

3 Answers 3

4

This is your query:

SELECT A
FROM B
WHERE C IN (SELECT D FROM E);

The where statement is quite simple. It filters out rows there is no match between B.C and E.D. By your statement, there is no match, so all rows are filtered out. The query returns no rows.

The where statement is not "true" or "false" in general. It is "true" or "false" for a given row in B (in this case). With no matches, the where clause will be uniformly false for all rows.

Sign up to request clarification or add additional context in comments.

3 Comments

"The where statement is quite simple. It returns rows when there is a match between " --- may be poorly worded, but from this phrase one might think that "where statement" returns rows by itself.
@zerkms . . . I know what you are saying (and I changed the wording). The problem is that I can't think of a good word to mean the opposite of "filter out". "A where clause filters in rows that match the conditions" isn't quite right.
I personally think that "A WHERE clause filters rows" is a perfect wording.
1

Ignore the tables for the moment and think about what this does:

SELECT 'Yes' WHERE 1 IN (SELECT 1 WHERE 1=0)

Comments

0

Neither, if the inner query returns no rows, you won't get any results returned. Selecting anything from an empty set will give you an empty set. IMO an empty set is neither true nor false in and of itself, only when compared to something else. So if the question is "will I get any results", then the answer is false.

10 Comments

"my where statement will be true or false" --- obviously it will be false
This can be a comment rather than answer.
@zerkms WHEREs aren't true or false, but determine WHICH rows match the criteria.
@zerkms correct, for a given RECORD. It's not accurate to say the Where clause will be false unless you use something like WHERE 1 = 0. No rows will match the criteria.
@zerkms no we can't because these are tables. No rows match the current data but tomorrow who knows.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.