4

I need to obtain the rows which having same column value from the following table, I tried it the following way, but it gives me a single row only.

select *
from employee e
group by e.empsalary
having count(e.empsalary) > 1

Table employee

enter image description here

Please suggest me a way.

2
  • SELECT empsalary and put it inside a nested query like WHERE empsalary IN (SELECT empsalary ...)
    – zerkms
    Mar 4, 2014 at 4:14
  • You can use SELF JOIN.
    – PM 77-1
    Mar 4, 2014 at 4:17

4 Answers 4

8

You should be able to accomplish this by joining the table to itself:

SELECT
    a.*
FROM
    employee a
    JOIN employee b
        ON a.empsalary = b.empsalary
        AND a.empid != b.empid
2
  • Finally! This is what ive been looking for, for centuries! Thank you :D Jul 18, 2017 at 13:04
  • @ZdeněkBednařík how do I check to match more than one column , not just empsalary but also say empaddress . Dec 23, 2021 at 14:05
3

Use an inner join to join with your query posted.

select A.* from employee A
inner join (
  select empsalary
  from employee 
  group by empsalary
  having count(*) > 1
) B ON A.empsalary = B.empsalary
0
2

Something like this perhaps

SELECT * FROM employee a
WHERE EXISTS (
    SELECT 1 FROM employee b
    WHERE a.empsalary = b.empsalary
    AND a.empid <> b.empid
);

Demo - http://sqlfiddle.com/#!2/d75d9/4

2
select * from employee where empsalary IN(select empsalary from employee  group by empsalary having count(empsalary ) > 1)

Try This.It gives 2 rows as you want.

Demo: http://sqlfiddle.com/#!2/d75d9/6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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