Not exists case

Source: Internet
Author: User

Today, a developer asks a question about not exists. Let's make an experiment.

Lab 1

SQL> select * from emp e1 where not exists (select 1 from emp e2 where e1.mgr = e2.empno );


EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------------------------------------------------------------------------------------
7839 king president 17-NOV-81 5000 10

Lab 2



SQL> select * from emp e1 where not exists (select 1 from emp e2 where e1.empno = e2.mgr );


EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------------------------------------------------------------------------------------
7369 smith clerk 7902 17-DEC-80 800 20
7499 allen salesman 7698 20-FEB-81 1600 300 30
7521 ward salesman 7698 22-FEB-81 1250 500 30
7654 martin salesman 7698 28-SEP-81 1250 1400 30
7844 turner salesman 7698 08-SEP-81 1500 0 30
7876 adams clerk 7788 23-MAY-87 1100 20
7900 james clerk 7698 03-DEC-81 950 30
7934 miller clerk 7782 23-JAN-82 1300 10


8 rows selected.


1 and 2 SQL statements are roughly the same, but the results are completely different.


Experiment 1 shows how many people have no manager

Lab 2: How many people have manager

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.