Home > database >  find records with same key but different second column value
find records with same key but different second column value

Time:09-06

My Input data is :

Id salary  
101 1000  
101 1000  
102 2500  
102 3000  
105 5000   
105 5000  
105 5000  
106 12  
106 142  
106 12  

Output :

102 2500  
102 3000  
106 12  
106 142  
106 12  

I mean based on the id , I want to find out which Id has different salary. If the salary is the same in all the records , I want to discard those records. Kindly help.

CodePudding user response:

select *
from   t   
where  id in(
            select   Id
            from     t
            group by Id
            having   max(salary) <> min(salary)
            )
Id salary
102 2500
102 3000
106 12
106 142
106 12

Fiddle

CodePudding user response:

SELECT `Id`, `salary` FROM salary 
  WHERE Id IN( SELECT Id FROm salary GROUP By Id HAVING COUNT(DISTINCT SALARY) > 1)
Id salary
102 2500
102 3000
106 12
106 142
106 12

fiddle

  •  Tags:  
  • sql
  • Related