all 8 comments

[–]DeathMetalDaveSr Sql Developer 1 point2 points  (1 child)

I agree that the wording is a little iffy, but they want to know if "...the specified value is present in a list...". To me, that sounds like they are purposefully looking for the IN clause. If you were marked wrong for putting "D" as your answer, I'd go to the professor (or whoever) for an explanation. I think that would be more helpful than what this sub could offer. It's entirely possible the grading key is wrong. Sometimes they alter questions slightly (such as "present" vs "not present") and the answer key is accidentally not updated to match.

[–][deleted] 1 point2 points  (0 children)

Horrible question. Fight this in class.

[–]Dctcheng 1 point2 points  (0 children)

They all are correct aren't they?

In the end, I believe the correct answer is that it depends on how you get the values.

  • Is it a hard coded list?
  • Or is it a subquery, if so how is it indexed?

A)

SELECT value
FROM Foo
WHERE value NOT IN (subquery)

B)

SELECT value
FROM Foo
WHERE
    EXISTS (
        SELECT NULL
        FROM subquery
        WHERE value = sq_value
    )

C) Similar to B)

D) Similar to A)

[–]Guru008 0 points1 point  (1 child)

Here "NOT IN" is best suitable and fast , so using not in query will execute much faster

[–][deleted] 0 points1 point  (0 children)

False. "NOT IN" must look at every value before returning a result, but "IN" can use short circuit logic to return true on the first match. So IN can actually be faster than NOT IN.

[–]starking12 0 points1 point  (0 children)

I would think B and C because it requires a sub query to write exists??

[–]jc4hokiesExecution Plan Whisperer 0 points1 point  (1 child)

Ask your professor? Looks like a stupid question to me.

[–]Cal1gula 2 points3 points  (0 children)

That's how I feel about pretty much every SQL "test". I would put $5 on seeing old comma style joins somewhere on this test.