剔除那些没有分配员工的部门

发布于 2024-10-09 10:03:28 字数 165 浏览 2 评论 0原文

我想编写一个sql查询,并想从DEPT表中获取在EMP表中没有分配员工的部门名称。

表结构:

EMP
EMPNO   ENAME    DEPTNO

DEPT
DEPTNO   DNAME

所以我想知道那些没有员工协会的DEPT。

I want to write an sql query , and want to get the dept name from DEPT table who has no employees assigned in EMP table.

Table Structure:

EMP
EMPNO   ENAME    DEPTNO

DEPT
DEPTNO   DNAME

So I like to know those DEPT who has no employees association.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

南笙 2024-10-16 10:03:28

只有 NOT EXISTS

SELECT D.DNAME
FROM DEPT D
WHERE
 NOT EXISTS (SELECT * FROM EMP E WHERE D.DEPTNO = E.DEPTNO)

或 EXCEPT 才是正确的,在这种情况下更复杂

SELECT D.DNAME
FROM DEPT D
EXCEPT
SELECT D.DNAME
FROM DEPT D
JOIN 
EMP E WHERE D.DEPTNO = E.DEPTNO

两者都应该给出相同的计划(带有左反半连接)

其他答案的注释:

  • A LEFT JOIN 将给出一行每个员工。你需要 DISTINCT。与 NOT EXISTS 相比,这会损害计划。

  • 如果存在没有部门的员工,则 。列表中带有 NULL 的 NOT IN 失败

所以通常应该使用 NOT EXISTS 或 EXCEPT

It's only correct with NOT EXISTS

SELECT D.DNAME
FROM DEPT D
WHERE
 NOT EXISTS (SELECT * FROM EMP E WHERE D.DEPTNO = E.DEPTNO)

or EXCEPT, more complex in this case

SELECT D.DNAME
FROM DEPT D
EXCEPT
SELECT D.DNAME
FROM DEPT D
JOIN 
EMP E WHERE D.DEPTNO = E.DEPTNO

Both should give the same plan (with a left anti semi join)

Notes on other answers:

  • A LEFT JOIN will give one row per employee. You'd need DISTINCT. Which compromises the plan compared with NOT EXISTS

  • NOT IN will give false results if there is an Employee who has no Department. NOT IN with a NULL in the list fails

So generally one should use NOT EXISTS or EXCEPT

南渊 2024-10-16 10:03:28
select dname from dept where deptno not in (select deptno from emp)
select dname from dept where deptno not in (select deptno from emp)
最丧也最甜 2024-10-16 10:03:28
SELECT D.DNAME
FROM DEPT D
LEFT JOIN EMP E ON D.DEPTNO = E.DEPTNO
WHERE E.DEPTNO IS NULL

更新:

@bernd_k 指出 DISTINCT 是不必要的(SELECT DISTINCT D.DNAME ...)这种情况 - 即使没有它,也不会返回重复的部门。

SELECT D.DNAME
FROM DEPT D
LEFT JOIN EMP E ON D.DEPTNO = E.DEPTNO
WHERE E.DEPTNO IS NULL

UPDATE:

@bernd_k pointed out that DISTINCT is not necessary (SELECT DISTINCT D.DNAME ...) in this case - even without it no duplicate departments will be returned.

゛时过境迁 2024-10-16 10:03:28
SELECT D.DEPTNO
FROM EMP E
JOIN DEPT D ON D.DEPTNO = E.DEPTNO (+)
WHERE E.EMPNO IS NULL;
SELECT D.DEPTNO
FROM EMP E
JOIN DEPT D ON D.DEPTNO = E.DEPTNO (+)
WHERE E.EMPNO IS NULL;
剩一世无双 2024-10-16 10:03:28

您可以从 dept 表中选择 emp 表中没有的编号:

SELECT dname
FROM dept 
WHERE deptno 
NOT IN (SELECT DISTINCT deptno 
FROM emp);

You can select these departments from dept table whom numbers are not present in emp table:

SELECT dname
FROM dept 
WHERE deptno 
NOT IN (SELECT DISTINCT deptno 
FROM emp);
倾城月光淡如水﹏ 2024-10-16 10:03:28
Select DName 
from DEPT
where DName NOT IN (Select Distinct EMP.DName from EMP);
Select DName 
from DEPT
where DName NOT IN (Select Distinct EMP.DName from EMP);
日久见人心 2024-10-16 10:03:28
select  x.DEPTNO from dept x where x.DEPTNO not in 
(select d.DEPTNO from department d join
employee e where e.deptid=d.DEPTNO)

子查询用于获取与某个部门关联的所有员工:

select d.DEPTNO from department d join
employee e where e.deptid=d.DEPTNO
 and using select  x.DEPTNO from dept x where x.DEPTNO 

not in 将给出不属于任何部门的员工。

select  x.DEPTNO from dept x where x.DEPTNO not in 
(select d.DEPTNO from department d join
employee e where e.deptid=d.DEPTNO)

The sub query is used to get all the employees who are associated with a department:

select d.DEPTNO from department d join
employee e where e.deptid=d.DEPTNO
 and using select  x.DEPTNO from dept x where x.DEPTNO 

not in will give the employees who do not belong to any department.

蝶…霜飞 2024-10-16 10:03:28
SELECT ID,NAME,SAL,DEPTNAME,DEPTID
FROM emp
FULL JOIN
DEPT
ON EMP.departmentid=DEPT.DEPTID
WHERE DEPTID IS NULL
SELECT ID,NAME,SAL,DEPTNAME,DEPTID
FROM emp
FULL JOIN
DEPT
ON EMP.departmentid=DEPT.DEPTID
WHERE DEPTID IS NULL
愿与i 2024-10-16 10:03:28

下面没有使用任何 except 或 not ,并且性能方面更好

select d.dname 
from emp e right
join dept d on e.deptno=d.deptno
group by d.dname
having count(e.empno)=0

The below is not using any except or not in and performance wise it is better

select d.dname 
from emp e right
join dept d on e.deptno=d.deptno
group by d.dname
having count(e.empno)=0
七颜 2024-10-16 10:03:28

查找不包含job_id ST_MAN 的部门的department_id

Find department_id for departments that do not contain the job_id ST_MAN

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文