0

Consider the following table,

|  Id  | Value |
----------------
|  1   |  1    |
|  1   |  3    |
|  1   |  1    |
|  2   |  2    |
|  2   |  3    |

How to write a sql query to select Id which has values only in 1 or 3(considering above table, required id=1)

1
  • Select * from T where id = 1 and value in (1,3)
    – Alocyte
    Mar 27, 2015 at 12:26

2 Answers 2

0
select id from <TABLE> t1 where not exists 
  (select 1 from <TABLE> t2 where t1.id=t2.id and t2.value not in (1,3));
0
0

From above answer,

select id from t1 where not exists (select 1 from t2 where t1.id=t2.id and t2.value not in (1,3)) GROUP BY ID;

From question

1 or 3 it will be 1 and 3

Not the answer you're looking for? Browse other questions tagged or ask your own question.