Skip to content
Advertisement

Select the non repeating/Distinct value in SQL

I’m trying to select the record based on the distinct id. When i go for ‘DISTINCT’ it picks the duplicate record and truncates the repeating record and gives me the one left out. How can i SQL to pick to just that record which isn’t repeated ?

INPUT

id name age location
1 a 22 usa
1 a 23 usa
2 b 44 uk
3 e 33 eu
3 f 55 eu
8 k 49 usa

OUTPUT

id name age location
2 b 44 uk
8 k 49 usa

Advertisement

Answer

ok , here is how you can do it :

select * from (
   select * , count(*) over (partition by id) cn
   from tablename
) t
where cn = 1 
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement