首页 > 试题广场 >

使用含有关键字exists查找未分配具体部门的员工的所有信息

[编程题]使用含有关键字exists查找未分配具体部门的员工的所有信息
  • 热度指数:111222 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
使用含有关键字exists查找未分配具体部门的员工的所有信息。
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
输出格式:
emp_no birth_date first_name last_name gender hire_date
10011 1953-11-07 Mary Sluis F 1990-01-22
示例1

输入

drop table if exists employees;
drop table if exists dept_emp;
CREATE TABLE `employees` (
  `emp_no` int(11) NOT NULL,
  `birth_date` date NOT NULL,
  `first_name` varchar(14) NOT NULL,
  `last_name` varchar(16) NOT NULL,
  `gender` char(1) NOT NULL,
  `hire_date` date NOT NULL,
  PRIMARY KEY (`emp_no`));
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
INSERT INTO employees VALUES(10001,'1953-09-02','Georgi','Facello','M','1986-06-26');
INSERT INTO employees VALUES(10002,'1964-06-02','Bezalel','Simmel','F','1985-11-21');
INSERT INTO employees VALUES(10003,'1959-12-03','Parto','Bamford','M','1986-08-28');
INSERT INTO employees VALUES(10004,'1954-05-01','Chirstian','Koblick','M','1986-12-01');
INSERT INTO employees VALUES(10005,'1955-01-21','Kyoichi','Maliniak','M','1989-09-12');
INSERT INTO employees VALUES(10006,'1953-04-20','Anneke','Preusig','F','1989-06-02');
INSERT INTO employees VALUES(10007,'1957-05-23','Tzvetan','Zielinski','F','1989-02-10');
INSERT INTO employees VALUES(10008,'1958-02-19','Saniya','Kalloufi','M','1994-09-15');
INSERT INTO employees VALUES(10009,'1952-04-19','Sumant','Peac','F','1985-02-18');
INSERT INTO employees VALUES(10010,'1963-06-01','Duangkaew','Piveteau','F','1989-08-24');
INSERT INTO employees VALUES(10011,'1953-11-07','Mary','Sluis','F','1990-01-22');
INSERT INTO dept_emp VALUES(10001,'d001','1986-06-26','9999-01-01');
INSERT INTO dept_emp VALUES(10002,'d001','1996-08-03','9999-01-01');
INSERT INTO dept_emp VALUES(10003,'d004','1995-12-03','9999-01-01');
INSERT INTO dept_emp VALUES(10004,'d004','1986-12-01','9999-01-01');
INSERT INTO dept_emp VALUES(10005,'d003','1989-09-12','9999-01-01');
INSERT INTO dept_emp VALUES(10006,'d002','1990-08-05','9999-01-01');
INSERT INTO dept_emp VALUES(10007,'d005','1989-02-10','9999-01-01');
INSERT INTO dept_emp VALUES(10008,'d005','1998-03-11','2000-07-31');
INSERT INTO dept_emp VALUES(10009,'d006','1985-02-18','9999-01-01');
INSERT INTO dept_emp VALUES(10010,'d005','1996-11-24','2000-06-26');
INSERT INTO dept_emp VALUES(10010,'d006','2000-06-26','9999-01-01');

输出

10011|1953-11-07|Mary|Sluis|F|1990-01-22
    select *
    from employees a
    where not exists(
        select 1 from dept_emp b
        where a.emp_no=b.emp_no
    )

发表于 2022-04-17 20:10:49 回复(0)
select * from employees 
where not exists
(select * from dept_emp where dept_emp.emp_no = employees.emp_no)

我大为震撼
发表于 2021-12-23 10:04:23 回复(0)
select *
from employees e
where not exists(
select *
from dept_emp d
where e.emp_no=d.emp_no);
not exists是不是可以理解成取非。
如果括号里有值返回,那就是真;再取一个not,那就变成假,就不选这一条数据。
发表于 2021-12-02 09:42:27 回复(0)
select * from employees ep
where not  exists (select *from dept_emp dp 
             where ep.emp_no = dp.emp_no);

发表于 2021-03-10 14:18:24 回复(0)
select * from employees where not EXISTS(
select emp_no from employees where employees.emp_no=dept_emp.emp_no);
为什么from 员工表不行?
我选择员工表所有分配部门的员工,然后NOT EXISTS不存在,反向不就是没分配部分的员工了?
发表于 2021-02-13 13:46:23 回复(1)
使用where not exists
select * 
from employees e
where not exists (select emp_no from dept_emp d 
                      where e.emp_no=d.emp_no );

发表于 2021-01-25 14:49:46 回复(0)
-- 集合差集思想
select t1.*
from employees as t1
where not exists (select emp_no from dept_emp where t1.emp_no=emp_no)
;
发表于 2020-07-20 14:10:07 回复(0)

麦迪擦边球法

select e.*
from employees e
left join dept_emp de
on e.emp_no = de.emp_no
where de.dept_no is null;


发表于 2020-04-15 11:34:53 回复(2)
select * from employees where  not exists 
(select emp_no from dept_emp  where emp_no = employees.emp_no)
发表于 2018-03-20 09:43:49 回复(0)
not exists 表示a的emp_no一个都等于b.emp_no都不存在
select * from employees a where not exists(select * from dept_emp b where a.emp_no =b.emp_no)
发表于 2018-03-13 17:08:33 回复(0)
本题可以用EXISTS和IN两种方法:

/* 1. EXISTS */
SELECT *
FROM employees
WHERE NOT EXISTS (SELECT emp_no
                 FROM dept_emp
                 WHERE employees.emp_no = dept_emp.emp_no);

/* 2. IN */
SELECT *
FROM employees
WHERE emp_no NOT IN (SELECT emp_no
                    FROM dept_emp);

/*
    1.什么时候用EXISTS,什么时候用IN?
        主表为employees,从表为dept_emp,在主表和从表都对关联的列emp_no建立索引的前提下:
            当主表比从表大时,IN查询的效率较高;
            当从表比主表大时,EXISTS查询的效率较高;
        原因如下:
            in是先执行子查询,得到一个结果集,将结果集代入外层谓词条件执行主查询,子查询只需要执行一次
            exists是先从主查询中取得一条数据,再代入到子查询中,执行一次子查询,判断子查询是否能返回结果,主查询有多少条数据,子查询就要执行多少次
*/
发表于 2019-11-14 10:19:29 回复(12)

关于使用Exists( != )不能通过的原因:

Exists的用法:

exists对外表用loop逐条查询,每次查询都会查看exists的条件语句,当 exists里的条件语句能够返回记录行时(无论记录行是的多少,只要能返回),条件就为真,返回当前loop到的这条记录;反之如果exists里的条 件语句不能返回记录行,则当前loop到的这条记录被丢弃,exists的条件就像一个bool条件,当能返回结果集则为true,不能返回结果集则为 false。

(上面太长了,balabala,看下面^_^)
总的来说,如果外表有n条记录,那么exists查询就是将这n条记录逐条取出,然后判断n遍exists条件
如果使用:

select * from employees
where  exists( select emp_no from dept_emp where dept_emp.emp_no != employees.emp_no)

那么,Exists都会从外表employees里面逐条比对,如,第一条的emp_no = '10001',那么
Exists判断:

select emp_no from dept_emp where dept_emp.emp_no !='10001'

可想而知,一定存在不等于10001的结果集。那么上面的查询语句其实也就等效于:

select * from employees

参考链接:https://www.cnblogs.com/beijingstruggle/p/5885137.html

发表于 2018-09-07 15:09:21 回复(5)
本题用 EXISTS 关键字的方法如下:意为在 employees 中挑选出令(SELECT emp_no FROM dept_emp WHERE emp_no = employees.emp_no)不成立的记录,也就是当 employees.emp_no=10011的时候。反之,把NOT去掉,则输出 employees.emp_no=10001~10010时的记录。
SELECT * FROM employees WHERE NOT EXISTS 
(SELECT emp_no FROM dept_emp WHERE emp_no = employees.emp_no)
由于 OJ系统没有限制我们只能使用 EXISTS 关键字,因此还能用 NOT IN 关键字替换,即在employees 中选出 dept_emp 中没有的 emp_no。
SELECT * FROM employees WHERE emp_no NOT IN (SELECT emp_no FROM dept_emp)

编辑于 2018-06-13 10:06:08 回复(21)
考点:exists 关键字的基本使用。
其实我看了好多遍都没太理解exists怎么用,看了一遍讨论区找到了一个通俗易懂的解释:在 employees 中挑选出令(SELECT emp_no FROM dept_emp WHERE emp_no = employees.emp_no)不成立的记录。感谢作者ciphersaw的解答。
也就是说,其实是将T表的emp_no代入子查询中看是否成立,not exists 就是不成立。
// 所以子查询中 emp_no 不是那么重要,只是随便找一个字段使语句成立
select *
from employees T
where not exists (
    select emp_no from dept_emp T2 where T2.emp_no = T.emp_no
)

//所以你可以写成
select *
from employees T
where not exists (
    select 0 from dept_emp T2 where T2.emp_no = T.emp_no
)

select *
from employees T
where not exists (
    select '哈哈哈' from dept_emp T2 where T2.emp_no = T.emp_no
)
//关键在于里面条件成不成立


发表于 2021-03-27 16:58:19 回复(4)
select * from employees ee where  not exists (
	select emp_no from dept_emp de where de.emp_no = ee.emp_no
)
exists关键字:强调的是是否返回结果集,不要求知道返回什么。只要返回了字段,就是真。
发表于 2017-08-31 20:36:55 回复(1)
这里解释一下下面两种答案,有误请及时提醒:
1、正确答案:
select * from employees 
where not exists (
                    select emp_no from dept_emp 
                    where emp_no = employees.emp_no
                  );
2、错误答案:
select * from employees 
where not exists (
                    select emp_no from employees 
                    where emp_no = dept_emp.emp_no
                  );
解析:
(1)先查出主查询的记录:employees表中所有的行
(2)将主查询中的记录逐条放入exists子查询,如果返回false,则保留该记录(因为是not exists)
(3)主查询的记录会逐条放入上面加粗的地方
(4)对于错误答案,关键在于没有将主查询和子查询关联,主查询的结果无法代入子查询,这样就会造成:在两张表的数据固定的情况下,子查询返回的是固定的                      
发表于 2020-05-21 16:13:58 回复(4)
select * from employees
where  exists(select * from dept_emp where employees.emp_no<>dept_emp.emp_no);这个和
select * from employees
where not exists(select * from dept_emp where employees.emp_no=dept_emp.emp_no);这个答案有啥区别?为啥下面的可以上面的不可以
发表于 2020-01-13 15:35:26 回复(3)
求助好心人:为什么左连接表的时候不行呢?
select * from employees where not exists
(select dp.emp_no from dept_emp as dp left join employees as e on dp.emp_no = e.emp_no);
发表于 2020-04-20 10:38:00 回复(5)
运行代码:
SELECT *
FROM employees e
WHERE NOT EXiSTS (
SELECT de.emp_no
 FROM dept_emp de 
WHERE e.emp_no = de.emp_no);

解题思路:

IN:确定给定的值是否与子查询或列表中的值相匹配。in在查询的时候,首先查询子查询的表,然后将内表和外表做一个笛卡尔积,然后按照条件进行筛选。所以相对内表比较小的时候,in的速度较快。

EXISTS:指定一个子查询,检测行的存在。遍历循环外表,然后看外表中的记录有没有和内表的数据一样的。匹配上就将结果放入结果集中。





发表于 2019-05-27 14:28:33 回复(0)
SELECT *
FROM employees
WHERE NOT EXISTS (
    SELECT emp_no
    FROM dept_emp
    WHERE emp_no = employees.emp_no)

——果果果子
发表于 2022-03-24 16:32:49 回复(0)

问题信息

难度:
145条回答 14018浏览

热门推荐

通过挑战的用户

查看代码