Oracle的EXISTS

--EXISTS is a conditional operator in SQL. It is used to determine whether a subquery returns a result set. If a result set is returned, true is returned, otherwise False is returned.

EXISTS 语法:

SELECT 
字段名
FROM  表1
WHERE 筛选条件
AND EXISTS(
SELECT 1 FROM 表2 
WHERE 值1 =值2 -- 关联的字段
AND 筛选条件
)

Check that there is no one in that department 

SELECT 
DEPTNO 
FROM DEPT D
WHERE NOT EXISTS (
SELECT 1 
FROM EMP E 
WHERE E.DEPTNO = D.DEPTNO);

-- The difference in query efficiency between IN and EXISTS mainly depends on the specific query statement and the amount of data.

Generally speaking, when the amount of data is small, the query efficiency of IN and EXISTS is not much different, and IN may even be slightly better than EXISTS.
However, when processing large amounts of data, EXISTS is often faster than IN.

Because, EXISTS uses a kind of counting called semi-join, it only needs to find a match once to return the result, while IN needs to scan the entire collection to find the match. 

Guess you like

Origin blog.csdn.net/weixin_57024726/article/details/133202195