EXISTS and NOT EXISTS Operator in Oracle SQL | Oracle SQL Tutorials -18

Hi,

I will explain EXISTS Operator in Oracle SQL in this post of Oracle SQL Tutorial series.

Read the previous post of this tutorial series before this.

HAVING Clause in Oracle SQL | Oracle SQL Tutorials -17

 

 

 

The EXISTS Operator

The EXISTS operator is used to check if existence of any record in a subquery.

The result of this operator is TRUE or FALSE.

 

The NOT EXISTS Operator

The NOT EXISTS operator is just like EXISTS but it is REVERSE of EXISTS.

The result of this operator is TRUE or FALSE.

 

The EXISTS Syntax

EXISTS syntax is as follows.

SELECT column1, column2, column3 FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name );

SELECT column_name(s) FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);

The NOT  EXISTS Syntax

EXISTS syntax is as follows.

SELECT column1, column2, column3 FROM table_name
WHERE NOT EXISTS
(SELECT column_name FROM table_name );

SELECT column_name(s) FROM table_name
WHERE NOT EXISTS
(SELECT column_name FROM table_name WHERE condition);
EXISTS and NOT EXISTS examples are as follows.
SQL> select first_name,last_name,salary from hr.employees where department_id in (20,30,40) and  EXISTS ( select department_id from hr.employees where department_id=10); 

FIRST_NAME           LAST_NAME                     SALARY
-------------------- ------------------------- ----------
Michael              Hartstein                      14000
Pat                  Fay                             7000
Den                  Raphaely                       12000
Alexander            Khoo                            4100
Shelli               Baida                           3900
Sigal                Tobias                          3800
Guy                  Himuro                          3600
Karen                Colmenares                      3500
Susan                Mavris                          7500

9 rows selected.

SQL> 
SQL> 
SQL> select first_name,last_name,salary from hr.employees where department_id in (20,30,40) and  EXISTS ( select department_id from hr.employees where department_id=100000);

no rows selected

SQL> 


SQL> select first_name,last_name,salary from hr.employees where department_id in (20,30,40) and NOT EXISTS ( select department_id from hr.employees where department_id=100000);

FIRST_NAME           LAST_NAME                     SALARY
-------------------- ------------------------- ----------
Michael              Hartstein                      14000
Pat                  Fay                             7000
Den                  Raphaely                       12000
Alexander            Khoo                            4100
Shelli               Baida                           3900
Sigal                Tobias                          3800
Guy                  Himuro                          3600
Karen                Colmenares                      3500
Susan                Mavris                          7500

9 rows selected.

SQL> 


SQL> select count(department_id),salary from hr.employees group by department_id,salary having sum(salary)>10000 and EXISTS ( select department_id from hr.employees where department_id=10);

COUNT(DEPARTMENT_ID)     SALARY
-------------------- ----------
                   3       3600
                   1      15000
                   3      10500
                   2       9000
                   1      12500
                   2      23000
                   3       4100
                   2      10000
                   2       8500
                   1      10600
                   3       3800

COUNT(DEPARTMENT_ID)     SALARY
-------------------- ----------
                   1      11000
                   1      13008
                   5       3500
                   2      12000
                   1      13008
                   1      12000
                   4       4200
                   2       7200
                   1      14000
                   1      14500
                   1      13000

COUNT(DEPARTMENT_ID)     SALARY
-------------------- ----------
                   2      11500
                   3      11000
                   2       5800
                   2       8000

26 rows selected.

SQL>

Do you want to learn Oracle SQL Tutorial for Beginners, then read the following articles.

Oracle SQL Tutorials For Beginners – Learn Oracle SQL from scratch with Oracle SQL Online Course

 695 views last month,  1 views today

About Mehmet Salih Deveci

I am Founder of SysDBASoft IT and IT Tutorial and Certified Expert about Oracle & SQL Server database, Goldengate, Exadata Machine, Oracle Database Appliance administrator with 10+years experience.I have OCA, OCP, OCE RAC Expert Certificates I have worked 100+ Banking, Insurance, Finance, Telco and etc. clients as a Consultant, Insource or Outsource.I have done 200+ Operations in this clients such as Exadata Installation & PoC & Migration & Upgrade, Oracle & SQL Server Database Upgrade, Oracle RAC Installation, SQL Server AlwaysOn Installation, Database Migration, Disaster Recovery, Backup Restore, Performance Tuning, Periodic Healthchecks.I have done 2000+ Table replication with Goldengate or SQL Server Replication tool for DWH Databases in many clients.If you need Oracle DBA, SQL Server DBA, APPS DBA,  Exadata, Goldengate, EBS Consultancy and Training you can send my email adress [email protected].-                                                                                                                                                                                                                                                 -Oracle DBA, SQL Server DBA, APPS DBA,  Exadata, Goldengate, EBS ve linux Danışmanlık ve Eğitim için  [email protected] a mail atabilirsiniz.

Leave a Reply

Do NOT follow this link or you will be banned from the site!