search
HomeDatabaseOracleWhat is the usage of not exists in oracle

In Oracle, "not exists" is used to determine whether a clause returns a result set. If the clause returns a result set, it is false. If the clause does not return a result set, it is true. The syntax is "select * from daul where not exists (clause query condition)".

What is the usage of not exists in oracle

The operating environment of this tutorial: Windows 10 system, Oracle 11g version, Dell G3 computer.

What is the usage of not exists in oracle

exists: The emphasis is on whether to return the result set, and it is not required to know what is returned. For example:

  select name from student where sex = 'm' and mark exists(select 1 from grade where ...)

As long as the sub-child guided by exists If the sentence has a result set returned, then the exists condition is established. Please note that the returned field is always 1. If it is changed to "select 2 from grade where...", then the returned field is 2. This number is meaningless. So the exists clause does not care about what is returned, but whether there is a result set returned.

The biggest difference between exists and in is that the in clause can only return one field, for example:

  select name from student where sex = 'm' and mark in (select 1,2,3 from grade where ...)

, the in clause returns three fields, which is not true Correct, the exists clause is allowed, but in only allows one field to be returned. Just remove any two fields in 1, 2, and 3.

And not exists and not in are the opposites of exists and in respectively.

  • exists (sql returns the result set as true)

  • not exists (sql does not return the result set as true)

The process of not exists is described in detail below:

As follows:

Table A

ID NAME

1 A1

2 A2

3 A3

Table B

ID AID NAME

1 1 B1

##2 2 B2

3 2 B3

Table A and table B have a 1-to-many relationship A.ID => B.AID

SELECT ID,NAME FROM A WHERE EXISTS (SELECT * FROM B WHERE A.ID=B.AID)

The execution result is

1 A1

2 A2

The reason can be analyzed as follows

SELECT ID,NAME FROM A WHERE EXISTS (SELECT * FROM B WHERE B.AID=1)
--->SELECT * FROM B WHERE B.AID=1有值返回真所以有数据
SELECT ID,NAME FROM A WHERE EXISTS (SELECT * FROM B WHERE B.AID=2)
--->SELECT * FROM B WHERE B.AID=2有值返回真所以有数据
SELECT ID,NAME FROM A WHERE EXISTS (SELECT * FROM B WHERE B.AID=3)
--->SELECT * FROM B WHERE B.AID=3无值返回真所以没有数据

NOT EXISTS is the other way around

SELECT ID,NAME FROM A WHERE NOT EXIST (SELECT * FROM B WHERE A.ID=B.AID)

The execution result is

3 A3

EXISTS = IN, the meaning is the same but there is a slight difference in syntax. It seems that the efficiency of using IN is less, which may be the reason why the index will not be executed.

SELECT ID,NAME FROM A  WHERE ID IN (SELECT AID FROM B)

NOT EXISTS = NOT IN, the meaning is the same but there is a difference in syntax. Little difference

SELECT ID,NAME FROM A WHERE ID NOT IN (SELECT AID FROM B)

Sometimes we will encounter the situation where we need to select a certain column without duplication, use a certain column as the selection condition, and other columns will be output normally.

Recommended tutorial: "

Oracle Video tutorial

The above is the detailed content of What is the usage of not exists in oracle. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Oracle's Customer Base: Industries and ApplicationsOracle's Customer Base: Industries and ApplicationsApr 12, 2025 am 12:04 AM

Oracle has a wide and diverse customer base, covering multiple fields such as financial services, healthcare, retail and e-commerce, and manufacturing. 1) In financial services, Oracle FLEXCUBE improves operational efficiency and data security; 2) In health care, OracleHealth Sciences Clinical Development accelerates new drug research and development; 3) In retail and e-commerce, OracleRetail and OracleCDP achieve personalized customer experience; 4) In manufacturing, OracleERPCloud optimizes supply chain management.

How to use triggers for oracleHow to use triggers for oracleApr 11, 2025 pm 11:57 PM

Triggers in Oracle are stored procedures used to automatically perform operations after a specific event (insert, update, or delete). They are used in a variety of scenarios, including data verification, auditing, and data maintenance. When creating a trigger, you need to specify the trigger name, association table, trigger event, and trigger time. There are two types of triggers: the BEFORE trigger is fired before the operation, and the AFTER trigger is fired after the operation. For example, the BEFORE INSERT trigger ensures that the age column of the inserted row is not negative.

How to change the oracle table nameHow to change the oracle table nameApr 11, 2025 pm 11:54 PM

Two ways to rename Oracle table names: use SQL statements: ALTER TABLE <Old table name> RENAME TO <New table name>;Use PL/SQL statements: EXECUTE IMMEDIATE 'ALTER TABLE ' || :old_table_name || ' RENAME TO ' || :new_table_name;

How to return after oracle submittedHow to return after oracle submittedApr 11, 2025 pm 11:51 PM

Oracle provides the following ways to fall back on committed database changes: Use the ROLLBACK statement to immediately revoke all uncommitted changes. Operation through the database management tool interface. Use Oracle Flashback technology to return to a specific point in time and restore data, flashback logging is required.

How to read oracle rebuild indexHow to read oracle rebuild indexApr 11, 2025 pm 11:48 PM

Methods to check whether the index has been rebuilt in Oracle: DBA_INDEXES view: view REBUILT value (YES/NO); ALL_INDEXES view: view STATUS value (VALID/UNUSABLE); V$INDEX_STATISTICS view: view NUM_REBUILDS value, indicating the number of index reconstructions.

How to restore oracle upgrade failedHow to restore oracle upgrade failedApr 11, 2025 pm 11:45 PM

After the Oracle upgrade fails, follow the following steps to restore the system: Terminate recovery and switch to recovery mode. Use the recovery command to roll back the data file. Open the database and confirm that the data file is installed and restore the redo log. If the control file is corrupted, recreate it. Reopen the database in full recovery mode. Verify the restore and confirm that the data and objects are intact. If a rollback segment was created during restore, roll it back.

How to modify the oracle database password expiredHow to modify the oracle database password expiredApr 11, 2025 pm 11:42 PM

To modify an expired Oracle database password, follow these steps: 1. Exit all sessions; 2. Connect with the database with SYSDBA permissions; 3. Execute the ALTER USER command to modify the password; 4. Reconnect with the new password; 5. Execute the query to confirm that the password has been modified.

How to deal with oracle escape charactersHow to deal with oracle escape charactersApr 11, 2025 pm 11:39 PM

Escape characters in Oracle are used to indicate special characters or control sequences, including line connections, string delimiters, line breaks, carriage return, tabs, and backspace characters. Escape character processing usually involves escaping special characters in a string, using | concatenating multiline strings, and using a backslash to escape the escape character itself.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.