C
C
Cyril2016-05-13 14:13:21
MySQL
Cyril, 2016-05-13 14:13:21

How to write such sql query?

There is a table of departments ( department ) and a table of employees ( employee ). The unique field in each of them is id.
There is also an employee-department relationship table ( employee_department ) with fields employee_id and department_id .
What will the SQL query look like to get all employees who are not, for example, in a department with ID = 7 ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2016-05-13
@belyaevcyrill

SELECT * FROM employee WHERE id NOT IN (SELECT employee_id FROM employee_departament WHERE departament_id = 7)

D
Dmitry Kovalsky, 2016-05-13
@dmitryKovalskiy

Please clarify - do you have a 1-to-1 comparison?
Then you can probably just

SELECT * FROM employee as e
INNER JOIN employee_department as ed ON e.id = ed.employeeId
WHERE ed.department_id != 7

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question