0

I have employee table with emp id (emp_id) and department (dep_id) fields. An employee could be working in more than one Department. I want to write a sql query to display unique emp_ids who work in more than one department.

Pl help me to write sql query.

Thx

Fortune
  • 29
  • 1
  • 6

2 Answers2

3

Answered here: SQL query for finding records where count > 1 You need to use count, group by and having like this.

select emp_id, count(dep_id)
from employee_department
group by emp_id
having count(dep_id)>1
Steve Robertson
  • 209
  • 5
  • 12
0

Query

SELECT COUNT(*)
  FROM
(
  SELECT id_employee, COUNT(*) AS CNT
    FROM Department_Employee
GROUP BY id_employee
) AS T
WHERE CNT > 1
Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45
Prabhat Sinha
  • 1,500
  • 20
  • 32