Question: There are two tables that would be relevant in this. Workon and Employee. The contents of these two tables are EMPID NAME SALARY DID (department ID) for employee and PID EMPID HOURS from work on. The SQL I have written is 

```
select e.name, w.pid
from employee e, workon w
where e.empid = w.empid
group by e.name, w.pid, w.empid
having count (e.name) > 1
order by w.pid

```

I have been trying to figure out why this code will not give me employees that work on more than one project. Please help me figure out what I am doing wrong.

Answer: 
your group by was returning one row per employee per project, by definition this could never have more than 1 row

below sql should work

WARNING: i am not sure how this will affect performance of server use at your own risk

following will return 1 row per employee per workon but be limited to employees with more than 1 workon record (so if employee has 5 workon records, you will get 5 rows with same e.name and then 5 different w.pid values)

```
select e.name, w.pid
from employee e, workon w
where e.empid = w.empid
and e.empid in (
select w.empid
from workon w -- there was a typo here originally
group by 1
having count (*) > 1
)
order by e.name, w.pid

```
You can try this below query. 

Here in a variable all distinct count department has been taken from department master table. After that only those employee has been selected where count match with distinct linked department count in relation table.

```
CREATE TABLE employees
(
    employee_id int NOT NULL CONSTRAINT pk_employees PRIMARY KEY,
    employee_name nvarchar(128) NOT NULL CONSTRAINT uk_employees_employee_name UNIQUE
);

CREATE TABLE departments
(
    department_id int NOT NULL PRIMARY KEY,
    department_name nvarchar(128) NOT NULL CONSTRAINT uk_departments_department_name UNIQUE
);

CREATE TABLE department_employees
(
    department_id int NOT NULL CONSTRAINT fk_department_employees_departments REFERENCES departments(department_id),
    employee_id int NOT NULL CONSTRAINT fk_departement_employees_employees REFERENCES employees(employee_id),
    CONSTRAINT pk_deparment_employees PRIMARY KEY (department_id, employee_id)
)

INSERT INTO employees
VALUES (1, 'John Doe'), (2, 'Jane Doe'), (3, 'William Doe'), (4, 'Margaret Doe')

INSERT INTO departments
VALUES (1, 'Accounting'), (2, 'Humman Resources'), (3, 'Marketing')

INSERT INTO department_employees
VALUES 
    (1, 1), (2, 1), (3, 1), 
    (2, 2), (2, 3),
    (3, 3), (3, 4)

declare @distinctDeptCount int 
SET @DistinctDeptCount = (SELECT Count(Distinct department_id) FROM departments)
--SELECT @DistinctDeptCount

SELECT Distinct employees.employee_id, employee_name
from employees     
where employees.employee_id in (
select employee_id from department_employees GROUP BY employee_id HAVING COUNT(department_id) >= @distinctDeptCount
)

```

Here is the live demo [Emp. with all Department](https://rextester.com/KLJFST63396)
The output is as shown below

```
employee_id employee_name
1           John Doe

```