5

I have a query as below,

SELECT count(*) from Employee where e_id IN (121, 234, 536, 234).

In above query, 234 is repeated twice.

But the above query returns result with count = 3 instead of 4.

My Question is how duplicate data gets filtered in Select query when i have not used DISTINCT.

or how does database treats IN clause, is it sort of List(duplicate values) or Set(unique values) or neither.

1

2 Answers 2

5

The WHERE clause only filters rows. It does not multiply them.

So, if a row matches one condition or all conditions doesn't matter. The row simply is filtered or not filtered.

If you want duplicates, then use a JOIN:

select count(*)
from employee e join
     (select 121 as e_id union all
      select 234 union all
      select 536 union all
      select 234
     ) matches
     using (e_id);
Sign up to request clarification or add additional context in comments.

3 Comments

i hve further updated my q's, how does database treats IN clause, is it sort of List(duplicate values) or Set(unique values) or neither.
@ankur-singhal What difference does it make? Either a row is in the result of not. If it matches twice, it's in the result.
@ankur-singhal it is treated as a set. See also my answer.
5

e_id IN (121, 234, 536, 234) works as predicate: for every row of Employee, the value of e_id is checked to see if it matches with any of the values in list. So (121, 234, 536, 234) is treated as a set here.

Comments

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.