Click here to Skip to main content
15,881,516 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a table like this

NAME    ID

XXX     51
XXX     49
YYY     51
YYY     20
ZZZ     2
YYY     22




The question is to select the name where the name id not in 51.

now result should be

NAME  ID

ZZZ   2


Can anyone help me?
Posted
Updated 27-Jun-13 1:32am
v3
Comments
ArunRajendra 27-Jun-13 7:29am    
Can you be more clear on what basis only zzz 2 record should be shown?
kesav prakash 27-Jun-13 7:33am    
the id should not be '51'
ArunRajendra 27-Jun-13 7:35am    
In that case it should display this records. Why only ZZZ 2?

XXX 49
YYY 20
ZZZ 2
YYY 22

I beleive you want a statement something like this:

SQL
Select NAME, ID from TableName 
       Where Name NOT IN (Select NAME from TableName Where ID = 51)


Or if you only want to filter on the ID, then you want:

SQL
Select NAME, ID from TableName Where ID <> 51
 
Share this answer
 
v2
Comments
kesav prakash 27-Jun-13 7:41am    
thank you so much sir i got the right answer
SELECT NAME,ID FROM dbo.Table_1 WHERE NAME NOT IN (SELECT DISTINCT NAME FROM dbo.Table_1 WHERE ID = '51')
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900